[Tex/LaTex] 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.
(./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
No file xcolor.aux.
Non-PDF special ignored!
Non-PDF special ignored!
Non-PDF special ignored!{/usr/local/texlive/2015/texmf-var/fonts/map/pdftex/upd
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
Output written on xcolor.pdf (2 pages, 22276 bytes).
Transcript written on xcolor.log.

and this is the screenshot:

enter image description here

Best Answer

Driver mismatch

The file myfile.tex is broken:

tmp$ latex

latex is initialized to generate DVI as output format.


Package xcolor is loaded with DVI driver dvips. Fine.

But then:

Non-PDF special ignored!

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.

Loading a package on the command line

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

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:


Then on the command line:

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