1

Is there a standard way to automatically inject certain files into a LaTeX preamble? Naturally I could write some patch code for this but I thought I should check if there's some "preferred" way to accomplish the job.

Inspired by Creating a default preamble the hypothetical MWE is as follows:

mypreamble.sty

\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{mypreamble}
 [2015/11/18 MyPreamble]
\RequirePackage[dvipsnames]{xcolor}
\endinput

file.tex

\documentclass{article}
\newcommand{\wow}[1]{\textbf{Wow, much #1!}}
\begin{document}
\textcolor{red}{This works, trust me.}
\wow{color}
\end{document}

Invocation would then be something like:

smart-pdflatex --with-style mypreamble.sty file.tex

Output to be the same as running

pdflatex smart-file.tex 

smart-file.tex

\documentclass{article}
\usepackage{mypreamble}
\newcommand{\wow}[1]{\textbf{Wow, much #1!}}
\begin{document}
\textcolor{red}{This works, trust me.}
\wow{color}
\end{document}

Note, the above examples use the article class, but I'm hoping for a solution that also works with memoir, etc.; and similarly, it would be great to have a recipe that works for xelatex as well as pdflatex.

At the moment the above examples are working only for the article×pdflatex "quadrant", using the recipe from David Carlisle's answer below. Switching article to memoir, I get

! LaTeX Error: Command \@namelet already defined.

And with xelatex I get

! LaTeX Error: Missing \begin{document}.

Update @cfr, Dec 19 2015, 0123 UTC:

For example, with mypreamble.sty as above, and adjusting the compilation command following suggestions from comments below, I try to compile

file.tex

\documentclass{memoir}
\newcommand{\wow}[1]{\textbf{Wow, much #1!}}
\begin{document}
\textcolor{red}{This works, trust me.}
\wow{color}
\end{document}

with the command (all on one line, no extra spaces):

pdflatex -jobname=file \\documentclass[a4paper,11pt]{memoir}\\usepackage{mypreamble}\\renewcommand\\documen‌​tclass[2][]{}\\input{file}

and I get the error

! LaTeX Error: \documen undefined.

Update - invisible character (oops)

The command above looks the same as this one but contains an invisible character, which is the source of the error. The following command works with memoir.

pdflatex -jobname=file \\documentclass[a4paper,11pt]{memoir}\\usepackage{mypreamble}\\renewcommand\\documentclass[2][]{}\\input{file}
3
  • 1
    mypreamble.sty is just for loading some package? or it is a complete preamble? – touhami Dec 18 '15 at 18:58
  • In my use case it should just load a package. I'll adjust the example to clarify. – Joe Corneli Dec 18 '15 at 19:10
  • You have an invisible character between the n and the t in the final \documentclass in the command line. If you eliminate that, it works fine. (Well, I commented the preamble package as well, obviously from file.tex.) – cfr Dec 19 '15 at 3:09
6
pdflatex -jobname=file \\RequirePackage{mypreamble} \\input{file}

would work in the basic case (you may or may not need to double the \\ depending on your commandline shell)

If requirements vary (e.g. for xelatex+memoir), some adjustments are needed.

xelatex -jobname=file \\documentclass[a4paper,11pt]{memoir}\\usepackage{mypreamble}\\renewcommand\\documentclass[2][]{}\\input{file}
11
  • 1
    pdflatex -jobname=file ... to be complete – touhami Dec 18 '15 at 18:49
  • 2
    this can't work with some packages that redefine class commands. One should try pdflatex -jobname=file \documentclass{article}\usepackage{mypreamble}\renewcommand{\documentclass}[1]{}\input{file} or pdflatex -jobname=file \documentclass[a4paper,12pt]{article}\usepackage{mypreamble}\renewcommand{\documentclass}[2][]{}\input{file} – touhami Dec 18 '15 at 19:34
  • OK, for example it doesn't appear to work with memoir. But your suggestions aren't immediately fixing it. – Joe Corneli Dec 18 '15 at 19:46
  • @JoeCorneli What exactly did you try? You'd want something like pdflatex -jobname=file \documentclass[a4paper,11pt]{memoir}\usepackage{mypreamble}\renewcommand\documentclass[2][]{}\input{file} where file.tex is the name of your .tex file and mypreamble.sty is the preamble. In some cases, of course, you'll doubtless still have trouble, but I suppose it would work in simple cases. Though I would not recommend doing it. – cfr Dec 19 '15 at 0:46
  • 1
    @JoeCorneli It is just an invisible character in your command line. It works if you remove it. – cfr Dec 19 '15 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.