I would like to export Latex from Lyx without the preamble. For example I would like to automatically remove all code before \section{} as well as \end{document}. This is so that I can automatically include the .tex output file into a larger Latex document for which I already have the preamble the way I want it.

The reason I am doing things this way is that I have large tables of equations which get very confusing to edit directly in plain Latex.

3 Answers 3


Not directly possible at the moment it seems, there is an open feature request for this in the LyX bug tracker:

But there is at least one workaround described there:

Create a dummy master LyX file, and include the other file(s) as child documents. When exporting the master file, each child document is created as a separate .tex file, without preamble.


The subfiles package as workaround

A simpler workaround would be using the \subfile command of the subfiles package, which automatically strips the document skeleton.

It's meant for making child documents compilable on their own, but by extension also fits this usecase.

Using a parent/child pair for export without preamble.

It does not help, if the purpose is to export to latex for inclusion in a journal template, where loading extra packages isn't allowed. In this case a viable workaround is to create a file main.lyx with child document child.lyx, and export from main.lyx recursively, which produces a preamble-free child.tex.

Automation as a script

This could also be easily automated as a script, by exploiting the straight-forward file-format of .lyx documents:

  • Create a .tmp.lyx with the same \begin_header ... \end_header as child.lyx to ensure consistent export of styles and insets.
  • Make its \begin_body ... \end_body contain only the child-document link.
  • Export .tmp.lyx to .tmp.tex. This will create the desired child.tex.
  • Remove the unnecessary .tmp.* files.
  • Re the subfiles variant: That does not seem to be how the subfiles package works.
    – srs
    Commented Jul 15, 2019 at 11:42

Use the standalone package in the including document as a workaround

The standalone package redefines \input so that any preamble is ignored. Usage:

  • Export your lyx document to tex normally.
  • In the including document, put \usepackage{standalone} in the preamble.
  • Load the exported tex document normally using \input or \include.
  • The preamble of the included document will be ignored.

The packge also has an option to somehow use a sub-preamble (see the docs), but I haven't used this.

NB: There's a standalone package and a standalone document class. The package is for the including document and the document class can, but doesn't need to be used for the included document. In this answer, we only use the package.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .