Aspose.TeX's input interface | Python
For formal definitions of the I/O implementation in Aspose.TeX for Python, please consult the API reference.
The idea behind the input directory
Given that the I/O primitives of the TeX language can only handle file names, Aspose.TeX for Python defines a directory as a mapping between names and collections of data. These collections of data can be files, streams, arrays, or any other type. The API allows for separate specification of input and output working directories. It provides the general InputWorkingDirectory class, which users can implement for their specific needs. Additionally, the API provides built-in implementations, which will be discussed later. The interface includes the get_file() method, which retrieves the data stream and determines the full file name based on a given name, which serves as the mapping key.
Retrieving file input from the disk file system
Here is the approach we would take:
Set ASPOSE_TEX_OUTPUT_DIR to an output directory, or use the default output. Apply a license first as described in
Licensing.
1# Read a TeX file from a file system working directory.
2import os
3from pathlib import Path
4from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
5from aspose.tex.io import InputFileSystemDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
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.NONSTOP_MODE
16options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
17options.terminal_out = OutputMemoryTerminal()
18options.input_working_directory = InputFileSystemDirectory(str(input_dir))
19device = XpsDevice()
20job_result = TeXJob("hello-world.tex", device, options).run()Set ASPOSE_TEX_INPUT_DIR to a folder containing hello-world.tex (default: input). The example uses plain TeX with TeXConfig.object_tex() and produces hello-world.xps and hello-world.log. Use this input:
1Hello, World!
2\endRetrieving file input from a ZIP archive
Another option is to store the input files in a ZIP archive and treat it as an input directory. In such a scenario, the following steps should be followed:
1# Read a TeX file from a subdirectory inside a ZIP archive.
2import os
3from pathlib import Path
4from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
5from aspose.tex.io import InputZipDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
6from aspose.tex.presentation.xps import XpsDevice
7
8output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
9output_dir.mkdir(parents=True, exist_ok=True)
10input_zip = Path(os.environ.get("ASPOSE_TEX_INPUT_ZIP", "zip-in.zip")).resolve()
11
12# Keep the binary ZIP stream open until the job finishes.
13with input_zip.open("rb") as input_stream:
14 options = TeXOptions.console_app_options(TeXConfig.object_tex())
15 options.interaction = Interaction.NONSTOP_MODE
16 options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
17 options.terminal_out = OutputMemoryTerminal()
18 # Resolve hello-world.tex inside the in/ directory of the archive.
19 options.input_working_directory = InputZipDirectory(input_stream, "in")
20 device = XpsDevice()
21 job_result = TeXJob("hello-world.tex", device, options).run()Set ASPOSE_TEX_INPUT_ZIP to an archive containing in/hello-world.tex; the default is zip-in.zip. Open it in binary mode (rb) and keep the stream open until the job finishes.
To begin with, we create the stream that contains the ZIP file. Subsequently, after creating the conversion options, we assign an instance of the InputZipDirectory class to the input_working_directory property. The second parameter of the constructor represents the base path within the archive. If we desire the entire archive to function as the input directory, we should provide an empty string as the argument.
The idea behind the input terminal
Now let’s consider the concept of terminal input. Aspose.TeX for Python defines the general IInputTerminal interface specifically for this purpose, which includes a single property that returns an instance of TerminalReader implementation. The available implementations will be discussed later on.
Getting terminal input from the console
To achieve this, we need to assign an instance of the InputConsoleTerminal class to the terminal_in option.
1# Read a line from the console while processing a TeX stream.
2import os
3from pathlib import Path
4from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
5from io import BytesIO
6from aspose.tex.io import InputConsoleTerminal, OutputConsoleTerminal, OutputFileSystemDirectory
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_tex())
13options.interaction = Interaction.SCROLL_MODE
14options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
15options.terminal_in = InputConsoleTerminal()
16options.terminal_out = OutputConsoleTerminal()
17# Enter a line of text when TeX requests input.
18with BytesIO(br"\read-1 to \answer \answer\par\end") as input_stream:
19 job_result = TeXJob(input_stream, XpsDevice(), options).run()The example reads a line using the TeX command \read and writes it to texput.xps. Run it in a terminal and enter a line such as Terminal input works!, or supply that line through standard input. SCROLL_MODE allows this explicit input request.