LaTeX 図のレンダリング | Python via .NET
特定の状況では、LaTeX ファイルから特定のコンテンツを単独のレンダリングされた要素(一般に 図 と呼ばれます)として抽出する必要があるかもしれません。ページレイアウトとは無関係です。たとえばオンライン出版物のイラストを作成する際に役立ちます。当社の API を使用すればこれが可能です。図のレンダリング対象フォーマットは PNG と SVG で、LaTeX 数式レンダリング機能と同様です。LaTeX 図のレンダリング は、 LaTeX 数式レンダリング と比較して、より汎用的な機能であることに注意してください。
LaTeX 図を PNG にレンダリングする
必要に応じて、まずこのトピックの API リファレンス セクション をご覧ください。数式レンダリングと同様に、例から始めます。以下です:
ASPOSE_TEX_OUTPUT_DIR に出力ディレクトリを指定するか、既定の output フォルダーを使用してください。LaTeX コードはすべて例に含まれているため、外部入力ファイルは不要です。事前にライセンスを適用してください。
ライセンス.
1# Render a LaTeX figure with a triangle and formulas to PNG.
2import os
3from pathlib import Path
4from aspose.pydrawing import Color
5from aspose.tex.features import PngFigureRenderer, PngFigureRendererOptions
6
7# Set an output directory, or use the default output folder.
8output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
9output_dir.mkdir(parents=True, exist_ok=True)
10
11# Create rendering options setting the image resolution to 150 dpi.
12options = PngFigureRendererOptions()
13options.resolution = 150
14# Specify the preamble.
15options.preamble = r"\usepackage{pict2e}"
16# Specify the scaling factor 300%.
17options.scale = 3000
18# Leave space around the rendered fragment.
19options.margin = 5
20# Specify the background color.
21options.background_color = Color.white
22# Specify whether to show the terminal output on the console or not.
23options.show_terminal = False
24
25# Create binary streams for the rendering log and figure image.
26with (output_dir / "text-and-formula.png.log").open("wb") as log_stream, \
27 (output_dir / "text-and-formula.png").open("wb") as stream:
28 options.log_stream = log_stream
29 # Run rendering.
30 size = PngFigureRenderer().render(r"""\setlength{\unitlength}{0.8cm}
31\begin{picture}(6,5)
32\thicklines
33\put(1,0.5){\line(2,1){3}} \put(4,2){\line(-2,1){2}} \put(2,3){\line(-2,-5){1}} \put(0.7,0.3){$A$} \put(4.05,1.9){$B$} \put(1.7,2.95){$C$}
34\put(3.1,2.5){$a$} \put(1.3,1.7){$b$} \put(2.5,1.05){$c$} \put(0.3,4){$F=\sqrt{s(s-a)(s-b)(s-c)}$} \put(3.5,0.4){$\displaystyle s:=\frac{a+b+c}{2}$}
35\end{picture}""", stream, options)
36
37# Show other results.
38print(options.error_report or "No rendering errors.")
39print()
40print(f"Size: {size.width}x{size.height}")最初に、 LaTeX 図レンダラーオプション クラスのインスタンスを作成し、同時に出力画像の解像度を指定します。
次に、プリアンブルを指定する必要があります。LaTeX 数式レンダリングとは異なり、LaTeX 図のレンダリングにはデフォルトのプリアンブルがありません。そのため、たとえば pict2e パッケージで作成されたグラフィックをレンダリングする場合は、プリアンブルにそれを指定する必要があります:
1\usepackage{pict2e}scale = 3000 で 300% に拡大し、margin = 5 でフラグメントの周囲に余白を設けます。PNG の例では resolution = 150 DPI を使用します。
次のオプションは背景色を決定します。数式レンダリングとは異なり、前景色を指定する必要はありません。色はすべて LaTeX コードで制御されていると想定しています。同様に背景色も LaTeX コードで制御されるため、このオプションは利便性のために用意されています。
この例ではバイナリファイルストリームを log_stream に設定し、text-and-formula.png と同じ場所に text-and-formula.png.log を保存します。with 文が両方のストリームを自動的に閉じます。
show_terminal はコンソールへの端末出力を制御します。例では False に設定していますが、ログはファイルに書き込まれます。
図のレンダリングを担当するメソッドは FigureRenderer.render() です。ポイント単位で図のサイズを返します。
第2引数は画像用に開いたバイナリ出力ストリームです。第3引数はレンダリングオプションです。
最後に、FigureRenderer.render() メソッド自体を呼び出し、オプションを第3引数として渡します。図の LaTeX コードは第1引数として提供されます。
最後の行ではエラーレポート、またはレポートが空なら No rendering errors. を表示し、その後に幅と高さをポイント単位で表示します。
以下はレンダリング結果です。
これは LaTeX 図のレンダリング 機能の最も一般的な使用例を表しています。
LaTeX 図を SVG にレンダリングする
同様に、LaTeX 図を SVG 形式でもレンダリングできます。
1# Render a LaTeX figure with a triangle and formulas to SVG.
2import os
3from pathlib import Path
4from aspose.pydrawing import Color
5from aspose.tex.features import SvgFigureRenderer, SvgFigureRendererOptions
6
7# Set an output directory, or use the default output folder.
8output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
9output_dir.mkdir(parents=True, exist_ok=True)
10
11# Create rendering options.
12options = SvgFigureRendererOptions()
13# Specify the preamble.
14options.preamble = r"\usepackage{pict2e}"
15# Specify the scaling factor 300%.
16options.scale = 3000
17# Leave space around the rendered fragment.
18options.margin = 5
19# Specify the background color.
20options.background_color = Color.white
21# Specify whether to show the terminal output on the console or not.
22options.show_terminal = False
23
24# Create binary streams for the rendering log and figure image.
25with (output_dir / "text-and-formula.svg.log").open("wb") as log_stream, \
26 (output_dir / "text-and-formula.svg").open("wb") as stream:
27 options.log_stream = log_stream
28 # Run rendering.
29 size = SvgFigureRenderer().render(r"""\setlength{\unitlength}{0.8cm}
30\begin{picture}(6,5)
31\thicklines
32\put(1,0.5){\line(2,1){3}} \put(4,2){\line(-2,1){2}} \put(2,3){\line(-2,-5){1}} \put(0.7,0.3){$A$} \put(4.05,1.9){$B$} \put(1.7,2.95){$C$}
33\put(3.1,2.5){$a$} \put(1.3,1.7){$b$} \put(2.5,1.05){$c$} \put(0.3,4){$F=\sqrt{s(s-a)(s-b)(s-c)}$} \put(3.5,0.4){$\displaystyle s:=\frac{a+b+c}{2}$}
34\end{picture}""", stream, options)
35
36# Show other results.
37print(options.error_report or "No rendering errors.")
38print()
39print(f"Size: {size.width}x{size.height}")この例では同じ出力ディレクトリに text-and-formula.svg とログ text-and-formula.svg.log を作成します。
違いは次のとおりです:
- PngFigureRendererOptions クラスの代わりに、 SvgFigureRendererOptions クラスを使用します。
- 解像度を指定する必要はありません。
- PngFigureRenderer クラスの代わりに、 SvgFigureRenderer クラスを使用します。
結果は以下です:
