Alternative methods to supply main TeX input file | Python

Supplying the main input file to the TeX engine using a stream

So far, we have only learned how to provide the main input file to the TeX engine as a file name, whether in its full form or in a shortened form, with or without an extension. However, there is another constructor available in the TeXJob class that accepts a stream as the first argument. This is useful if, for some reason, the main input file is not in the form of a file on the disk file system. Here is an example of how it can be implemented:

Set ASPOSE_TEX_OUTPUT_DIR to an output directory, or use the default output. Apply a license first as described in Licensing.

 1# Render LaTeX supplied as a binary in-memory stream to XPS.
 2import os
 3from pathlib import Path
 4from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 5from io import BytesIO
 6from aspose.tex.io import OutputFileSystemDirectory, OutputMemoryTerminal
 7from aspose.tex.presentation.xps import XpsDevice
 8
 9output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
10output_dir.mkdir(parents=True, exist_ok=True)
11
12options = TeXOptions.console_app_options(TeXConfig.object_latex)
13options.interaction = Interaction.NONSTOP_MODE
14options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
15options.terminal_out = OutputMemoryTerminal()
16latex_source = r"\documentclass{article} \begin{document} Hello, World! \end{document}"
17device = XpsDevice()
18with BytesIO(latex_source.encode("ascii")) as input_stream:
19    job_result = TeXJob(input_stream, device, options).run()

This example uses a complete LaTeX document and TeXConfig.object_latex. The binary stream remains open for the duration of the job; the output is XPS.

Note that all output files will be named texput. This is because the engine does not have access to any other name. texput is the default job name. You can find more detailed information about job names TeX output and How to set the job name paragraphs.

Inputting the main TeX input file through the terminal

There is one more constructor available in the TeXJob class that does not allow us to specify the input at all. You might be wondering what such a TeX job would process. In this case, we would need to use an input terminal. The TeX engine will prompt us to enter the file name from the terminal.

Here is the code:

 1# Choose the main TeX input filename through the console.
 2import os
 3from pathlib import Path
 4from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 5from aspose.tex.io import InputConsoleTerminal, InputFileSystemDirectory, OutputConsoleTerminal, OutputFileSystemDirectory
 6from aspose.tex.presentation.xps import XpsDevice
 7
 8input_dir = Path(os.environ.get("ASPOSE_TEX_INPUT_DIR", "input")).resolve()
 9if not (input_dir / "hello-world.tex").is_file():
10    raise FileNotFoundError(input_dir / "hello-world.tex")
11output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
12output_dir.mkdir(parents=True, exist_ok=True)
13
14options = TeXOptions.console_app_options(TeXConfig.object_tex())
15options.interaction = Interaction.SCROLL_MODE
16options.input_working_directory = InputFileSystemDirectory(str(input_dir))
17options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
18options.terminal_in = InputConsoleTerminal()
19options.terminal_out = OutputConsoleTerminal()
20# At the prompt, enter hello-world.tex from the configured input directory.
21job_result = TeXJob(XpsDevice(), options).run()

Set ASPOSE_TEX_INPUT_DIR to a folder containing the following plain TeX file named hello-world.tex (default folder: input):

1Hello, World!
2\end

This terminal example uses TeXConfig.object_tex(). When prompted, enter hello-world.tex and press Enter, or supply that filename through standard input. The job creates hello-world.xps and hello-world.log.