LaTeX 数式レンダリング | Aspose.TeX for Java
LaTeX 数式を PNG にレンダリングする方法
実際、LaTeX 数式 レンダリング機能を示す最も簡単な方法はサンプルから始めることです。こちらがその例です:
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 の組版と同様に rendering options インスタンスを作成します。ここでは同時に出力画像の解像度も指定しています。
次に、プリアンブルを指定します。デフォルトのプリアンブルは次のとおりです:
1\usepackage{amsmath}
2\usepackage{amsfonts}
3\usepackage{amssymb}これは基本的な LaTeX より多少高度な数式サポートを提供します。例えば、独自のハイライトを数式内で使用したい場合は color パッケージを追加できます(コード例に示しています)。
続いて、レンダラに出力を 300% に拡大するよう指示します。
次の 2 つのオプションは前景色と背景色を定義します。カスタムハイライトで「色付け」されていない数式の部分は TextColor の色で表示されます。
例の次の行はあまり意味がありませんが、ログ出力を任意のストリームにリダイレクトできることを示しています。
最後のオプション ShowTerminal はターミナル出力をコンソールに書き込むかどうかを切り替えます。
実際にレンダリングを行うメソッドは MathRenderer.render() です。式のサイズ(ポイント単位)を返します。
画像を書き込むストリームはメソッドの第 2 引数として受け取ります。次にストリームを作成します。
そして最後に、MathRenderer.render() メソッド自体を呼び出し、オプションを第 3 引数として渡します。式の LaTeX コードは第 1 引数として渡されます。
例の最後の行は、数式レンダリングの成果物である式のサイズと簡易エラーレポート(エラーがあった場合)を出力します。
以下がレンダリング結果です。
これは LaTeX 数式 レンダリングの最も一般的な使用例です。
無料の web app もご確認ください。この機能は Aspose.TeX for .NET API で実装されています。Java バージョンのページは こちら。
LaTeX 数式を SVG にレンダリングする方法
ほぼ同様の手順で、LaTeX 数式を 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}主な違いは以下の通りです:
- SvgMathRendererOptions クラスを使用し、 PngMathRendererOptions の代わりに使用します。
- 解像度を指定しません。
- SvgMathRenderer クラスを使用し、 PngMathRenderer の代わりに使用します。
以下が結果です:
無料の web app もご確認ください。この機能は Aspose.TeX for .NET API で実装されています。
