ラテックスフィギュアレンダリング| .NET
ページレイアウトに接続せずに、個別にレンダリングされたピース、または *“figure”*として、LaTexファイルからコンテンツを抽出する場合があります。たとえば、インターネット上での出版物のイラストになる可能性があります。 APIで行うことができます。ターゲット形式については、PNGとSVGの2つのオプションがあります。ラテックス数学式レンダリング機能の場合と同様です。また、ラテックスフィギュアレンダリングは ラテックス数学式レンダリングの一般化であることも注目に値します。
ラテックスフィギュアをPNGにレンダリングする方法
このトピックに関連するAPI参照セクションは ここです。また、式レンダリングと同様に、例から始めます。ここにあります:
1 // Render LaTeX figure to PNG image
2
3 // Create rendering options setting the image resolution to 150 dpi.
4 PngFigureRendererOptions options = new PngFigureRendererOptions();
5 options.Resolution = 150;
6
7 // Specify the preamble.
8 options.Preamble = "\\usepackage{pict2e}";
9
10 // Specify the scaling factor 300%.
11 options.Scale = 3000;
12
13 // Specify the background color.
14 options.BackgroundColor = System.Drawing.Color.White;
15
16 // Specify the output stream for the log file.
17 options.LogStream = new System.IO.MemoryStream();
18
19 // Specify whether to show the terminal output on the console or not.
20 options.ShowTerminal = true;
21
22 // Create the output stream for the figure image.
23 using (System.IO.Stream stream = System.IO.File.Open(
24 System.IO.Path.Combine(OutputDir, "text-and-formula.png"), System.IO.FileMode.Create))
25 {
26 // Run rendering.
27 System.Drawing.SizeF size = new PngFigureRenderer().Render(@"\setlength{\unitlength}{0.8cm}
28\begin{picture}(6,5)
29\thicklines
30\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$}
31\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}$}
32\end{picture}", stream, options);
33
34 // Show other results.
35 System.Console.Out.WriteLine(options.ErrorReport);
36 System.Console.Out.WriteLine();
37 System.Console.Out.WriteLine($"Size: {size}"); // Dimensions of the resulting image.
38 }まず、 LaTexフィギュアオプションインスタンスを作成します。ここでは、出力画像解像度を同時に指定します。
次に、前文を指定します。ラテックスフィギュアレンダリングにはデフォルトのプリアンブルはありません。たとえば、 pict2eラテックスパッケージを使用してプロットされたグラフィックをレンダリングする場合は、preambleで指定する必要があります。
1\usepackage{pict2e}次に、レンダラーに出力を300%スケーリングするよう指示します。
次のオプションは、背景色を定義します。数学の式レンダリングとは異なり、色は完全にラテックスコードの制御下にあると仮定するため、前景の色を指定しません。実際、背景色もそうですので、これは単なる便利なオプションです。
例の次の行はあまり意味がありません。ログ出力をストリームに向けることができることを示しています。
また、最後のオプション「showterminal」を使用すると、ターミナル出力の書き込みをコンソールに切り替えることができます。
実際にレンダリングを実行する方法は figurerenderer.render()です。フィギュアのサイズをポイントで返します。
画像が記述されるストリームは、2番目の引数としてメソッドによって受け入れられます。次にストリームを作成します。
そして最後に、 figurerenderer.render()メソッド自体を3番目の引数として渡すオプションを呼び出します。図のラテックスコードは、最初の引数として渡されます。
この例の最後の行には、図のレンダリングの2つのアーティファクトが印刷されています。図のサイズと簡単なエラーレポート(エラーがある場合)。
レンダリングの結果です。
これは、LaTexフィギュアレンダリング機能の最も一般的なユースケースです。
ラテックスフィギュアをSVGにレンダリングする方法
ほぼ同じように、SVG形式でラテックスフィギュアをレンダリングできます。
1 // Render LaTeX figure to SVG image
2
3 // Create rendering options.
4 FigureRendererOptions options = new SvgFigureRendererOptions();
5
6 // Specify the preamble.
7 options.Preamble = "\\usepackage{pict2e}";
8
9 // Specify the scaling factor 300%.
10 options.Scale = 3000;
11
12 // Specify the background color.
13 options.BackgroundColor = System.Drawing.Color.White;
14
15 // Specify the output stream for the log file.
16 options.LogStream = new System.IO.MemoryStream();
17
18 // Specify whether to show the terminal output on the console or not.
19 options.ShowTerminal = true;
20
21 // Create the output stream for the figure image.
22 using (System.IO.Stream stream = System.IO.File.Open(
23 System.IO.Path.Combine(OutputDir, "text-and-formula.svg"), System.IO.FileMode.Create))
24 {
25 // Run rendering.
26 System.Drawing.SizeF size = new SvgFigureRenderer().Render(@"\setlength{\unitlength}{0.8cm}
27\begin{picture}(6,5)
28\thicklines
29\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$}
30\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}$}
31\end{picture}", stream, options);
32
33 // Show other results.
34 System.Console.Out.WriteLine(options.ErrorReport);
35 System.Console.Out.WriteLine();
36 System.Console.Out.WriteLine($"Size: {size}"); // Dimensions of the resulting image.
37 }違いは次のとおりです。
- pngfigurerenderereroptionsの代わりに svgfigurerenderereroptionsクラスを使用します。
- 解決策は指定しません。
- pngfigurerendererの代わりに svgfigurerendererクラスを使用します。
これが結果です:
