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.

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:

1options.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.

Output Document

Here is the complete source code for the example:

 1# Create conversion options for Object LaTeX format upon Object TeX engine extension.
 2options = TeXOptions.console_app_options(TeXConfig.object_latex)
 3# Specify a file system working directory for the output.
 4options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
 5# Specify a file system working directory for the required input.
 6# The directory containing packages may be located anywhere.
 7options.required_input_directory = InputFileSystemDirectory(path.join(Util.input_directory, "packages"))
 8# Initialize the options for saving in PNG format.
 9options.save_options = PngSaveOptions()
10# Run LaTeX to PNG conversion.
11TeXJob(path.join(Util.input_directory, "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:

Archive’s Layout

And here is the method by which we define the accessibility to these source files:

1with open("path-to-zip-with-pgfplots-sources") as zipStream:
2    ...
3    options.required_input_directory = InputZipDirectory(zipStream)
4    ...

After running a TeX job with this option, we get the output document:

Output Document

Here is the complete source code for the example:

 1# Create conversion options for Object LaTeX format upon Object TeX engine extension.
 2options = TeXOptions.console_app_options(TeXConfig.object_latex)
 3# Specify a file system working directory for the output.
 4options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
 5# Initialize the options for saving in PNG format.
 6options.save_options = PngSaveOptions()
 7# Create a file stream for the ZIP archive containing the required package.
 8# The ZIP archive may be located anywhere.
 9with open(path.join(Util.input_directory, "packages\\pgfplots.zip"), "rb") as zip_stream:
10    # Specify a ZIP working directory for the required input.
11    options.required_input_directory = InputZipDirectory(zip_stream, "")
12
13    # Run LaTeX to PNG conversion.
14    TeXJob(path.join(Util.input_directory, "required-input-zip.tex"), ImageDevice(True), options).run()

NOTE: The result was verified using the pgfplots package version 1.18.1. While the version of the pfg package 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.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.