ラテックスフィギュアレンダリング| .NET

ページレイアウトに接続せずに、個別にレンダリングされたピース、または *“figure”*として、LaTexファイルからコンテンツを抽出する場合があります。たとえば、インターネット上での出版物のイラストになる可能性があります。 APIで行うことができます。ターゲット形式については、PNGとSVGの2つのオプションがあります。ラテックス数学式レンダリング機能の場合と同様です。また、ラテックスフィギュアレンダリングラテックス数学式レンダリングの一般化であることも注目に値します。

ラテックスフィギュアをPNGにレンダリングする方法

このトピックに関連するAPI参照セクションは ここです。また、式レンダリングと同様に、例から始めます。ここにあります:

 1// Create rendering options setting the image resolution to 150 dpi.
 2PngFigureRendererOptions options = new PngFigureRendererOptions();
 3options.Resolution = 150;
 4// Specify the preamble.
 5options.Preamble = "\\usepackage{pict2e}";
 6// Specify the scaling factor 300%.
 7options.Scale = 3000;
 8// Specify the background color.
 9options.BackgroundColor = System.Drawing.Color.White;
10// Specify the output stream for the log file.
11options.LogStream = new System.IO.MemoryStream();
12// Specify whether to show the terminal output on the console or not.
13options.ShowTerminal = true;
14
15// Create the output stream for the figure image.
16using (System.IO.Stream stream = System.IO.File.Open(
17    System.IO.Path.Combine(RunExamples.OutputDirectory, "text-and-formula.png"), System.IO.FileMode.Create))
18{
19    // Run rendering.
20    System.Drawing.SizeF size = new PngFigureRenderer().Render(@"\setlength{\unitlength}{0.8cm}
21\begin{picture}(6,5)
22\thicklines
23\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$}
24\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}$}
25\end{picture}", stream, options);
26    
27    // Show other results.
28    System.Console.Out.WriteLine(options.ErrorReport);
29    System.Console.Out.WriteLine();
30    System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
31}

まず、 LaTexフィギュアオプションインスタンスを作成します。ここでは、出力画像解像度を同時に指定します。

次に、前文を指定します。ラテックスフィギュアレンダリングにはデフォルトのプリアンブルはありません。たとえば、 pict2eラテックスパッケージを使用してプロットされたグラフィックをレンダリングする場合は、preambleで指定する必要があります。

1\usepackage{pict2e}

次に、レンダラーに出力を300%スケーリングするよう指示します。

次のオプションは、背景色を定義します。数学の式レンダリングとは異なり、色は完全にラテックスコードの制御下にあると仮定するため、前景の色を指定しません。実際、背景色もそうですので、これは単なる便利なオプションです。

例の次の行はあまり意味がありません。ログ出力をストリームに向けることができることを示しています。

また、最後のオプション「showterminal」を使用すると、ターミナル出力の書き込みをコンソールに切り替えることができます。

実際にレンダリングを実行する方法は figurerenderer.render()です。フィギュアのサイズをポイントで返します。

画像が記述されるストリームは、2番目の引数としてメソッドによって受け入れられます。次にストリームを作成します。

そして最後に、 figurerenderer.render()メソッド自体を3番目の引数として渡すオプションを呼び出します。図のラテックスコードは、最初の引数として渡されます。

この例の最後の行には、図のレンダリングの2つのアーティファクトが印刷されています。図のサイズと簡単なエラーレポート(エラーがある場合)。

レンダリングの結果です。

ラテックスフィギュアはPNGにレンダリングします

これは、LaTexフィギュアレンダリング機能の最も一般的なユースケースです。

ラテックスフィギュアをSVGにレンダリングする方法

ほぼ同じように、SVG形式でラテックスフィギュアをレンダリングできます。

 1// Create rendering options.
 2FigureRendererOptions options = new SvgFigureRendererOptions();
 3// Specify the preamble.
 4options.Preamble = "\\usepackage{pict2e}";
 5// Specify the scaling factor 300%.
 6options.Scale = 3000;
 7// Specify the background color.
 8options.BackgroundColor = System.Drawing.Color.White;
 9// Specify the output stream for the log file.
10options.LogStream = new System.IO.MemoryStream();
11// Specify whether to show the terminal output on the console or not.
12options.ShowTerminal = true;
13
14// Create the output stream for the figure image.
15using (System.IO.Stream stream = System.IO.File.Open(
16    System.IO.Path.Combine(RunExamples.OutputDirectory, "text-and-formula.svg"), System.IO.FileMode.Create))
17{
18    // Run rendering.
19    System.Drawing.SizeF size = new SvgFigureRenderer().Render(@"\setlength{\unitlength}{0.8cm}
20\begin{picture}(6,5)
21\thicklines
22\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$}
23\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}$}
24\end{picture}", stream, options);
25    
26    // Show other results.
27    System.Console.Out.WriteLine(options.ErrorReport);
28    System.Console.Out.WriteLine();
29    System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
30}

違いは次のとおりです。

これが結果です:

ラテックスフィギュアはSVGにレンダリングします

Have any questions about Aspose.TeX?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.