LaTeX 数式のレンダリング | Python via .NET
PNG に LaTeX 数式をレンダリングする
必要に応じて、 このトピックのAPIリファレンスセクションをご覧ください。実際、LaTeX数式レンダリング機能を理解するには、まず例を挙げるのが最も効果的です。以下に例を示します。
ASPOSE_TEX_OUTPUT_DIR に出力ディレクトリを指定するか、既定の output フォルダーを使用してください。LaTeX コードはすべて例に含まれているため、外部入力ファイルは不要です。事前にライセンスを適用してください。
ライセンス.
1# Render a highlighted LaTeX math formula to PNG.
2import os
3from pathlib import Path
4from aspose.pydrawing import Color
5from aspose.tex.features import PngMathRenderer, PngMathRendererOptions
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 = PngMathRendererOptions()
13options.resolution = 150
14# Specify the preamble.
15options.preamble = r"""\usepackage{amsmath}
16\usepackage{amsfonts}
17\usepackage{amssymb}
18\usepackage{color}"""
19# Specify the scaling factor 300%.
20options.scale = 3000
21# Leave space around the rendered fragment.
22options.margin = 5
23# Specify the foreground color.
24options.text_color = Color.black
25# Specify the background color.
26options.background_color = Color.white
27# Specify whether to show the terminal output on the console or not.
28options.show_terminal = False
29
30# Create binary streams for the rendering log and formula image.
31with (output_dir / "math-formula.png.log").open("wb") as log_stream, \
32 (output_dir / "math-formula.png").open("wb") as stream:
33 options.log_stream = log_stream
34 # Run rendering.
35 size = PngMathRenderer().render(r"""\begin{equation*}
36e^x = x^{\color{red}0} + x^{\color{red}1} + \frac{x^{\color{red}2}}{2} + \frac{x^{\color{red}3}}{6} + \cdots = \sum_{n\geq 0} \frac{x^{\color{red}n}}{n!}
37\end{equation*}""", stream, options)
38
39# Show other results.
40print(options.error_report or "No rendering errors.")
41print()
42print(f"Size: {size.width}x{size.height}")詳細を見ていきましょう。まず、TeX/LaTeXタイプッティングに似た レンダリングオプションオブジェクトを初期化します。このオブジェクトには、必要な出力画像解像度の指定が含まれています。
次に、プリアンブルを指定します。デフォルトのプリアンブルは次のとおりです。
1\usepackage{amsmath}
2\usepackage{amsfonts}
3\usepackage{amssymb}これは、標準的なLaTeXに比べて数式のサポートが強化されています。例えば、提供されているコード例に示すように、colorパッケージをインクルードすることで、数式内のハイライト表示をカスタマイズできます。
scale = 3000 で 300% に拡大し、margin = 5 でフラグメントの周囲に余白を設けます。PNG の例では resolution = 150 DPI を使用します。
次の2つのオプションは、前景色と背景色を決定します。数式の中でカスタムハイライトの影響を受けない部分は、text_colorカラーで表示されます。
この例ではバイナリファイルストリームを log_stream に設定し、math-formula.png と同じ場所に math-formula.png.log を保存します。with 文が両方のストリームを自動的に閉じます。
show_terminal はコンソールへの端末出力を制御します。例では False に設定していますが、ログはファイルに書き込まれます。
実際のレンダリング処理は MathRenderer.render() メソッドによって実行され、数式のサイズをポイント単位で返します。
第2引数は画像用に開いたバイナリ出力ストリームです。第3引数はレンダリングオプションです。
最後に、MathRenderer.render() メソッドが呼び出され、オプションが3番目の引数として渡されます。数式の LaTeX コードは1番目の引数として提供されます。
最後の行ではエラーレポート、またはレポートが空なら No rendering errors. を表示し、その後に幅と高さをポイント単位で表示します。
レンダリングの結果は次のとおりです。
これは、LaTeX 数式レンダリング 機能の最も一般的で幅広い応用例です。
また、 Aspose.TeX for .NET API の実装機能を活用した無料の Webアプリもお試しください。
SVG に LaTeX 数式をレンダリングする
同様に、同じ方法でLaTeXの数式をSVG形式に変換できます。
1# Render a highlighted LaTeX math formula to SVG.
2import os
3from pathlib import Path
4from aspose.pydrawing import Color
5from aspose.tex.features import SvgMathRenderer, SvgMathRendererOptions
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 = SvgMathRendererOptions()
13# Specify the preamble.
14options.preamble = r"""\usepackage{amsmath}
15\usepackage{amsfonts}
16\usepackage{amssymb}
17\usepackage{color}"""
18# Specify the scaling factor 300%.
19options.scale = 3000
20# Leave space around the rendered fragment.
21options.margin = 5
22# Specify the foreground color.
23options.text_color = Color.black
24# Specify the background color.
25options.background_color = Color.white
26# Specify whether to show the terminal output on the console or not.
27options.show_terminal = False
28
29# Create binary streams for the rendering log and formula image.
30with (output_dir / "math-formula.svg.log").open("wb") as log_stream, \
31 (output_dir / "math-formula.svg").open("wb") as stream:
32 options.log_stream = log_stream
33 # Run rendering.
34 size = SvgMathRenderer().render(r"""\begin{equation*}
35e^x = x^{\color{red}0} + x^{\color{red}1} + \frac{x^{\color{red}2}}{2} + \frac{x^{\color{red}3}}{6} + \cdots = \sum_{n\geq 0} \frac{x^{\color{red}n}}{n!}
36\end{equation*}""", stream, options)
37
38# Show other results.
39print(options.error_report or "No rendering errors.")
40print()
41print(f"Size: {size.width}x{size.height}")この例では同じ出力ディレクトリに math-formula.svg とログ math-formula.svg.log を作成します。
違いは以下のとおりです。
- PngMathRendererOptions クラスの代わりに、 SvgMathRendererOptions クラスを使用します。
- 解像度は指定しません。
- PngMathRenderer クラスの代わりに、 SvgMathRenderer クラスを使用します。
結果は以下のとおりです。
Aspose.TeX for .NET API の実装機能を活用した無料の Web アプリ もお試しください。
