6

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:

enter image description here

Gabriele
  • 1,815

2 Answers2

6

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.

egreg
  • 1,121,712
  • OK, but why it doesn't work it I do: latex -jobname=example '\RequirePackage{xcolor} \AtBeginDocument{\pagecolor{yellow}} \input{example.tex}'? – Gabriele Jun 18 '17 at 09:44
  • Sorry, your answer to my question is correct. I'll need to post another answer to point my problem. – Gabriele Jun 18 '17 at 09:48
  • @GabrieleNicolardi That's a completely different problem. And a bug in the new color drivers for TeX Live 2017. :-( – egreg Jun 18 '17 at 09:51
  • @egred Thank you! You're really great! But I'm using Tex Live 2015. – Gabriele Jun 18 '17 at 09:54
  • @GabrieleNicolardi See http://tug.org/pipermail/tex-live/2017-June/040357.html and http://tug.org/pipermail/tex-live/2017-June/040359.html – egreg Jun 18 '17 at 09:57
  • @GabrieleNicolardi I get the background color when using TeX Live 2015. – egreg Jun 18 '17 at 10:00
  • I really thank you. I do not get the background color but Heiko Oberdiek solution fixed my problem. There's a "titans fight" here ;-) . You "LaTeX people" are very great! – Gabriele Jun 18 '17 at 10:08
3

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.

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
  \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}'
Heiko Oberdiek
  • 271,626