LaTeX 図レンダリング | Aspose.TeX for .NET

時々、LaTeX ファイルからコンテンツを抽出して、ページレイアウトとは無関係に別々にレンダリングされた 「図」 として扱いたいことがあります。たとえば、インターネット上の出版物のイラストにすることができます。これを当社の API で実現できます。対象フォーマットは PNG と SVG の 2 つがあります。LaTeX 数式レンダリング機能と同様です。また、LaTeX 図のレンダリングLaTeX 数式レンダリング の一般化であることにも留意してください。

LaTeX 図を 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 図オプション のインスタンスを作成します。ここで出力画像の解像度も同時に指定します。

次に、プレアンブルを指定します。LaTeX 図のレンダリングにはデフォルトのプレアンブルがないため、たとえば pict2e LaTeX パッケージを使用して描画したグラフィックをレンダリングする場合は、プレアンブルで指定する必要があります:

1\usepackage{pict2e}

次に、レンダラに出力を 300% に拡大するよう指示します。

次のオプションは背景色を定義します。数式レンダリングとは異なり、前景色は指定しません。なぜなら、色はすべて LaTeX コード側で制御されていると想定しているからです。実際、背景色も同様で、これは単なる便利なオプションです。

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

そして最後のオプション ShowTerminal は、ターミナル出力をコンソールに書き込むかどうかを切り替えることができます。

実際にレンダリングを行うメソッドは FigureRenderer.Render() です。これは図のサイズをポイント単位で返します。

画像を書き込むストリームはメソッドの第2引数として受け取ります。次にストリームを作成します。

最後に、FigureRenderer.Render() メソッド自体を呼び出し、オプションを第3引数として渡します。図の LaTeX コードは第1引数として渡されます。

例の最後の行は、図のレンダリング結果として 2 つの情報—図のサイズと簡易エラーレポート(エラーがある場合)—を出力します。

以下はレンダリング結果です。

LaTeX Figure rendering to PNG

これは LaTeX 図のレンダリング 機能の最も汎用的な使用例です。

LaTeX 図を SVG にレンダリングする方法

同様の方法で、LaTeX 図を 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            }

相違点は次のとおりです:

結果は以下の通りです:

LaTeX Figure rendering to SVG

Have any questions about Aspose.TeX?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.