LinuxCommandLibrary

latexpand

Expand LaTeX documents by inlining included files

TLDR

Simplify the specified source file and save the result to the specified output file

$ latexpand [[-o|--output]] [path/to/output.tex] [path/to/file.tex]
copy

Do not remove comments
$ latexpand --keep-comments [[-o|--output]] [path/to/output.tex] [path/to/file.tex]
copy

Do not expand \includes, \inputs etc.
$ latexpand --keep-includes [[-o|--output]] [path/to/output.tex] [path/to/file.tex]
copy

Expand \usepackages as far as the corresponding STY files can be found
$ latexpand --expand-usepackage [[-o|--output]] [path/to/output.tex] [path/to/file.tex]
copy

Inline the specified BBL file
$ latexpand --expand-bbl [path/to/bibliography.bbl] [[-o|--output]] [path/to/output.tex] [path/to/file.tex]
copy

SYNOPSIS

latexpand [OPTIONS] [FILE...]

PARAMETERS

-o, --output=FILE
    Specifies the path for the output file. If omitted, output is sent to standard output (stdout).

-u, --unlink
    Deletes the original input file(s) after successful processing. Use with caution.

--discard-comments
    Removes all LaTeX comments (lines starting with %) from the output.

--discard-macros
    Removes macro definitions (e.g., \newcommand, \def) from the output after they have been expanded.

--expand-usepackage
    Expands \usepackage commands by embedding the content of the corresponding `.sty` files directly into the output.

--expand-addbibresource
    Expands \addbibresource commands by embedding the content of the referenced `.bib` (BibTeX/BibLaTeX) files into the output.

--expand-includegraphics
    Expands \includegraphics commands by embedding the content of the specified graphics files directly into the output. This can result in very large files.

--read-sources-only
    Processes only the explicitly provided source files, without attempting to expand any \input or \include directives found within them. This is typically for specialized use cases.

--empty-comments
    When used with --discard-comments, replaces comments with empty lines instead of removing them completely, preserving line numbering.

--full
    A convenience option equivalent to using --discard-comments, --expand-usepackage, --expand-addbibresource, and --expand-includegraphics together.

--help
    Displays a help message with usage information and exits.

--version
    Displays the command's version information and exits.

DESCRIPTION

latexpand is a utility designed to flatten LaTeX files by expanding various commands into their contents. It replaces common directives such as \input, \include, \subfile, and \includegraphics with the actual text or binary content of the referenced files. Furthermore, it expands package inclusions (\usepackage) and bibliography resource additions (\addbibresource) by embedding the respective `.sty` and `.bib` file contents. The tool also handles macro definitions like \newcommand, \def, and \let, expanding them where applicable.

Its primary purpose is to generate a single, self-contained `.tex` file from a multi-file LaTeX project. This flattened output is particularly useful for journal submissions, sharing LaTeX documents without dependencies, or for automated processing tools that require a single input file.

CAVEATS

Using the --expand-includegraphics option can embed large binary files directly into the LaTeX output, which will significantly increase the file size and may make the resulting document unwieldy or difficult to manage.

The --unlink option should be used with extreme caution, as it permanently deletes the original input file(s) after processing. Always ensure you have backups or understand the implications before using this option.

SEE ALSO

kpsewhich(1), tex(1), latex(1)

Copied to clipboard