External LaTeX packages | Python
External LaTeX packages
The Aspose.TeX library comes with a variety of
common LaTeX packages, so there is no need to manually provide these packages to the TeX engine. However, there may be situations where your LaTeX file requires a package that is not included in the library’s “bundle” of packages. In such cases, you can try providing the necessary input, such as the source files of the required package, through the
required_input_directory option of the
TeXOptions class instance. We will explore how this works with two examples. Apply a
license first. Set ASPOSE_TEX_INPUT_DIR to a folder containing ExternalLaTeXPackages, with the two .tex files and the packages subdirectory shown in the examples (default root: input). Set ASPOSE_TEX_OUTPUT_DIR to the output directory, or use the default output.
Required input provided in an unpacked form (the fancybox package).
Suppose we have the following basic LaTeX file named required-input-fs.tex, which is part of our
example solution:
1\documentclass{article}
2\usepackage[a6paper,landscape]{geometry}
3\usepackage{fancybox}
4\begin{document}
5Test: \fbox{
6 \begin{Bitemize}[b]
7 \item First item
8 \item A second one\\ on two lines
9 \item(2pt) A third with extra space
10 \end{Bitemize}
11}
12\par\bigskip
13Test: \fbox{
14 \begin{Beqnarray}[t]
15 y & = & x^2 \\
16 a^2 + 2ab + b^2 & = & (a + b)^2 \\
17 \int_0^\infty e^{-ax} dx & = & \frac{1}{a}
18 \end{Beqnarray}
19}
20\end{document}The 3rd line of the file indicates that it requires the fancybox package, which is not included in the “native” support. Let’s assume we have the source file for the fancybox package. Since it is a simple package consisting of a single file, we can place this file anywhere in our file system and specify the directory path as follows:
1from aspose.tex.io import InputFileSystemDirectory
2
3# Configure an existing TeXOptions instance.
4options.required_input_directory = InputFileSystemDirectory('path-to-directory-where-fancybox.sty-located')Once we run a TeX job with this option (remember to adjust the other options as necessary), we will obtain the output document, which in this case is a PNG image.

Here is the complete source code for the example:
1# Load an external LaTeX package from a directory.
2import os
3from pathlib import Path
4from os import path
5from io import BytesIO
6from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
7from aspose.tex.io import InputFileSystemDirectory, InputZipDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
8from aspose.tex.presentation.image import ImageDevice, PngSaveOptions
9
10output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
11output_dir.mkdir(parents=True, exist_ok=True)
12input_dir = Path(os.environ.get("ASPOSE_TEX_INPUT_DIR", "input")).resolve() / "ExternalLaTeXPackages"
13# Create conversion options for Object LaTeX format upon Object TeX engine extension.
14options = TeXOptions.console_app_options(TeXConfig.object_latex)
15options.interaction = Interaction.NONSTOP_MODE
16options.terminal_out = OutputMemoryTerminal()
17# Specify a file system working directory for the output.
18options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
19# Specify a file system working directory for the required input.
20# The directory containing packages may be located anywhere.
21options.required_input_directory = InputFileSystemDirectory(path.join(str(input_dir), "packages"))
22# Initialize the options for saving in PNG format.
23options.save_options = PngSaveOptions()
24# Run LaTeX to PNG conversion.
25TeXJob(path.join(str(input_dir), "required-input-fs.tex"), ImageDevice(True), options).run()Providing required input in an archived form (the pgfplots package)
Now, suppose we have the following LaTeX file named required-input-zip.tex, which is also a simple file from our example solution:
1\documentclass{article}
2\usepackage[margin=0.25in]{geometry}
3\usepackage{pgfplots}
4\pgfplotsset{width=10cm,compat=1.18}
5\begin{document}
6
7First example is 2D and 3D math expressions plotted side-by-side.
8
9%Here begins the 2D plot
10\begin{tikzpicture}
11\begin{axis}
12\addplot[color=red]{exp(x)};
13\end{axis}
14\end{tikzpicture}
15%Here ends the 2D plot
16\hskip 5pt
17%Here begins the 3D plot
18\begin{tikzpicture}
19\begin{axis}
20\addplot3[
21 surf,
22]
23{exp(-x^2-y^2)*x};
24\end{axis}
25\end{tikzpicture}
26%Here ends the 3D plot
27
28\end{document}On the third line, it can be observed that the file necessitates the pgfplots package, which is not “natively” supported. Once again, assuming that we possess the source files for the pgfplots package, these files are quite numerous and are located in two different directories within the installation directory of any LaTeX typesetting application. The pgfplots folder can be found in both the \tex\generic and \tex\latex directories. For the Aspose.TeX library to function correctly, it is necessary to provide the contents of both these folders as input. To achieve this, we desire to package these source files into a ZIP archive. Below is the desired layout of the archive:

And here is the method by which we define the accessibility to these source files:
1from aspose.tex.io import InputZipDirectory
2
3# Configure an existing TeXOptions instance and run the job while the stream is open.
4with open("path-to-zip-with-pgfplots-sources", "rb") as zip_stream:
5 options.required_input_directory = InputZipDirectory(zip_stream, "")
6 # Run TeXJob here, as shown in the complete example below.After running a TeX job with this option, we get the output document:

Here is the complete source code for the example:
1# Load external LaTeX packages from a ZIP archive.
2import os
3from pathlib import Path
4from os import path
5from io import BytesIO
6from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
7from aspose.tex.io import InputFileSystemDirectory, InputZipDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
8from aspose.tex.presentation.image import ImageDevice, PngSaveOptions
9
10output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
11output_dir.mkdir(parents=True, exist_ok=True)
12input_dir = Path(os.environ.get("ASPOSE_TEX_INPUT_DIR", "input")).resolve() / "ExternalLaTeXPackages"
13# Create conversion options for Object LaTeX format upon Object TeX engine extension.
14options = TeXOptions.console_app_options(TeXConfig.object_latex)
15options.interaction = Interaction.NONSTOP_MODE
16options.terminal_out = OutputMemoryTerminal()
17# Specify a file system working directory for the output.
18options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
19# Initialize the options for saving in PNG format.
20options.save_options = PngSaveOptions()
21# Create a file stream for the ZIP archive containing the required package.
22# The ZIP archive may be located anywhere.
23with open(path.join(str(input_dir), "packages/pgfplots.zip"), "rb") as zip_stream:
24 # Specify a ZIP working directory for the required input.
25 options.required_input_directory = InputZipDirectory(zip_stream, "")
26
27 # Run LaTeX to PNG conversion.
28 TeXJob(path.join(str(input_dir), "required-input-zip.tex"), ImageDevice(True), options).run()NOTE: The result was verified using the
pgfplotspackage version 1.18.1. While the version of thepfgpackage included in the Aspose.TeX library is 3.1.9a.
Restrictions
If you come across a package that is developed under the LaTeX3e kernel anв is required by your LaTeX file, it is unlikely to work with the Aspose.TeX library. This is because the library is based on the LaTeX2e kernel.
In addition, there may be cases where a package required by your LaTeX file directly uses device-dependent primitive commands that are not supported by the Aspose.TeX library’s Object TeX engine. In such cases, the package will not work with the library.