1

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.

  • Regarding your first bullet, there might be a way to change them. If you would like, upload a minimal example (https://wiki.lyx.org/FAQ/MinimalExample) with a specific one you would like to change. Otherwise, I don't know if it's possible. If you go to View > Source Pane you can set it to show only the "Body" which would exclude the preamble. You could then copy/paster. Other than that, you might want to make a script that automatically strips the LyX preamble after it is exported. – scottkosty May 16 '22 at 11:35
  • Thanks for the answer. Depending on what you put in the body, LyX automatically adds things here and there in the preamble. Thus there are too many things for a case-by-case basis and I am looking for a global workaround. The script approach looks interesting. Could you give some details ? – Captain Pi May 16 '22 at 11:48
  • Well, after you built up 10 cases and told LyX not to load each of the packages, you could then just copy that setup to future setups and I think that might be the best approach. This is the first time I've seen someone want to completely disable LyX's preamble, so I'm still wondering if either (1) there might be a "LyX way" to do some of what you are currently doing customly instead of using ERT; or (2) if you might be happier just not using LyX. LyX is supposed to make things easier but it seems it is getting in the way and that must be annoying. – scottkosty May 16 '22 at 13:27
  • For the script, to make it seamless, you'd make your own "LyX converter". It would take time to understand how to do this. If you want to go this route, study the information in Help > Customization (it's a dry read though). Then, the setup would essentially be that when you want to export to PDF, you would choose the option "PDF (my preamble)" and that would be seamless. In the background, LyX would export to LaTeX, then your script would strip LyX's preamble, then LaTeX would compile to PDF. If you want to go this way, join lyx-users mailing list and ask for help if you get stuck. – scottkosty May 16 '22 at 13:29
  • While you can ask LyX to not load a lot of packages, some are not removable. Also it is not robust as any modification of the file might add another package and one has to find a workaround on this case-by-case basis. Basically, I want to benefit from LyX user interface which is excellent for redaction, while still having full control on the preamble. An option I am considering is exporting from LyX to LateX and then have a script remove everything LyX adds to the tex file before compiling it. I will update my post if I succeed. Thanks for the suggestion. – Captain Pi May 16 '22 at 17:56
  • That sounds like a good plan. Good luck! One other thought that came to mind: perhaps you could write a separate .tex file that inputs the .tex file that LyX exports. It might be possible to have the "main" tex file (that one that is doing the importing) ignore the preamble of the .tex file it inputs. Maybe the following is useful: https://www.ctan.org/tex-archive/macros/latex/contrib/standalone – scottkosty May 16 '22 at 18:16
  • Because LyX make the preamble depending on what you type, disable this could even worse that deal by real conflicts with user preambles. As alternative, beside to pass to Dark Side of plain text editors , you can also consider use a Markdown in RStudio in visual mode, that is also a WYSYWYM editor, but you can take a full control of the preamble via custom pandoc templates. – Fran May 20 '22 at 10:41

1 Answers1

1

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.