# Load a package by command line

is there a way to load a package by command line? I tried:

latex "\RequirePackage{xcolor} \input{myfile.tex}"


but it doesn't work.

I need to load some packages "temporarily", by scripts I wrote, to do some checks on my output files (pdf or dvi).

Edit. I found that the string I use generates the file xcolor.pdf instead of myfile.pdf

As requested, this is my .log shell output:

tmp$latex "\RequirePackage{xcolor} \input{example.tex}" This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015) (preloaded format=latex) restricted \write18 enabled. entering extended mode LaTeX2e <2015/10/01> patch level 2 Babel <3.9m> and hyphenation patterns for 79 languages loaded. (/usr/local/texlive/2015/texmf-dist/tex/latex/xcolor/xcolor.sty (/usr/local/texlive/2015/texmf-dist/tex/latex/latexconfig/color.cfg) (/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/dvips.def)) (./example.tex (/usr/local/texlive/2015/texmf-dist/tex/latex/base/article.cls Document Class: article 2014/09/29 v1.4h Standard LaTeX document class (/usr/local/texlive/2015/texmf-dist/tex/latex/base/size11.clo)) (/usr/local/texlive/2015/texmf-dist/tex/latex/lipsum/lipsum.sty) No file xcolor.aux. [1 Non-PDF special ignored! Non-PDF special ignored! Non-PDF special ignored!{/usr/local/texlive/2015/texmf-var/fonts/map/pdftex/upd map/pdftex.map} Non-PDF special ignored! Non-PDF special ignored!] [2 Non-PDF special ignored! Non-PDF special ignored! Non-PDF special ignored! Non-PDF special ignored!] (./xcolor.aux) )</usr/local/texlive/2015/texmf-dist/f onts/type1/public/amsfonts/cm/cmr10.pfb> Output written on xcolor.pdf (2 pages, 22276 bytes). Transcript written on xcolor.log.  and this is the screenshot: • Check if you need double backslashes Jun 18, 2017 at 7:50 • Could you enlarge on 'doesn't work': it should. Perhaps your shell needs the backslash escaping (often as two backslashes). Jun 18, 2017 at 7:50 • Jun 18, 2017 at 7:52 • @JosephWright I get the package.pdf file instead of filename.þdf ones... also with double backslash excaping. Jun 18, 2017 at 8:19 • @JosephWright Thank you for your hint ;-) Jun 18, 2017 at 9:14 ## 2 Answers # Driver mismatch The file myfile.tex is broken: tmp$ latex


latex is initialized to generate DVI as output format.

(/usr/local/texlive/2015/texmf-dist/tex/latex/xcolor/xcolor.sty
...
(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/dvips.def))


Package xcolor is loaded with DVI driver dvips. Fine.

But then:

Non-PDF special ignored!
{/usr/local/texlive/2015/texmf-var/fonts/map/pdftex/upd


map/pdftex.map} ... Output written on xcolor.pdf

The mode switches to PDF and a PDF file is generated. Color specials for DVI have no effect.

The LaTeX document should never change the output mode. Many, many packages relay on a stable output mode. Check the code for changes of \pdfoutput. It may be read (better is the use of package ifpdf), but should not be changed.

There are packages, that can be loaded before \documentclass using \RequirePackage, see egreg's answer.

Other package require to be loaded after the class. Some of them, can be loaded even quite late in \AtBeginDocument, then the command line would become:

$pdflatex '\AtBeginDocument{\RequirePackage{xcolor}\pagecolor{yellow}}\input{myfile}'  An alternative is providing a hook in the file myfile.tex. For example, at the right place in the preamble: \ifx\WithPageColor Y \usepackage{xcolor} \pagecolor{yellow} \fi  If \WithPageColor is undefined, the page color setting is ignored. But, it can be defined on the command line: $ pdflatex '\let\WithPageColor=Y\input{myfile}'


Another variant for the hook in the preamble:

\documentclass{...}
...
\providecommand{\PageColorHook}{}
\PageColorHook
...
\begin{document}
\end{document}


Then on the command line:

\$ pdflatex '\newcommand{\PageColorHook}{\usepackage{xcolor}{\pagecolor{yellow}}\input{myfile}'


Yes, that's true: the first input file is used for the name.

How to fix it?

latex -jobname=myfile '\RequirePackage{xcolor}\input{myfile.tex}'


On your system, double quotes may work, on mine they don't.

Why does that happen? When a TeX engine starts with

engine <options> <argument>


(in your case engine is latex)

it looks at what it is fed with. If the <argument> does not start with a backslash, it is considered a file name and essentially the engine executes \input <argument>. Otherwise the engine waits for the first \input instruction and the name of the input file will determine the job name, used for the log file and the output (dvi or PDF).

Note: the \input command is the primitive one.

Since \RequirePackage eventually does \input xcolor.sty, we have that the jobname is set to xcolor because of the rules.

The option -jobname fixes it by disabling the above feature and establishing the jobname at the outset.

There is a big difference between the command line call above and when the input file starts with \RequirePackage. In the latter case, \RequirePackage is seen when a file has already been \input and the jobname decided upon.

• OK, but why it doesn't work it I do: latex -jobname=example '\RequirePackage{xcolor} \AtBeginDocument{\pagecolor{yellow}} \input{example.tex}'? Jun 18, 2017 at 9:44
• Sorry, your answer to my question is correct. I'll need to post another answer to point my problem. Jun 18, 2017 at 9:48
• @GabrieleNicolardi That's a completely different problem. And a bug in the new color drivers for TeX Live 2017. :-( Jun 18, 2017 at 9:51
• @egred Thank you! You're really great! But I'm using Tex Live 2015. Jun 18, 2017 at 9:54