LaTeX ファイルの修復 | Aspose.TeX for Java
LaTeX ファイルのチェックと修復方法
テキストファイルが LaTeX ファイルかどうか確信が持てず、組版したい場合は、Aspose.TeX API for Java が提供する LaTeX のチェックおよび修復機能を利用できます。以下の例では、Aspose.TeX for Java のサンプルプロジェクトにある invalid-latex.tex というファイルをチェックし、修復します。
まず最初に、サンプルファイルは TeX 構文を使用していますが、LaTeX に必要な構造が欠けていることに注意してください。ご存知のとおり、LaTeX ファイルは \documentclass コマンドで始まるプリアンブルと、\begin{document} と \end{document} の間にある本文から構成されます。
それでは Java のコードサンプルを見てみましょう。
1// Create repair options.
2LaTeXRepairerOptions options = new LaTeXRepairerOptions();
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// Specify the callback class to externally guess packages required for undefined commands or environments.
9options.setGuessPackageCallback(new PackageGuesser());
10
11// Run the repair process.
12new LaTeXRepairer(Utils.getInputDirectory() + "invalid-latex.tex", options).run();通常の TeX ジョブと同様に、まず処理に必要なオプションを保持するオブジェクトを作成します。これらのオプションは通常の TeX ジョブとほぼ同じです。たとえば、 setInputWorkingDirectory() メソッドで入力ファイルを読むディレクトリを指定しますが、この例ではファイルシステム上のメイン入力ファイルへのフルパスを直接渡すため使用していません。次に、 setOutputWorkingDirectory() メソッドで出力ファイルを書き込むディレクトリを指定します。必要に応じて、 setRequiredInputDirectory() メソッドで、Aspose.TeX ライブラリに組み込まれていない LaTeX パッケージを格納できるディレクトリを指すことができます。最後に、 setGuessPackageCallback() メソッドについては後述します。
オプションが設定されたら、プロセスを実行します。
では、チェックと修復の流れはどのようになるのでしょうか。まず API は入力ファイル内に \documentclass があるか検索します。見つからなければ、API はファイルの先頭に \documentclass{article} を挿入すべきだと判断し、修復レポート(.log)にその情報を記録します。
次に、調整された入力ファイルの先頭からスキャンを開始します。LaTeX フォーマットが組み込まれた TeX エンジンは、途中で \begin{document} がまだ見つからないというエラーを出すことがあります。このとき、 \begin{document} を挿入すべき位置が特定され、レポートに反映されます。
ファイルをさらに走査していくと、エンジンは未定義のコマンドや環境に出くわすことがあります。そのような場合、API は最も一般的なコマンドや環境に対して、
埋め込み必須パッケージ(これらのコマンドや環境を定義するパッケージ)を自動的に推測して使用します。ただし、
IGuessPackageCallback インターフェイスを実装したクラスを作成すれば、外部からこの推測ロジックをカスタマイズできます。そのクラスのインスタンスは setGuessPackageCallback() メソッドでプロセスに渡します。
以下は、\head コマンドを fancyhdr パッケージにマッピングする簡単な例です。
1// The callback class to externally guess packages required for undefined commands or environments.
2public static class PackageGuesser implements IGuessPackageCallback
3{
4 private Map<String, String> _map = new HashMap<String, String>();
5
6 public PackageGuesser()
7 {
8 _map.put("lhead", "fancyhdr"); // Defines the mapping between the \lhead command and the fancyhdr package.
9 }
10
11 public String guessPackage(String commandName, boolean isEnvironment)
12 {
13 if (!isEnvironment)
14 {
15 String packageName = _map.get(commandName);
16 return packageName != null ? packageName : ""; // It's better to return an empty string to avoid consequent calls for the same command name.
17 }
18
19 // Some code for environments
20 // ...
21
22 return "";
23 }
24}サンプルファイルについて説明すると、エンジンは最初に \chapter コマンドに出会います。これは article クラスには定義されていませんが、book クラスには定義されています。そのため API はドキュメントクラスを \documentclass{book} に変更します。次に \lhead コマンドが見つかり、プリアンブルに \usepackage{fancyhdr} を挿入すべきと判断します。さらにファイル後半で \href と \includegraphics が現れたため、Repairer はそれぞれ \usepackage{hyperref} と \usepackage{graphics} をプリアンブルに追加します。これらの判断は API の内部マッピングに基づいて行われ、すべての修復内容はレポートファイルに記録されます。
最終的に、LaTeX 文書の正しい終了部が欠如しているためエンジンは異常終了します。その結果、Repairer はファイル末尾に \end{document} を付加し、レポートにもその旨を記載します。
修正された元ファイルを作成した後、Repairer は最終確認のためにそのファイルで TeX ジョブを実行します。この実行で致命的なエラーが検出されなければ、修正後のファイルは期待通りに組版可能です。
以下は完全なレポートです。
1Trying to repair the original file...
2--------------------------------------------------------------------------------
3\documentclass is missing in the original file. Inserted at the beginning.
4\begin{document} is missing in the original file. Inserted at line 3, pos. 0.
5The command \chapter at line 3, pos. 0 is undefined. Consider using \usepackage{package_name} in the preamble,
6 where 'package_name' is the name of the package which defines this command.
7The command \lhead at line 5, pos. 0 is undefined. \usepackage{fancyhdr} is inserted in the preamble
8 since the 'fancyhdr' package supposedly defines the command.
9The command \href at line 8, pos. 0 is undefined. \usepackage{hyperref} is inserted in the preamble
10 since the 'hyperref' package supposedly defines the command.
11The command \href at line 17, pos. 0 is undefined. \usepackage{hyperref} is inserted in the preamble
12 since the 'hyperref' package supposedly defines the command.
13The command \href at line 20, pos. 0 is undefined. \usepackage{hyperref} is inserted in the preamble
14 since the 'hyperref' package supposedly defines the command.
15The command \href at line 27, pos. 0 is undefined. \usepackage{hyperref} is inserted in the preamble
16 since the 'hyperref' package supposedly defines the command.
17The command \href at line 32, pos. 0 is undefined. \usepackage{hyperref} is inserted in the preamble
18 since the 'hyperref' package supposedly defines the command.
19The command \includegraphics at line 54, pos. 2 is undefined. \usepackage{graphicx} is inserted in the preamble
20 since the 'graphicx' package supposedly defines the command.
21The command \href at line 67, pos. 0 is undefined. \usepackage{hyperref} is inserted in the preamble
22 since the 'hyperref' package supposedly defines the command.
23The command \href at line 95, pos. 57 is undefined. \usepackage{hyperref} is inserted in the preamble
24 since the 'hyperref' package supposedly defines the command.
25The command \href at line 96, pos. 0 is undefined. \usepackage{hyperref} is inserted in the preamble
26 since the 'hyperref' package supposedly defines the command.
27The command \href at line 98, pos. 100 is undefined. \usepackage{hyperref} is inserted in the preamble
28 since the 'hyperref' package supposedly defines the command.
29\end{document} is missing in the original file. Inserted at the end.
30
31Checking the repaired file...
32--------------------------------------------------------------------------------
33There are no critical errors in the fixed file.また、
AI LaTeX Repairer の無料ウェブアプリもぜひお試しください。このアプリは
Aspose.TeX for .NET API に実装された機能をベースにしており、IGuessPackageCallback インターフェイスのより高度な実装が含まれています。