ラテックス数学式レンダリング| 。ネット
ラテックス数学をPNGにレンダリングする方法
このトピックに関連するAPI参照セクションは ここです。実際、ラテックス数学の式レンダリング機能を実証する最も簡単な方法は、例から始めることです。ここにあります:
1// Create rendering options setting the image resolution to 150 dpi.
2PngMathRendererOptions options = new PngMathRendererOptions();
3options.Resolution = 150;
4// Specify the preamble.
5options.Preamble = @"\usepackage{amsmath}
6\usepackage{amsfonts}
7\usepackage{amssymb}
8\usepackage{color}";
9// Specify the scaling factor 300%.
10options.Scale = 3000;
11// Specify the foreground color.
12options.TextColor = System.Drawing.Color.Black;
13// Specify the background color.
14options.BackgroundColor = System.Drawing.Color.White;
15// Specify the output stream for the log file.
16options.LogStream = new System.IO.MemoryStream();
17// Specify whether to show the terminal output on the console or not.
18options.ShowTerminal = true;
19
20// Create the output stream for the formula image.
21using (System.IO.Stream stream = System.IO.File.Open(
22 System.IO.Path.Combine(RunExamples.OutputDirectory, "math-formula.png"), System.IO.FileMode.Create))
23{
24 // Run rendering.
25 System.Drawing.SizeF size = new PngMathRenderer().Render(@"\begin{equation*}
26e^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!}
27\end{equation*}", stream, options);
28
29 // Show other results.
30 System.Console.Out.WriteLine(options.ErrorReport);
31 System.Console.Out.WriteLine();
32 System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
33}
詳細について説明しましょう。まず、Tex/LaTexタイプセットと同様に、 レンダリングオプションインスタンスを作成します。ここでは、出力画像解像度を同時に指定します。
次に、前文を指定します。デフォルトのプリアンブルは次のとおりです。
1\usepackage{amsmath}
2\usepackage{amsfonts}
3\usepackage{amssymb}
基本的なラテックスよりもわずかに高度な数学式サポートを提供します。たとえば、コードの例で示したように、式で独自のハイライトを使用する場合は、「Color」パッケージを追加できます。
次に、レンダラーに出力を300%スケーリングするよう指示します。
次の2つのオプションは、前景と背景の色を定義します。カスタムハイライトによって覆われていない(「色付き」)式のこれらの部分は、「TextColor」色で表示されます。
例の次の行はあまり意味がありません。ログ出力をストリームに向けることができることを示しています。
また、最後のオプション「showterminal」を使用すると、ターミナル出力の書き込みをコンソールに切り替えることができます。
実際にレンダリングを実行する方法は MathRenderer.Render()です。式のサイズをポイントで返します。
画像が記述されるストリームは、2番目の引数としてメソッドによって受け入れられます。次にストリームを作成します。
そして最後に、 mathrenderer.render()
メソッド自体を3番目の引数として渡します。式のラテックスコードは、最初の引数として渡されます。
この例の最後の行には、数学式の2つのアーティファクトが印刷されています - 式のサイズと簡単なエラーレポート(エラーがある場合)。
レンダリングの結果です。
これは、LaTex MATH式レンダリング機能の最も一般的なユースケースです。
.NET APIのAspose.TeX内に実装されている機能に基づいて構築された無料の Webアプリをチェックアウトすることもできます。
ラテックス数学をSVGにレンダリングする方法
ほぼ同じように、LaTex MathフォーミュラをSVG形式にレンダリングできます。
1// Create rendering options.
2MathRendererOptions options = new SvgMathRendererOptions();
3// Specify the preamble.
4options.Preamble = @"\usepackage{amsmath}
5\usepackage{amsfonts}
6\usepackage{amssymb}
7\usepackage{color}";
8// Specify the scaling factor 300%.
9options.Scale = 3000;
10// Specify the foreground color.
11options.TextColor = System.Drawing.Color.Black;
12// Specify the background color.
13options.BackgroundColor = System.Drawing.Color.White;
14// Specify the output stream for the log file.
15options.LogStream = new System.IO.MemoryStream();
16// Specify whether to show the terminal output on the console or not.
17options.ShowTerminal = true;
18
19// Create the output stream for the formula image.
20using (System.IO.Stream stream = System.IO.File.Open(
21 System.IO.Path.Combine(RunExamples.OutputDirectory, "math-formula.svg"), System.IO.FileMode.Create))
22{
23 // Run rendering.
24 System.Drawing.SizeF size = new SvgMathRenderer().Render(@"\begin{equation*}
25e^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!}
26\end{equation*}", stream, options);
27
28 // Show other results.
29 System.Console.Out.WriteLine(options.ErrorReport);
30 System.Console.Out.WriteLine();
31 System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
32}
違いは次のとおりです。
- pngmathrendereroptionsの代わりに svgmathrendereroptionsクラスを使用します。
- 解像度を指定しません。
- pngmathrendererの代わりに svgmathrendererクラスを使用します。
これが結果です:
Aspose.TeX for .NET API に実装された機能に基づいて構築された無料の Web アプリ もぜひご確認ください。