ラテックスフィギュアレンダリング| Java用のAspose.TeX

出力ドキュメントページのその場所に関係なく、ラテックスファイルから個別にレンダリングされたピース、または *“figure”*として、いくつかのコンテンツ(図面、書き込み、プロットなど)を抽出することができます。たとえば、インターネット上での出版物のイラストが当てはまります。私たちのAPIはタスクを支援します。 PNGとSVGの2つの使用可能なターゲット形式があります。ラテックス数学の式レンダリングのように。また、ラテックスフィギュアレンダリングは[ラテックス数学式レンダリング]の一般化であることに注意する必要があります(1)。

ラテックスフィギュアをPNGにレンダリングする方法

また、式レンダリングと同様に、例から始めます。ここにあります:

 1// Create rendering options setting the image resolution to 150 dpi.
 2PngFigureRendererOptions options = new PngFigureRendererOptions();
 3options.setResolution(150);
 4// Specify the preamble.
 5options.setPreamble("\\usepackage{pict2e}");
 6// Specify the scaling factor 300%.
 7options.setScale(3000);
 8// Specify the background color.
 9options.setBackgroundColor(Color.WHITE);
10// Specify the output stream for the log file.
11options.setLogStream(new ByteArrayOutputStream());
12// Specify whether to show the terminal output on the console or not.
13options.showTerminal(true);
14
15// Create the output stream for the figure image.
16final OutputStream stream = new FileOutputStream(Utils.getOutputDirectory() + "text-and-formula.png");
17try {
18    // Run rendering.
19    com.aspose.tex.Size2D size = new PngFigureRenderer().render("\\setlength{\\unitlength}{0.8cm}\r\n" +
20    "\\begin{picture}(6,5)\r\n" +
21    "\\thicklines\r\n" +
22    "\\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$}\r\n" +
23    "\\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}$}\r\n" +
24    "\\end{picture}", stream, options);
25    
26    // Show other results.
27    System.out.println(options.getErrorReport());
28    System.out.println();
29    System.out.println("Size: " + size.getWidth() + "x" + size.getHeight()); // Dimensions of the resulting image.
30} finally {
31    if (stream != null)
32        stream.close();
33}

まず、 レンダリングオプションインスタンスを作成します。ここでは、出力画像解像度を指定します。

次に、前文を指定します。ラテックスフィギュアレンダリングにはデフォルトのプリアンブルはありません。たとえば、 pict2eラテックスパッケージを使用してプロットされたグラフィックをレンダリングする必要がある場合は、preambleで指定する必要があります。

1\usepackage{pict2e}

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

次のオプションは、背景色を定義します。数学の式レンダリングとは異なり、色は完全にラテックスコードの制御下にあると仮定するため、前景の色を指定しません。実際、背景色もそうですので、これは単なる便利なオプションです。

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

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

実際にレンダリングを実行する方法は figurerenderer.render()です。フィギュアのサイズをポイントで返します。

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

そして最後に、 figurerenderer.render()メソッド自体を3番目の引数として渡すオプションを呼び出します。図のラテックスコードは、最初の引数として渡されます。

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

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

ラテックスフィギュアはPNGにレンダリングします

これは、LaTexフィギュアレンダリング機能の最も一般的なユースケースです。

ラテックスフィギュアをSVGにレンダリングする方法

ほぼ同じように、ラテックスフィギュアをSVG形式にレンダリングできます。

 1// Create rendering options.
 2SvgFigureRendererOptions options = new SvgFigureRendererOptions();
 3// Specify the preamble.
 4options.setPreamble("\\usepackage{pict2e}");
 5// Specify the scaling factor 300%.
 6options.setScale(3000);
 7// Specify the background color.
 8options.setBackgroundColor(Color.WHITE);
 9// Specify the output stream for the log file.
10options.setLogStream(new ByteArrayOutputStream());
11// Specify whether to show the terminal output on the console or not.
12options.showTerminal(true);
13
14// Create the output stream for the figure image.
15final OutputStream stream = new FileOutputStream(Utils.getOutputDirectory() + "text-and-formula.svg");
16try {
17    // Run rendering.
18    com.aspose.tex.Size2D size = new SvgFigureRenderer().render("\\setlength{\\unitlength}{0.8cm}\r\n" +
19    "\\begin{picture}(6,5)\r\n" +
20    "\\thicklines\r\n" +
21    "\\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$}\r\n" +
22    "\\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}$}\r\n" +
23    "\\end{picture}", 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}

違いは次のとおりです。

これが結果です:

ラテックスフィギュアはSVGにレンダリングします

Have any questions about Aspose.TeX?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.