Other ways to provide main TeX input file | Java

Providing the main input file to the TeX engine as a stream

Up to this point, we only knew how to pass the main input file to the TeX engine as a file name, whether fully specified or in short form, whether with an extension or without it. But there’s another constructor of the TeXJob class, which accepts a stream as the first argument. This is convenient if for some reason we have the main input file in a form other than a file on the disk file system. Here’s how we can deal with it:

Set outputDirectory to an existing output folder. Import the Aspose.TeX classes used in the snippets, com.aspose.tex.rendering.XpsDevice, com.aspose.tex.rendering.XpsSaveOptions and, for stream input, java.io.ByteArrayInputStream and java.nio.charset.StandardCharsets. The calling method must handle or declare exceptions. Apply a license as described in the licensing guide to avoid evaluation restrictions. The stream example contains a complete LaTeX document, so it needs no input working directory. Streams and devices remain open until the job finishes and are closed or disposed afterward.

 1// Typeset a complete LaTeX document directly from a memory stream.
 2// outputDirectory is an existing output folder.
 3TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
 4OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
 5options.setOutputWorkingDirectory(output);
 6options.setSaveOptions(new XpsSaveOptions());
 7options.setInteraction(Interaction.NonstopMode);
 8options.setTerminalOut(new OutputMemoryTerminal());
 9XpsDevice device = new XpsDevice();
10TeXJobResult result;
11try (ByteArrayInputStream input = new ByteArrayInputStream(
12        "\\documentclass{article} \\begin{document} Hello, World! \\end{document}"
13                .getBytes(StandardCharsets.US_ASCII))) {
14    // Without a specified job name, stream input uses texput.
15    result = new TeXJob(input, device, options).run();
16} finally {
17    device.dispose();
18    output.close();
19}

When stream input has no explicitly assigned job name, the engine uses the default name texput. This example creates texput.xps and texput.log. Set options.setJobName(...) to choose another job name. See here and here for details about job names.

Entering the main TeX input file from the terminal

There’s yet another constructor of the TeXJob class, which doesn’t allow us to specify the input at all. So what is such a TeX job going to process, you may wonder? This time we will need an input terminal. The TeX engine will ask us to enter the file name from the terminal.

Set inputDirectory to a folder containing a complete LaTeX document named hello-world.ltx, and outputDirectory to an existing output folder. Run this example in a console, enter hello-world.ltx at the ** prompt and press Enter. It creates hello-world.xps and writes terminal output to the console. Both examples use NonstopMode for processing, but the terminal constructor still needs the initial filename. For unattended execution, provide an IInputTerminal implementation or use file/stream input.

Here is the code:

 1// Let the TeX engine request the main LaTeX filename from the console.
 2// inputDirectory contains hello-world.ltx; outputDirectory is an existing output folder.
 3TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
 4InputFileSystemDirectory input = new InputFileSystemDirectory(inputDirectory);
 5OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
 6options.setInputWorkingDirectory(input);
 7options.setOutputWorkingDirectory(output);
 8options.setSaveOptions(new XpsSaveOptions());
 9options.setInteraction(Interaction.NonstopMode);
10options.setTerminalIn(new InputConsoleTerminal());
11options.setTerminalOut(new OutputConsoleTerminal());
12XpsDevice device = new XpsDevice();
13TeXJobResult result;
14try {
15    // At the ** prompt, enter hello-world.ltx and press Enter.
16    result = new TeXJob(device, options).run();
17} finally {
18    device.dispose();
19    output.close();
20    input.close();
21}

As soon as it is run, the engine sticks, waiting for us to enter the file name (with or without a path, with or without an extension):

Terminal Window

And this is how it would look on the output terminal which is the console:

 1This is ObjectTeX, Version 3.1415926-1.0 (Aspose.TeX 21.8)
 2entering extended mode
 3**<path_to_the_file>/hello-world.ltx
 4(<path_to_the_file>/hello-world.ltx
 5LaTeX2e <2011/06/27>
 6(article.cls
 7Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
 8(size10.clo))
 9No file hello-world.aux.
10[1]
11(<output_directory>\hello-world.aux) )
12Output written on hello-world.xps (1 page).
13Transcript written on hello-world.log.