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} の間にある本文から構成されます。

inputFile に invalid-latex.tex の完全パス、outputDirectory に既存の出力フォルダー、packagesDirectory に fancyhdr.sty と依存ファイルを含むフォルダーを指定します。以下のコールバックを import 文とともに PackageGuesser.java として保存し、修復処理の前に PackageGuesser packageGuesser = new PackageGuesser() を作成します。使用する Aspose.TeX クラスをインポートし、例外を処理または宣言してください。 ライセンスガイドに従ってライセンスを適用します。入出力ディレクトリは修復が終わるまで開いたままにし、その後閉じます。

 1// Repair the LaTeX structure and infer packages for undefined commands.
 2// inputFile is the full invalid LaTeX path; packagesDirectory contains fancyhdr.sty.
 3// outputDirectory is an existing output folder; packageGuesser is a PackageGuesser instance.
 4LaTeXRepairerOptions options = new LaTeXRepairerOptions();
 5OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
 6InputFileSystemDirectory packages = new InputFileSystemDirectory(packagesDirectory);
 7options.setOutputWorkingDirectory(output);
 8options.setRequiredInputDirectory(packages);
 9options.setGuessPackageCallback(packageGuesser);
10TeXJobResult result;
11try {
12    result = new LaTeXRepairer(inputFile, options).run();
13} finally {
14    output.close();
15    packages.close();
16}

通常の TeX ジョブと同様に、まず処理に必要なオプションを保持するオブジェクトを作成します。これらのオプションは通常の TeX ジョブとほぼ同じです。たとえば、 setInputWorkingDirectory() メソッドで入力ファイルを読むディレクトリを指定しますが、この例ではファイルシステム上のメイン入力ファイルへのフルパスを直接渡すため使用していません。次に、 setOutputWorkingDirectory() メソッドで出力ファイルを書き込むディレクトリを指定します。必要に応じて、 setRequiredInputDirectory() メソッドで、Aspose.TeX ライブラリに組み込まれていない LaTeX パッケージを格納できるディレクトリを指すことができます。最後に、 setGuessPackageCallback() メソッドについては後述します。

オプションが設定されたら、プロセスを実行します。

では、チェックと修復の流れはどのようになるのでしょうか。まず API は入力ファイル内に \documentclass があるか検索します。見つからなければ、API はファイルの先頭に \documentclass{article} を挿入すべきだと判断し、修復レポート(.log)にその情報を記録します。

次に、調整された入力ファイルの先頭からスキャンを開始します。LaTeX フォーマットが組み込まれた TeX エンジンは、途中で \begin{document} がまだ見つからないというエラーを出すことがあります。このとき、 \begin{document} を挿入すべき位置が特定され、レポートに反映されます。

ファイルをさらに走査していくと、エンジンは未定義のコマンドや環境に出くわすことがあります。そのような場合、API は最も一般的なコマンドや環境に対して、 埋め込み必須パッケージ(これらのコマンドや環境を定義するパッケージ)を自動的に推測して使用します。ただし、 IGuessPackageCallback インターフェイスを実装したクラスを作成すれば、外部からこの推測ロジックをカスタマイズできます。そのクラスのインスタンスは setGuessPackageCallback() メソッドでプロセスに渡します。

この例は \lhead コマンドを fancyhdr パッケージに対応付けます。コールバックには先頭のバックスラッシュを除いた名前が渡され、未知のコマンドや環境には空文字列を返します。

 1import com.aspose.tex.features.IGuessPackageCallback;
 2
 3// Save as PackageGuesser.java and assign an instance with setGuessPackageCallback().
 4public class PackageGuesser implements IGuessPackageCallback {
 5    @Override
 6    public String guessPackage(String commandName, boolean isEnvironment) {
 7        // The command name has no leading backslash.
 8        if (!isEnvironment && "lhead".equals(commandName)) {
 9            return "fancyhdr";
10        }
11        // Empty means that this callback cannot identify a package.
12        return "";
13    }
14}

サンプルファイルについて説明すると、エンジンは最初に \chapter コマンドに出会います。これは article クラスには定義されていませんが、book クラスには定義されています。そのため API はドキュメントクラスを \documentclass{book} に変更します。次に \lhead コマンドが見つかり、プリアンブルに \usepackage{fancyhdr} を挿入すべきと判断します。さらにファイル後半で \href と \includegraphics が現れたため、Repairer はそれぞれ \usepackage{hyperref} と \usepackage{graphicx} をプリアンブルに追加します。これらの判断は API の内部マッピングに基づいて行われ、すべての修復内容はレポートファイルに記録されます。

最終的に、LaTeX 文書の正しい終了部が欠如しているためエンジンは異常終了します。その結果、Repairer はファイル末尾に \end{document} を付加し、レポートにもその旨を記載します。

修復後、Repairer は最終確認のために TeX ジョブを実行します。この例の最終レポートには致命的なエラーがありません。run() の戻り値は元のスキャンの状態を示す場合があるため、修復レポートと修正済み .tex ファイルを確認してください。修復処理は不足している外部リソースを提供しません。たとえばサンプルが参照する Figures/uet_logo.pdf は、完全な組版のために別途用意する必要があります。

以下は完全なレポートです。

 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 インターフェイスのより高度な実装が含まれています。