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\\documentclass[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}
mypreamble.sty
is just for loading some package? or it is a complete preamble? – touhami Dec 18 '15 at 18:58n
and thet
in the final\documentclass
in the command line. If you eliminate that, it works fine. (Well, I commented the preamble package as well, obviously fromfile.tex
.) – cfr Dec 19 '15 at 3:09