ラテックスファイルの修理| .netのAspose.TeX
ラテックスファイルを確認して修復する方法
ラテックスファイルであると思われるテキストファイルがあり、それがタイプセットになりたいが、それが実際にラテックスファイルであるかどうかはわかりません(おそらくあなたはラテックスの世界に新しくなっています)。次の例では、.NET例プロジェクトのAspose.TeXから、サンプルファイル「Invalid-Latex.tex」を確認して修復します。
まず第一に、サンプルファイルはTex構文を使用しているように見えますが、LaTexに必要な構造がないことに言及する価値があります。ご存知かもしれませんが、ラテックスファイルには、documentClass
コマンドと「Document」環境内のボディ、つまり \ begin {document}
と \ end {document}
の間にボディから始まる前文が必要です。
次に、C#コードサンプルを見てみましょう。
1// Create repair options.
2LaTeXRepairerOptions options = new LaTeXRepairerOptions();
3// Specify a file system working directory for the output.
4options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
5// Specify a file system working directory for the required input.
6// The directory containing packages may be located anywhere.
7options.RequiredInputDirectory = new InputFileSystemDirectory(Path.Combine(RunExamples.InputDirectory, "packages"));
8// Specify the callback class to externally guess packages required for undefined commands or environments.
9options.GuessPackageCallback = new PackageGuesser();
10// Run the repair process.
11new Features.LaTeXRepairer(Path.Combine(RunExamples.InputDirectory, "invalid-latex.tex"), options).Run();
通常のTexの仕事と同様に、最初に実行しようとしているプロセスのオプションを含むオブジェクトを作成します。それらのほとんどは、通常のTexジョブのオプションと同じです。実際、 inputworkingDirectoryは、入力ファイルを読み取るスペースです。ファイルシステムのメイン入力ファイルへのフルパスを提供し、メイン入力ファイルにカスタムファイルが含まれていないため、ここでは使用しません。次に、 outputWorkingDirectoryは、出力ファイルを書き込む必要があるスペースです。 rebulyinputDirectoryが割り当てられた場合、Asoppes.texライブラリに埋め込まれていないラテックスパッケージを保存できるスペースを指します。 推測PackageCallbackプロパティについては後で説明します。
オプションを割り当てた後、プロセスを実行するだけです!
それで、チェックと修復のプロセスはどのようなものですか?まず、APIは入力ファイルを検索してdocumentClass
の発生を検索します。失敗した場合、 \ documentclass {article}
をファイルの最初に挿入する必要があると想定しています。この事実は、修理レポートファイル( .log
)に反映されています。
次に、最初から調整された入力ファイルのスキャンを開始します。ラテックス形式で充電されたTEXエンジンは、ある時点でエラーを投げる可能性があり、「{document} `がこれまでに発見されていないことを示していますが、すでに発生するはずです。したがって、「{document}」を挿入する必要がある位置が定義され、レポートに反映されます。
エンジンがファイルをさらにスキャンすると、未定義のコマンドまたは環境が見つかる場合があります。 APIは、最も一般的なコマンドと環境の一部について、 埋め込まれた必須パッケージ(これらのパッケージが含まれるまで定義されていないコマンドと環境を定義するもの)で仮定を作成できます。ただし、 iguesspackagecallbackインターフェイスを実装することにより、そのような仮定を外部から行う方法があります。このようなクラスのインスタンスは、「buesspackagecallback」オプションに割り当てる必要があります。
fancehdr
パッケージに\ head
コマンドをマップするだけの非常に簡単な例があります。
1public class PackageGuesser : IGuessPackageCallback
2{
3 private Dictionary<string, string> _map = new Dictionary<string, string>();
4
5 public PackageGuesser()
6 {
7 _map.Add("lhead", "fancyhdr"); // Defines the mapping between the \lhead command and the fancyhdr package.
8 }
9
10 public string GuessPackage(string commandName, bool isEnvironment)
11 {
12 string packageName;
13 if (!isEnvironment)
14 {
15 _map.TryGetValue(commandName, out packageName);
16 return 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
コマンドに遭遇します。これは、「記事」ドキュメントクラスで定義されていないが、Book
ドキュメントクラスで定義されています。 APIはドキュメントクラスを調整して、固定ファイルの最終バージョンが「\ documentClass {book}」で始まるようになります。次に、エンジンは前述の\ lhead
コマンドを見つけ、 \ usepackage {fancyhdr}
をプリアンブルに挿入する必要があると判断します。後で発生する\ href
および \ increctgraphics
コマンドは、修理業者を挿入するようにします。これらの決定は、APIの内部マッピングに基づいています。繰り返しますが、そのような修正はすべてレポートファイルに記録されます。
最後に、ラテックスドキュメントの通常の終了が欠落しているため、エンジンは異常に終了します。これにより、修理業者は\ end {document}
をファイルの最後に追加し、レポートにこの事実を反映させます。
元のファイルの固定バージョンが構築されると、修理業者は最終チェックのために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 **Webアプリもチェックしてください。これは、[.NET APIのAspose.TeX]内に実装された機能に基づいて構築され、「Iguesspackagecalback」インターフェースのより高度な実装を伴います。