Disable LyX preamble

lyxpreamble

In LyX, I would like to know if there is a way, for a given document, to disable all parts of the preamble that LyX adds automatically, leaving only the user-defined preamble before compilation occurs (Document->Settings->Preamble).

It might seem like an odd request, but there are 2 big advantages in doing so:

  • LyX often pre-loads packages with its own options, and one cannot change them in the user-defined preamble.
  • It is easier to export a user-defined preamble (I store it inside a preamble.tex file and one just puts input{…/preamble.tex} in a new LyX or LaTeX document.

Does anyone know a way to do it ?

EDIT: I FOUND A SOLUTION, see below. Thanks for the time others have put into solving my problem.

Best Answer

I was able to find my own LyX-only solution as follows :

Step 1: create a custom class, using the method in this post. I thus created a file "course.cls" based on the book class and registered it in the LyX class database. See also this post to put the .cls anywhere in your hard drive.

Step 2: change your document class in LyX (Document->Settings->Document class)

Step 3: In course.cls, after \LoadClassWithOptions{...}, add

\usepackage{afterpackage}
\AfterPackage{inputenc}{
\PopDefaultHookLabel
\input{Path/To/My/Own/Preamble.tex}
\usepackage{docmute}
\docmute
}

The package afterpackage allows to execute a code right after the package inputenc is loaded, which is usually the third line of LyX automatic preamble, before LyX adds anything specific. Then you put your own preamble, and finally the docmute package discards the rest of the preamble.

This is the only solution that works. I tried to input a preamble then use docmute, the rest of LyX preamble is not discarded (it seems that the class has its own preamble which is the one affected by docmute).

This fact seems to also mess with some hooks, but adding \PopDefaultHookLabel solves that. You can then for example copy and paste your whole LyX preamble and modify it as you please. Be also careful that in doing so, some special characters like é è present in LyX preamble have to be replaced by their '{e} and `{e} counterparts.