ラテックス数学式レンダリング| Python
ラテックス数学式をPNGにレンダリングします
必要に応じて、このトピックの API参照セクションをご覧ください。実際、ラテックス数学式レンダリング機能を実証する最良の方法は、例から始めることです。ここにあります:
1# Create rendering options setting the image resolution to 150 dpi.
2options = PngMathRendererOptions()
3options.resolution = 150 # Specify the preamble.
4options.preamble = r"""\usepackage{amsmath}
5\usepackage{amsfonts}
6\usepackage{amssymb}
7\usepackage{color}"""
8# Specify the scaling factor 300%.
9options.scale = 3000
10# Specify the foreground color.
11options.text_color = Color.black
12# Specify the background color.
13options.background_color = Color.white
14# Specify the output stream for the log file.
15options.log_stream = BytesIO()
16# Specify whether to show the terminal output on the console or not.
17options.show_terminal = True
18
19# Create the output stream for the formula image.
20with open(path.join(Util.output_directory, "math-formula.png"), "wb") as stream:
21 # Run rendering.
22 size = PngMathRenderer().render(r"""\begin{equation*}
23e^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!}
24\end{equation*}""", stream, options)
25
26# Show other results.
27print(options.error_report)
28print()
29print(f"Size: {size.width}x{size.height}")
詳細について説明しましょう。まず、目的の出力画像解像度の仕様を含むTex/LaTexタイプセットと同様に、 レンダリングオプションオブジェクトを開始します。
次に、前文を指定します。デフォルトのプリアンブルは次のとおりです。
1\usepackage{amsmath}
2\usepackage{amsfonts}
3\usepackage{amssymb}
標準のラテックスと比較して、数式の強化されたサポートを提供します。たとえば、「Color」パッケージを含めて、指定されたコードの例で示されているように、式のハイライトをカスタマイズすることができます。
次に、レンダラーに出力を300%スケーリングするよう指示します。
次の2つのオプションは、前景と背景の色を決定します。カスタムハイライトの影響を受けない式の部分は、「TextColor」色で表示されます。
この例の次の行は、ログ出力を特定のストリームにリダイレクトするオプションがあるというイラストとして機能します。
最後に、「showterminal」オプションを使用すると、コンソールに端子出力を表示するかどうかを制御できます。
実際のレンダリングプロセスは、 MathRenderer.Render()メソッドによって実行され、ポイントの式のサイズを返します。
この方法は、画像が2番目の引数として記述されるストリームを受け入れます。その後、ストリームの作成に進みます。
そして最後に、 mathrenderer.render()
メソッドが呼び出され、オプションは3番目の引数として渡されます。式のラテックスコードは、最初の引数として提供されます。
この例の最後の行には、数式レンダリングプロセスの2つの出力が表示されます。エラーが発生した場合、式のサイズと簡潔なエラーレポートです。
レンダリングの結果です。
これは、ラテックス数学式レンダリング機能の最も一般的で広範なアプリケーションを表しています。
**無料 Webアプリを探索することもできます。
LaTex MATHフォーミュラをSVGにレンダリングします
同様に、同じアプローチを使用して、ラテックス数学の式をSVG形式に変換できます。
1# Create rendering options.
2options = SvgMathRendererOptions()
3# Specify the preamble.
4options.preamble = r"""\usepackage{amsmath}
5\usepackage{amsfonts}
6\usepackage{amssymb}
7\usepackage{color}"""
8# Specify the scaling factor 300%.
9options.scale = 3000
10# Specify the foreground color.
11options.text_color = Color.black
12# Specify the background color.
13options.background_color = Color.white
14# Specify the output stream for the log file.
15options.log_stream = BytesIO()
16# Specify whether to show the terminal output on the console or not.
17options.show_terminal = True
18
19# Create the output stream for the formula image.
20with open(path.join(Util.output_directory, "math-formula.svg"), "wb") as stream:
21 # Run rendering.
22 size = SvgMathRenderer().render(r"""\begin{equation*}
23e^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!}
24\end{equation*}""", stream, options)
25
26# Show other results.
27print(options.error_report)
28print()
29print(f"Size: {size.width}x{size.height}")
違いは次のとおりです。
- pngmathrendereroroptionsクラスを使用する代わりに、 svgmathrendereroptionsクラスを使用します。
- 解像度を指定しません。
- pngmathrendererクラスを使用する代わりに、 svgmathrendererクラスを使用します。
これが結果です:
**無料 Webアプリを探索することもできます。