Other managing TeX options | Python

The examples below create XPS output. Apply a license as described in Licensing and set ASPOSE_TEX_OUTPUT_DIR to the output folder, or use the default output. Run each example in a separate output folder to retain all results.

Setting the interaction mode

As mentioned in TeX interaction paragraph, Aspose.TeX for Python provides the capability to set the initial interaction mode in which the engine starts. Here is an example of how it can be done:

 1# Process a LaTeX document without interactive error prompts.
 2import os
 3from pathlib import Path
 4from io import BytesIO
 5from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 6from aspose.tex.io import InputFileSystemDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 7from aspose.tex.presentation.xps import XpsDevice, XpsSaveOptions
 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.output_working_directory = OutputFileSystemDirectory(str(output_dir))
14options.terminal_out = OutputMemoryTerminal()
15options.interaction = Interaction.NONSTOP_MODE
16save_options = XpsSaveOptions()
17options.save_options = save_options
18device = XpsDevice()
19source = r"\documentclass{article}\begin{document}Hello, World!\end{document}"
20with BytesIO(source.encode("ascii")) as stream:
21    result = TeXJob(stream, device, options).run()

Setting the job name

When the main input file is provided as a file name, the resulting output files will have the same name but with different extensions. The TeX engine refers to the input file’s name as the job name and uses it for the output files. The exception is auxiliary files with explicitly specified file names. However, when the main input file is passed as a stream, the TeX engine uses the default job name, which is texput. In both scenarios, overriding the job name by assigning the appropriate conversion option is possible.

 1# Override the output job name.
 2import os
 3from pathlib import Path
 4from io import BytesIO
 5from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 6from aspose.tex.io import InputFileSystemDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 7from aspose.tex.presentation.xps import XpsDevice, XpsSaveOptions
 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.output_working_directory = OutputFileSystemDirectory(str(output_dir))
14options.terminal_out = OutputMemoryTerminal()
15options.interaction = Interaction.NONSTOP_MODE
16save_options = XpsSaveOptions()
17options.job_name = "my-job-name"
18options.save_options = save_options
19device = XpsDevice()
20source = r"\documentclass{article}\begin{document}Hello, World!\end{document}"
21with BytesIO(source.encode("ascii")) as stream:
22    result = TeXJob(stream, device, options).run()

“Stopping time”

To automatically generate a title from certain definitions in the preamble, LaTeX offers a feature that typically includes the current date. However, there might be instances where we want to fix the date to a specific value. Here is a method to achieve that:

 1# Fix the date used by LaTeX.
 2import os
 3from pathlib import Path
 4from io import BytesIO
 5from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 6from aspose.tex.io import InputFileSystemDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 7from aspose.tex.presentation.xps import XpsDevice, XpsSaveOptions
 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.output_working_directory = OutputFileSystemDirectory(str(output_dir))
14options.terminal_out = OutputMemoryTerminal()
15options.interaction = Interaction.NONSTOP_MODE
16save_options = XpsSaveOptions()
17from datetime import datetime
18options.date_time = datetime(2022, 12, 18)
19options.save_options = save_options
20device = XpsDevice()
21source = r"\documentclass{article}\begin{document}\today\end{document}"
22with BytesIO(source.encode("ascii")) as stream:
23    result = TeXJob(stream, device, options).run()

Ignoring missing packages

If we have a LaTeX file that includes references to packages that are not supported by the Aspose.TeX for Python library, the TeX engine will encounter an error and stop when attempting to load these packages. To prevent this, we can utilize the following option:

 1# Skip an unavailable LaTeX package.
 2import os
 3from pathlib import Path
 4from io import BytesIO
 5from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 6from aspose.tex.io import InputFileSystemDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 7from aspose.tex.presentation.xps import XpsDevice, XpsSaveOptions
 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.output_working_directory = OutputFileSystemDirectory(str(output_dir))
14options.terminal_out = OutputMemoryTerminal()
15options.interaction = Interaction.NONSTOP_MODE
16save_options = XpsSaveOptions()
17options.ignore_missing_packages = True
18options.save_options = save_options
19device = XpsDevice()
20source = r"\documentclass{article}\usepackage{asposeexamplemissingpackage}\begin{document}Hello, World!\end{document}"
21with BytesIO(source.encode("ascii")) as stream:
22    result = TeXJob(stream, device, options).run()

How to avoid building ligatures

By default, the TeX engine constructs ligatures for specific character pairs if the font contains the necessary data. However, we can instruct the engine to bypass ligature construction using the following code:

 1# Disable construction of typographic ligatures.
 2import os
 3from pathlib import Path
 4from io import BytesIO
 5from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 6from aspose.tex.io import InputFileSystemDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 7from aspose.tex.presentation.xps import XpsDevice, XpsSaveOptions
 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.output_working_directory = OutputFileSystemDirectory(str(output_dir))
