外部乳胶软件包| Java的Aspose.TeX
外部乳胶软件包
Aspose.TeX库中包含许多 常见的乳胶软件包。因此,您不必关心如何将这些包装提供给图书馆的Tex引擎。但是有时(可能经常)您的乳胶文件可能需要一个包裹以外的包装包装。在这种情况下,您可以尝试使用 setRequiredInputDirectory() TexOptions类实例的方法提供所需的输入,即所需的软件包的源文件。我们将看到这两个示例如何工作。
打开包装所需的输入(fancybox
软件包)
假设我们有以下简单的乳胶文件,这是我们的 示例解决方案:
1\documentclass{article}
2\usepackage[a6paper,landscape]{geometry}
3\usepackage{fancybox}
4\begin{document}
5Test: \fbox{
6 \begin{Bitemize}[b]
7 \item First item
8 \item A second one\\ on two lines
9 \item(2pt) A third with extra space
10 \end{Bitemize}
11}
12\par\bigskip
13Test: \fbox{
14 \begin{Beqnarray}[t]
15 y & = & x^2 \\
16 a^2 + 2ab + b^2 & = & (a + b)^2 \\
17 \int_0^\infty e^{-ax} dx & = & \frac{1}{a}
18 \end{Beqnarray}
19}
20\end{document}
在第三行上,我们看到该文件需要“ fancybox”软件包,而不是“本地”支持。我们还假设我们有“ fancybox”软件包源文件。这是一个简单的软件包,因此它实际上仅由一个文件组成。我们可以将此文件放置在文件系统中的任何位置,并简单地指定目录路径:
1options.setRequiredInputDirectory(new InputFileSystemDirectory("path-to-directory-where-fancybox.sty-located"));
使用此选项运行Tex作业(不要忘记根据需要调整其他选项)后,我们将获得输出文档(即PNG图像)。
这是该示例的完整源代码:
1// Create conversion options for Object LaTeX format upon Object TeX engine extension.
2TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
3// Specify a file system working directory for the output.
4options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
5// Specify a file system working directory for the required input.
6// The directory containing packages may be located anywhere.
7options.setRequiredInputDirectory(new InputFileSystemDirectory(Utils.getInputDirectory() + "packages"));
8// Initialize the options for saving in PNG format.
9options.setSaveOptions(new PngSaveOptions());
10// Run LaTeX to PNG conversion.
11new TeXJob(Utils.getInputDirectory() + "required-input-fs.tex", new ImageDevice(), options).run();
存档所需的输入(pgfplots
软件包)
现在,让我们假设我们也有以下简单的乳胶文件,这是我们的示例解决方案中的“必需输入” zip.tex。
1\documentclass{article}
2\usepackage[margin=0.25in]{geometry}
3\usepackage{pgfplots}
4\pgfplotsset{width=10cm,compat=1.18}
5\begin{document}
6
7First example is 2D and 3D math expressions plotted side-by-side.
8
9%Here begins the 2D plot
10\begin{tikzpicture}
11\begin{axis}
12\addplot[color=red]{exp(x)};
13\end{axis}
14\end{tikzpicture}
15%Here ends the 2D plot
16\hskip 5pt
17%Here begins the 3D plot
18\begin{tikzpicture}
19\begin{axis}
20\addplot3[
21 surf,
22]
23{exp(-x^2-y^2)*x};
24\end{axis}
25\end{tikzpicture}
26%Here ends the 3D plot
27
28\end{document}
在第三行上,我们看到该文件需要“ PGFPLOTS”软件包,这也不是“本地”支持的。同样,我们假设我们有“ PGFPLOTS”软件包源文件。如果您在任何乳胶排放应用程序的安装目录中找到两个位置,则在两个位置之间进行了大量文件。您可以在\ tex \ generic
和\ tex \ latex
文件夹上找到pgfplots
文件夹。并且必须根据需要输入Aspose.TeX库提供两个文件夹的内容。我们希望将这些源文件包装在zip存档中,因此这是存档的布局:
这是我们指定对这些源文件的访问的方式:
1final Stream zipStream = File.Open("path-to-zip-with-pgfplots-sources"), FileMode.Open))
2try {
3 ...
4 options.setRequiredInputDirectory(new InputZipDirectory(zipStream));
5 ...
6} finally {
7 if (zipStream != null)
8 zipStream.close();
9}
通过此选项运行Tex作业后,我们将获得输出文档:
这是该示例的完整源代码:
1// Create conversion options for Object LaTeX format upon Object TeX engine extension.
2TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
3// Specify a file system working directory for the output.
4options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
5// Initialize the options for saving in PNG format.
6options.setSaveOptions(new PngSaveOptions());
7// Create a file stream for the ZIP archive containing the required package.
8// The ZIP archive may be located anywhere.
9final InputStream stream = new FileInputStream(Utils.getInputDirectory() + "packages\\pgfplots.zip");
10try {
11 // Specify a ZIP working directory for the required input.
12 options.setRequiredInputDirectory(new InputZipDirectory(stream, ""));
13
14 // Run LaTeX to PNG conversion.
15 new TeXJob(Utils.getInputDirectory() + "required-input-zip.tex", new ImageDevice(), options).run();
16} finally {
17 if (stream != null)
18 stream.close();
19}
注意: 结果是使用
pgfplots
软件包版本 1.18.1 验证的。而 Aspose.TeX 库中包含的pfg
软件包版本为 3.1.9a。
使用外部字体软件包
通常,乳胶发行版带有一系列可用于排版的默认字体。除了这些字体外,Aspose.TeX还提供几个非标准字体软件包,例如“ amsfonts”,“ eurosym”和“ wasysym”。如前所述,Aspose.TeX也支持使用外部软件包的使用,尽管我们的重点主要集中在不包括字体的软件包上。
Aspose.TeX中的对象Tex引擎扩展名需要字体映射,它是带有.map
扩展名的文本文件,该文件指定了与每个Tex的内部字体名称相对应的物理字体。这些字体地图需要在初始化阶段加载到TEX的内存中,并且必须包含在软件包文件中。由于引擎不知道所有字体映射的名称,因此它搜索了具有`.map’扩展名的任何文件。因此,将
IinputWorkingDirectory用作
InsultInputDirectory选项的接口的实现必须提供一种基于其扩展名的访问文件名的收集的方法。具体而言,此实现还必须实现
IfileCollector接口。标准实现,
InputFileSystemDirectory和
InputzipDirectory,已经满足此要求。
下面,我们提供一个自定义所需输入目录的示例,该目录还实现了 IfileCollector接口。我们的主要重点是根据其扩展名来收集文件名,我们故意省略了文件存储和检索的详细信息。
1// This is an implementation of IInputWorkingDirectory that is suitable for the TeX job's RequiredInputDirectory option
2// in case required input contains fonts provided by external packages.
3// The class additionally implements IFileCollector, which provides access to file collections by extension.
4// This is necessary to load external font maps, which are files (outside TeX syntax) that map TeX's
5public class RequiredInputDirectory implements IInputWorkingDirectory, IFileCollector
6{
7 private Map<String, Map<String, String>> _fileNames = new HashMap<String, Map<String, String>>();
8
9 public RequiredInputDirectory()
10 {
11 }
12
13 // This method should preliminarily be called for each file entry that is supposed to be located inside
14 // the required input directory. Inside is an example of how the dictionary of file names could be organized
15 // for easy collection of file names by extension.
16 // Here fileName is a full file name. This can be a file path on a file system, a URL, or whatever else (theoretically).
17 public void storeFileName(String fileName)
18 {
19 String extension = getExtension(fileName);
20 String name = getFileNameWithoutExtension(fileName);
21
22 Map<String, String> files = _fileNames.get(extension);
23 if (files == null)
24 _fileNames.put(extension, files = new HashMap<String, String>());
25
26 files.put(name, fileName);
27 }
28
29 // The IInputWorkingDirectory implementation.
30 public TeXInputStream getFile(String fileName, boolean searchSubdirectories)
31 {
32 return new TeXInputStream(null, fileName); // Here we actually return a stream for the file requested by its name.
33 }
34
35 // Here is how we gather file collections by extension.
36 public String[] getFileNamesByExtension(String extension)
37 {
38 return getFileNamesByExtension(extension, null);
39 }
40
41 // Here is how we gather file collections by extension.
42 public String[] getFileNamesByExtension(String extension, String path)
43 {
44 Map<String, String> files = _fileNames.get(extension);
45 if (files == null)
46 return new String[0];
47
48 return files.values().toArray(new String[0]);
49 }
50
51 private String getExtension(String fileName)
52 {
53 int pos = fileName.indexOf('.');
54 if (pos < 0)
55 return "";
56
57 return fileName.substring(pos);
58 }
59
60 private String getFileNameWithoutExtension(String fileName)
61 {
62 int pos = fileName.lastIndexOf('/');
63 if (pos >= 0)
64 fileName = fileName.substring(pos + 1);
65
66 pos = fileName.indexOf('.');
67 if (pos < 0)
68 return fileName;
69
70 return fileName.substring(0, pos);
71 }
72
73 public void close()
74 {
75 _fileNames.clear();
76 }
77}
限制
您的乳胶文件要求的包装可以在 * latex3e 内核下开发。这样的软件包很可能无法与Aspose.TeX库一起使用,因为后者基于lastx2e*内核。
此外,您的乳胶文件要求的软件包可以直接调用Aspose.TeX库对象Tex引擎不支持的设备依赖的原始命令。不幸的是,这样的软件包无法确定。