ラテックス数学式レンダリング| .NET

ラテックス数学をPNGにレンダリングする方法

このトピックに関連するAPI参照セクションは ここです。実際、ラテックス数学の式レンダリング機能を実証する最も簡単な方法は、例から始めることです。ここにあります:

 1            // Render LaTeX math formula to PNG image
 2
 3            // Create rendering options setting the image resolution to 150 dpi.
 4            PngMathRendererOptions options = new PngMathRendererOptions();
 5            options.Resolution = 150;
 6            
 7            // Specify the preamble.
 8            options.Preamble = @"\usepackage{amsmath}
 9            \usepackage{amsfonts}
10            \usepackage{amssymb}
11            \usepackage{color}";
12            
13            // Specify the scaling factor 300%.
14            options.Scale = 3000;
15            
16            // Specify the foreground color.
17            options.TextColor = System.Drawing.Color.Black;
18            
19            // Specify the background color.
20            options.BackgroundColor = System.Drawing.Color.White;
21            
22            // Specify the output stream for the log file.
23            options.LogStream = new System.IO.MemoryStream();
24            
25            // Specify whether to show the terminal output on the console or not.
26            options.ShowTerminal = true;
27
28            // Create the output stream for the formula image.
29            using (System.IO.Stream stream = System.IO.File.Open(
30                System.IO.Path.Combine(OutputDir, "math-formula.png"), System.IO.FileMode.Create))
31            {
32                // Run rendering.
33                System.Drawing.SizeF size = new PngMathRenderer().Render(@"\begin{equation*}
34e^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!}
35\end{equation*}", stream, options);
36
37                // Show other results.
38                System.Console.Out.WriteLine(options.ErrorReport);
39                System.Console.Out.WriteLine();
40                System.Console.Out.WriteLine($"Size: {size}"); // Dimensions of the resulting image.
41            }

詳細について説明しましょう。まず、Tex/LaTexタイプセットと同様に、 レンダリングオプションインスタンスを作成します。ここでは、出力画像解像度を同時に指定します。

次に、前文を指定します。デフォルトのプリアンブルは次のとおりです。

1\usepackage{amsmath}
2\usepackage{amsfonts}
3\usepackage{amssymb}

基本的なラテックスよりもわずかに高度な数学式サポートを提供します。たとえば、コードの例で示したように、式で独自のハイライトを使用する場合は、「Color」パッケージを追加できます。

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

次の2つのオプションは、前景と背景の色を定義します。カスタムハイライトによって覆われていない(「色付き」)式のこれらの部分は、「TextColor」色で表示されます。

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

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

実際にレンダリングを実行する方法は MathRenderer.Render()です。式のサイズをポイントで返します。

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

そして最後に、 mathrenderer.render()メソッド自体を3番目の引数として渡します。式のラテックスコードは、最初の引数として渡されます。

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

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

ラテックス数学式はPNGにレンダリングします

これは、LaTex MATH式レンダリング機能の最も一般的なユースケースです。

.NET APIのAspose.TeX内に実装されている機能に基づいて構築された無料の Webアプリをチェックアウトすることもできます。

ラテックス数学をSVGにレンダリングする方法

ほぼ同じように、LaTex MathフォーミュラをSVG形式にレンダリングできます。

 1            // Render LaTeX math formula to SVG image
 2
 3            // Create rendering options.
 4            MathRendererOptions options = new SvgMathRendererOptions();
 5            
 6            // Specify the preamble.
 7            options.Preamble = @"\usepackage{amsmath}
 8            \usepackage{amsfonts}
 9            \usepackage{amssymb}
10            \usepackage{color}";
11            
12            // Specify the scaling factor 300%.
13            options.Scale = 3000;
14            
15            // Specify the foreground color.
16            options.TextColor = System.Drawing.Color.Black;
17            
18            // Specify the background color.
19            options.BackgroundColor = System.Drawing.Color.White;
20            
21            // Specify the output stream for the log file.
22            options.LogStream = new System.IO.MemoryStream();
23            
24            // Specify whether to show the terminal output on the console or not.
25            options.ShowTerminal = true;
26
27            // Create the output stream for the formula image.
28            using (System.IO.Stream stream = System.IO.File.Open(
29                System.IO.Path.Combine(OutputDir, "math-formula.svg"), System.IO.FileMode.Create))
30            {
31                // Run rendering.
32                System.Drawing.SizeF size = new SvgMathRenderer().Render(@"\begin{equation*}
33e^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!}
34\end{equation*}", stream, options);
35
36                // Show other results.
37                System.Console.Out.WriteLine(options.ErrorReport);
38                System.Console.Out.WriteLine();
39                System.Console.Out.WriteLine($"Size: {size}"); // Dimensions of the resulting image.
40            }

違いは次のとおりです。

これが結果です:

LaTeX 数式の SVG レンダリング

Aspose.TeX for .NET API に実装された機能に基づいて構築された無料の Web アプリ もぜひご確認ください。

Have any questions about Aspose.TeX?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.