その他のTEX変換出力形式| .NET経由でPython用のAspose.TeX

現在、LaTex以外の形式で記述されたTEXファイルを変換する必要がある可能性は低いです。ただし、何らかの理由でTex LanguageまたはTexの内部を勉強している場合、それはまだ可能です。いずれにせよ、pythonのAspose.TeXを使用すると、plane tex形式で記述されたファイルを変換できます。また、これらの形式で設計されたカスタム形式と型文書を作成することもできます。

まず、カスタム形式を作成します。

カスタム形式の作成

フォーマットファイルは TeX エンジンの内部状態のバイナリ表現です。まず ライセンスを適用します。customtex.tex を ASPOSE_TEX_INPUT_DIR(既定値:input)の CustomTeXFormat サブフォルダーに配置します。ASPOSE_TEX_OUTPUT_DIR は出力フォルダー(既定値:output)を指定し、最初の例はそこに customtex.fmt を生成します。

 1# Create a reusable custom TeX format.
 2import os
 3from pathlib import Path
 4from os import path
 5from io import BytesIO
 6from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 7from aspose.tex.io import InputFileSystemDirectory, InputZipDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 8from aspose.tex.resourceproviders import FormatProvider
 9from aspose.tex.presentation.xps import XpsDevice
10
11output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
12output_dir.mkdir(parents=True, exist_ok=True)
13input_dir = Path(os.environ.get("ASPOSE_TEX_INPUT_DIR", "input")).resolve() / "CustomTeXFormat"
14# Create TeX engine options for no format upon ObjectTeX engine extension.
15options = TeXOptions.console_app_options(TeXConfig.object_ini_tex)
16options.interaction = Interaction.NONSTOP_MODE
17options.terminal_out = OutputMemoryTerminal()
18# Specify a file system working directory for the input.
19options.input_working_directory = InputFileSystemDirectory(str(input_dir))
20# Specify a file system working directory for the output.
21options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
22
23# Run format creation.
24TeXJob.create_format("customtex", options)
25
26# For further output to look fine.
27options.terminal_out.writer.write_line()

ご覧のとおり、コードはTEXファイルを変換するためのコードに似ています。ただし、いくつかの区別があります。

まず、この場合、 texconfig.object_ini_texジョブ構成を利用します。この構成により、エンジンの状態が「バージン」であることが保証されます。つまり、内部パラメーターにはデフォルト値があり、制御シーケンスのセットがプリミティブのセットに合わせます。この例では、一連のプリミティブが展開されます Aspose.TeXおよびObject Texの記事。

それに続いて、通常どおり、入力および出力作業ディレクトリのセットアップを進めます。入力作業ディレクトリには、メイン形式のソースファイルとそのすべての依存関係を含める必要があります。

2番目の大きな違いは、ジョブを実行する方法です。この場合、static create_format()メソッドを使用します。これは、オプションと併せて、メインソースファイルの名前が形式名と同一であることを要求します。

カスタム形式で TeX ファイルを組版する

次の例は単独で実行できるように、まず customtex.fmt を作成します。その後、フォーマットを読み込み、バイナリストリームのテキストを組版して typeset-with-custom-format.xps を生成します。

 1# Typeset text using a custom TeX format.
 2import os
 3from pathlib import Path
 4from os import path
 5from io import BytesIO
 6from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 7from aspose.tex.io import InputFileSystemDirectory, InputZipDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 8from aspose.tex.resourceproviders import FormatProvider
 9from aspose.tex.presentation.xps import XpsDevice
10
11output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
12output_dir.mkdir(parents=True, exist_ok=True)
13input_dir = Path(os.environ.get("ASPOSE_TEX_INPUT_DIR", "input")).resolve() / "CustomTeXFormat"
14
15format_options = TeXOptions.console_app_options(TeXConfig.object_ini_tex)
16format_options.input_working_directory = InputFileSystemDirectory(str(input_dir))
17format_options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
18format_options.interaction = Interaction.NONSTOP_MODE
19format_options.terminal_out = OutputMemoryTerminal()
20TeXJob.create_format("customtex", format_options)
21# Create the format provider using the file system input working directory.
22# We use the project output directory as our custom format file is supposed to be located there.
23with FormatProvider(InputFileSystemDirectory(str(output_dir)), "customtex") as format_provider:
24    # Create conversion options for a custom format upon ObjectTeX engine extension.
25    options = TeXOptions.console_app_options(TeXConfig.object_tex(format_provider))
26    options.interaction = Interaction.NONSTOP_MODE
27    options.terminal_out = OutputMemoryTerminal()
28    options.job_name = "typeset-with-custom-format"
29    # Specify the input working directory. This is not required here as we are providing the main input as a stream.
30    # But it is required when the main input has dependencies (e.g. images).
31    options.input_working_directory = InputFileSystemDirectory(str(input_dir))
32    # Specify a file system working directory for the output.
33    options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
34
35    # Run the job.
36    TeXJob(BytesIO("Congratulations! You have successfully typeset this text with your own TeX format!\\end".encode('ascii')),
37           XpsDevice(), options).run()
38
39    # For further output to look fine.
40    options.terminal_out.writer.write_line()

形式を指定するには、 formatproviderクラスのインスタンスを作成する必要があります。 Options Constructorでは、 texconfig.object_tex()構成を使用します。これは、フォーマットプロバイダーが引数として必要で、エンジンの「処女」状態の上に形式をロードします。

残りのコードは、この ガイドで前述した機能を利用しているため、おなじみのものでなければなりません。

Plain TeX 形式で TeX ファイルを組版する

上記のコードからフォーマットプロバイダーを削除すると、エンジンはデフォルト形式をロードします。これは オブジェクトTexである4番目の意味でです。したがって、Plain Tex形式でTexファイルが記述されている場合は、このアプローチを使用してサポートされているターゲット形式に変換できます。