外部ラテックスパッケージ| Java用のAspose.TeX
外部ラテックスパッケージ
JavaのAspose.TeXライブラリには、 一般的なラテックスパッケージが含まれています。したがって、これらのパッケージをライブラリのTexエンジンに提供する方法に注意する必要はありません。しかし、(おそらく)ラテックスファイルには、パッケージの「ネイティブにサポートされている「バンドル」を超えたパッケージが必要になる場合があります。この場合、 setRequiredInputDirectory() texoptionsクラスインスタンスのメソッドを使用して、必要な入力、つまり必要なパッケージのソースファイルを提供することを試みることができます。これが2つの例でどのように機能するかを確認します。
未解決の必要な入力(「ファンシーボックス」パッケージ)
次の単純なラテックスファイルがあるとしましょう。
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}
3行目では、ファイルには「ネイティブに」サポートされていない「ファンシーボックス」パッケージが必要であることがわかります。また、「ファンシーボックス」パッケージソースファイルがあると仮定します。シンプルなパッケージなので、実際には1つのファイルで構成されています。このファイルをファイルシステムのどこにでも配置し、次のようにディレクトリパスを指定できます。
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
パッケージ)アーカイブ
次に、次の非常に単純なLaTexファイルもあります。これは、「必要な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}
3行目では、このファイルが「pgfplots」パッケージを必要としていることがわかりますが、これも「ネイティブ」サポートされていません。ここでも、pgfplots
パッケージのソースファイルが存在することを前提としています。これは、LaTeX 組版アプリケーションのインストールディレクトリで見つけると、2つの場所に分かれた非常に大きなファイルセットです。pgfplots
フォルダは、\tex\generic
フォルダと \tex\latex
フォルダの両方にあります。そして、これら両方のフォルダの内容は、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 です。
外部フォントパッケージを使用します
通常、LaTeXディストリビューションには、タイプセッティングに使用できるデフォルトのフォントがいくつか付属しています。これらのフォントに加えて、Aspose.TeX は amsfonts
、eurosym
、wasysym
といった非標準フォントパッケージも提供しています。前述の通り、Aspose.TeX は外部パッケージの使用もサポートしていますが、ここでは主にフォントを含まないパッケージに焦点を当ててきました。
Aspose.TeX の Object TeX エンジン拡張機能には、フォント マップが必要です。フォント マップは、各 TeX の内部フォント名に対応する物理フォントを指定する、拡張子 .map
を持つテキスト ファイルです。これらのフォント マップは、初期化フェーズで TeX のメモリに読み込む必要があり、パッケージ ファイルに含める必要があります。エンジンはすべてのフォント マップの名前を認識していないため、拡張子 .map
を持つファイルを検索します。そのため、
RequiredInputDirectory オプションの値として使用される
IInputWorkingDirectory インターフェイスの実装では、拡張子に基づいてファイル名のコレクションにアクセスする方法を提供する必要があります。具体的には、この実装は
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ライブラリで動作しない可能性が最も高くなります。後者はlatex2e*カーネルに基づいているためです。
また、ラテックスファイルが必要とするパッケージは、asops.texライブラリのオブジェクトtexエンジンによってサポートされていないデバイス依存のプリミティブコマンドを直接呼び出すことができます。残念ながら、このようなパッケージは確かに機能しません。