ラテックスフィギュアレンダリング| Python用のAspose.TeX
特定の状況では、ページレイアウトに接続せずに、一般に *フィギュア *と呼ばれるスタンドアロンレンダリングピースとして、LaTexファイルから特定のコンテンツを抽出する必要がある場合があります。これは、たとえば、オンライン公開のイラストを作成するときに役立つ可能性があります。私たちのAPIを使用すると、これを達成できます。レンダリング数値のターゲット形式は、LaTex Math式レンダリング機能と同様に、PNGとSVGです。 ラテックスフィギュアレンダリングは、 LaTex MATH式レンダリングと比較して、より一般的な機能であることに注意してください。
ラテックスフィギュアをPNGにレンダリングします
必要に応じて、最初に このトピックのAPI参照セクションをご覧ください。フォーミュラレンダリングと同様に、例から始めます。ここにあります:
1# Create rendering options setting the image resolution to 150 dpi.
2options = PngFigureRendererOptions()
3options.resolution = 150 # Specify the preamble.
4options.preamble = r"\usepackage{pict2e}"
5# Specify the scaling factor 300%.
6options.scale = 3000
7# Specify the background color.
8options.background_color = Color.white
9# Specify the output stream for the log file.
10options.log_stream = BytesIO()
11# Specify whether to show the terminal output on the console or not.
12options.show_terminal = True
13
14# Create the output stream for the figure image.
15with open(path.join(Util.output_directory, "text-and-formula.png"), "wb") as stream:
16 # Run rendering.
17 size = PngFigureRenderer().render(r"""\setlength{\unitlength}{0.8cm}
18\begin{picture}(6,5)
19\thicklines
20\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$}
21\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}$}
22\end{picture}""", stream, options)
23
24# Show other results.
25print(options.error_report)
26print()
27print(f"Size: {size.width}x{size.height}")
まず、 LaTexフィギュアレンダラーオプションクラスのインスタンスを作成します。このクラスでは、出力画像解像度を同時に指定します。
次に、前文を指定する必要があります。ラテックス数学の式レンダリングとは異なり、ラテックスフィギュアレンダリングのデフォルトのプリアンブルはありません。したがって、たとえば、「PICT2E」ラテックスパッケージを使用して作成されたグラフィックをレンダリングする予定がある場合は、前文で指定する必要があります。
1\usepackage{pict2e}
次に、レンダラーに出力を300%スケーリングするよう指示します。
次のオプションは、背景色を決定します。数学の式レンダリングとは異なり、色はラテックスコードによって完全に制御されていると仮定するため、前景の色を指定する必要はありません。同様に、背景色はラテックスコードによっても制御されるため、このオプションは便利なために単純に提供されます。
この例の次の行は、特に意味がない場合があります。ログ出力を特定のストリームに向けるオプションがあることを実証することを意図しています。
最終的なオプション「Showterminal」では、端子出力をコンソールに表示するかどうかを制御できます。
図のレンダリングを担当する方法は figurerenderer.render()です。フィギュアのサイズをポイントで返します。
このメソッドは、画像が記述されるストリームである2番目の引数としてストリームを受け入れます。次の行でストリームを作成します。
最後に、 figurerenderer.render()
メソッド自体を呼び出し、3番目の引数としてオプションを渡します。図のラテックスコードは、最初の引数として提供されます。
この例の最後の数行には、図のレンダリングに関連する2つの情報が表示されます。図のサイズと簡単なエラーレポート(エラーがある場合)です。
レンダリングの結果です。
これは、ラテックスフィギュアレンダリング機能の最も一般的なユースケースを表します。
ラテックスフィギュアをSVGにレンダリングします
同様に、SVG形式でラテックスフィギュアをレンダリングすることもできます。
1# Create rendering options.
2options = SvgFigureRendererOptions()
3# Specify the preamble.
4options.preamble = r"\usepackage{pict2e}"
5# Specify the scaling factor 300%.
6options.scale = 3000
7# Specify the background color.
8options.background_color = Color.white
9# Specify the output stream for the log file.
10options.log_stream = BytesIO()
11# Specify whether to show the terminal output on the console or not.
12options.show_terminal = True
13
14# Create the output stream for the figure image.
15with open(path.join(Util.output_directory, "text-and-formula.svg"), "wb") as stream:
16 # Run rendering.
17 size = SvgFigureRenderer().render(r"""\setlength{\unitlength}{0.8cm}
18\begin{picture}(6,5)
19\thicklines
20\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$}
21\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}$}
22\end{picture}""", stream, options)
23
24# Show other results.
25print(options.error_report)
26print()
27print(f"Size: {size.width}x{size.height}")
違いは次のとおりです。
- pngfigurerenderereroptionsクラスを使用する代わりに、 svgfigurerenderereroptionsクラスを使用します。
- 解像度を指定する必要はありません。
- pngfigurerendererクラスを使用する代わりに、 svgfigurerendererクラスを使用します。
これが結果です: