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