LaTeX 数式のレンダリング | Python via .NET
PNG に LaTeX 数式をレンダリングする
必要に応じて、 このトピックのAPIリファレンスセクションをご覧ください。実際、LaTeX数式レンダリング機能を理解するには、まず例を挙げるのが最も効果的です。以下に例を示します。
1from aspose.tex.features import *
2from aspose.pydrawing import Color
3from util import Util
4from io import BytesIO
5from os import path
6###############################################
7###### Class and Method declaration here ######
8###############################################
9
10# Create rendering options setting the image resolution to 150 dpi.
11options = PngMathRendererOptions()
12options.resolution = 150 # Specify the preamble.
13options.preamble = r"""\usepackage{amsmath}
14\usepackage{amsfonts}
15\usepackage{amssymb}
16\usepackage{color}"""
17# Specify the scaling factor 300%.
18options.scale = 3000
19# Specify the foreground color.
20options.text_color = Color.black
21# Specify the background color.
22options.background_color = Color.white
23# Specify the output stream for the log file.
24options.log_stream = BytesIO()
25# Specify whether to show the terminal output on the console or not.
26options.show_terminal = True
27
28# Create the output stream for the formula image.
29with open(path.join(Util.output_directory, "math-formula.png"), "wb") as stream:
30 # Run rendering.
31 size = PngMathRenderer().render(r"""\begin{equation*}
32e^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!}
33\end{equation*}""", stream, options)
34
35# Show other results.
36print(options.error_report)
37print()
38print(f"Size: {size.width}x{size.height}")詳細を見ていきましょう。まず、TeX/LaTeXタイプセッティングに似た レンダリングオプションオブジェクトを初期化します。このオブジェクトには、必要な出力画像解像度の指定が含まれています。
次に、プリアンブルを指定します。デフォルトのプリアンブルは次のとおりです。
1\usepackage{amsmath}
2\usepackage{amsfonts}
3\usepackage{amssymb}これは、標準的なLaTeXに比べて数式のサポートが強化されています。例えば、提供されているコード例に示すように、colorパッケージをインクルードすることで、数式内のハイライト表示をカスタマイズできます。
次に、レンダラーに出力を300%に拡大するように指示します。
次の2つのオプションは、前景色と背景色を決定します。数式の中でカスタムハイライトの影響を受けない部分は、TextColorカラーで表示されます。
例の次の行は、ログ出力を特定のストリームにリダイレクトするオプションがあることを示しています。
最後に、ShowTerminal オプションを使用すると、ターミナル出力をコンソールに表示するかどうかを制御できます。
実際のレンダリング処理は MathRenderer.render() メソッドによって実行され、数式のサイズをポイント単位で返します。
このメソッドは、画像を書き込むストリームを第2引数として受け取ります。その後、ストリームの作成に進みます。
最後に、MathRenderer.render() メソッドが呼び出され、オプションが3番目の引数として渡されます。数式の LaTeX コードは1番目の引数として提供されます。
例の最後の行には、数式レンダリング処理の2つの出力(数式のサイズと、エラーが発生した場合の簡潔なエラーレポート)が表示されます。
レンダリングの結果は次のとおりです。
これは、LaTeX 数式レンダリング 機能の最も一般的で幅広い応用例です。
また、 Aspose.TeX for .NET API の実装機能を活用した無料の Webアプリもお試しください。
SVG に LaTeX 数式をレンダリングする
同様に、同じ方法でLaTeXの数式をSVG形式に変換できます。
1from aspose.tex.features import *
2from aspose.pydrawing import Color
3from util import Util
4from io import BytesIO
5from os import path
6###############################################
7###### Class and Method declaration here ######
8###############################################
9
10# Create rendering options.
11options = SvgMathRendererOptions()
12# Specify the preamble.
13options.preamble = r"""\usepackage{amsmath}
14\usepackage{amsfonts}
15\usepackage{amssymb}
16\usepackage{color}"""
17# Specify the scaling factor 300%.
18options.scale = 3000
19# Specify the foreground color.
20options.text_color = Color.black
21# Specify the background color.
22options.background_color = Color.white
23# Specify the output stream for the log file.
24options.log_stream = BytesIO()
25# Specify whether to show the terminal output on the console or not.
26options.show_terminal = True
27
28# Create the output stream for the formula image.
29with open(path.join(Util.output_directory, "math-formula.svg"), "wb") as stream:
30 # Run rendering.
31 size = SvgMathRenderer().render(r"""\begin{equation*}
32e^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!}
33\end{equation*}""", stream, options)
34
35# Show other results.
36print(options.error_report)
37print()
38print(f"Size: {size.width}x{size.height}")違いは以下のとおりです。
- PngMathRendererOptions クラスの代わりに、 SvgMathRendererOptions クラスを使用します。
- 解像度は指定しません。
- PngMathRenderer クラスの代わりに、 SvgMathRenderer クラスを使用します。
結果は以下のとおりです。
Aspose.TeX for .NET API の実装機能を活用した無料の Web アプリ もお試しください。 < A href ="/ tex/ pyth