14options.terminal_out = OutputMemoryTerminal()
15options.interaction = Interaction.NONSTOP_MODE
16save_options = XpsSaveOptions()
17options.no_ligatures = True
18options.save_options = save_options
19device = XpsDevice()
20source = r"\documentclass{article}\begin{document}office efficient affine\end{document}"
21with BytesIO(source.encode("ascii")) as stream:
22    result = TeXJob(stream, device, options).run()

Repeating the job

As mentioned in LaTeX input file paragraph, labels and references, there are situations where we may need to run the same job twice. Here is a method to accomplish that:

 1# Resolve a forward reference by repeating the job.
 2import os
 3from pathlib import Path
 4from io import BytesIO
 5from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 6from aspose.tex.io import InputFileSystemDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 7from aspose.tex.presentation.xps import XpsDevice, XpsSaveOptions
 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.output_working_directory = OutputFileSystemDirectory(str(output_dir))
14options.terminal_out = OutputMemoryTerminal()
15options.interaction = Interaction.NONSTOP_MODE
16save_options = XpsSaveOptions()
17options.repeat = True
18options.save_options = save_options
19device = XpsDevice()
20source = r"\documentclass{article}\begin{document}See Section \ref{sec:target}.\section{Target}\label{sec:target}\end{document}"
21with BytesIO(source.encode("ascii")) as stream:
22    result = TeXJob(stream, device, options).run()

Turning math formulas into raster images

If there is a need to convert math formulas into raster images instead of rendering them with fonts, the following option can be used for this purpose:

 1# Render math formulas as raster images.
 2import os
 3from pathlib import Path
 4from io import BytesIO
 5from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 6from aspose.tex.io import InputFileSystemDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 7from aspose.tex.presentation.xps import XpsDevice, XpsSaveOptions
 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.output_working_directory = OutputFileSystemDirectory(str(output_dir))
14options.terminal_out = OutputMemoryTerminal()
15options.interaction = Interaction.NONSTOP_MODE
16save_options = XpsSaveOptions()
17save_options.rasterize_formulas = True
18options.save_options = save_options
19device = XpsDevice()
20source = r"\documentclass{article}\begin{document}Formula: $x^2+y^2=z^2$\end{document}"
21with BytesIO(source.encode("ascii")) as stream:
22    result = TeXJob(stream, device, options).run()

Turning graphics into raster images

The ObjectTeX engine provides the capability to include graphic files in raster formats such as PNG and JPG, as well as PS (EPS) and XPS (OXPS) formats. The latter two formats typically contain vector elements and text. If we want to rasterize and include them as solid images, we can utilize the following option:

 1# Rasterize an included vector EPS graphic.
 2import os
 3from pathlib import Path
 4from io import BytesIO
 5from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 6from aspose.tex.io import InputFileSystemDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 7from aspose.tex.presentation.xps import XpsDevice, XpsSaveOptions
 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.output_working_directory = OutputFileSystemDirectory(str(output_dir))
14options.terminal_out = OutputMemoryTerminal()
15options.interaction = Interaction.NONSTOP_MODE
16save_options = XpsSaveOptions()
17save_options.rasterize_included_graphics = True
18input_root = Path(os.environ.get("ASPOSE_TEX_INPUT_DIR", "input")).resolve()
19options.input_working_directory = InputFileSystemDirectory(str(input_root / "Typesetting"))
20options.save_options = save_options
21device = XpsDevice()
22source = r"\documentclass{article}\usepackage{graphicx}\begin{document}\includegraphics{triangle.eps}\end{document}"
23with BytesIO(source.encode("ascii")) as stream:
24    result = TeXJob(stream, device, options).run()

The graphics example reads Typesetting/triangle.eps under ASPOSE_TEX_INPUT_DIR (default: input). Preserve that subdirectory when preparing the example resources.

Subsetting fonts

If we desire to decrease the size of the output file, we can employ font subsetting, which entails that the fonts in the resulting document will not include data for those glyphs that are not present in the document. Here is a solution to achieve this:

 1# Embed only the used font glyphs.
 2import os
 3from pathlib import Path
 4from io import BytesIO
 5from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 6from aspose.tex.io import InputFileSystemDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 7from aspose.tex.presentation.xps import XpsDevice, XpsSaveOptions
 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.output_working_directory = OutputFileSystemDirectory(str(output_dir))
14options.terminal_out = OutputMemoryTerminal()
15options.interaction = Interaction.NONSTOP_MODE
16save_options = XpsSaveOptions()
17save_options.subset_fonts = True
18options.save_options = save_options
19device = XpsDevice()
20source = r"\documentclass{article}\begin{document}Hello, World!\end{document}"
21with BytesIO(source.encode("ascii")) as stream:
22    result = TeXJob(stream, device, options).run()