# Define a command on xelatex command line

I would like to define the command \qonly on the xelatex command line to be the string chapter07. Later in my LaTeX file I will have an ifdef that will provide the argument to an \includeonly if \qonly is defined.

This works if I am using latexmk:

$latexmk -usepretex='\def\qonly{chapter07}' -pdfxe book.tex -bibtex  But this does not work with xelatex: $ xelatex '\relax\def\qonly{chapter07}' book.tex


It gives me this error:

xelatex '\def\qonly{chapter07}' book.tex
This is XeTeX, Version 3.14159265-2.6-0.999992 (TeX Live 2020) (preloaded format=xelatex)
restricted \write18 enabled.
entering extended mode
LaTeX2e <2020-02-02> patch level 5
L3 programming layer <2020-03-06>

! LaTeX Error: Missing \begin{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...

<*> \def\qonly{chapter07} b
ook.tex
? ^D
! Emergency stop.
...

<*> \def\qonly{chapter07} b
ook.tex
No pages of output.
Transcript written on texput.log.
\$


Is there any way do this?

EDIT: One of the posters was confused why I have the backquotes above. The reason is that I'm using MacOS and my shell is zsh, which interprets the backquotes as a quote mark, so it's necessary to quote it. Here's what happens if I don't provide the backquote:

% xelatex \def\qonly{chapter07} book.tex
This is XeTeX, Version 3.14159265-2.6-0.999992 (TeX Live 2020) (preloaded format=xelatex)
restricted \write18 enabled.
entering extended mode
! I can't find file defqonly{chapter07}'.
<*> defqonly{chapter07}
book.tex
(Press Enter to retry, or Control-D to exit)
Please type another input file name:

• you need to use \input{book} on the command line. – Ulrike Fischer Oct 16 at 16:09
• @UlrikeFischer - brilliant. Can you make this an answer so I can accept it? – vy32 2 days ago
• Why should I add another answer? Werner says the same, accept this. – Ulrike Fischer 2 days ago
• Because I missed that the key difference was book.tex vs. \input{book}. See my comments to @werner's answer. – vy32 2 days ago

You don't need to pass quoted commands to the compiler. Think of whatever follows a call to the compiler as code that should be within a regular TeX file (except if you're passing the tex file itself, as in xelatex book.tex). So, drop the use of the quotes and \input your source:

xelatex \def\qonly{chapter07} \input{book}


Since book.tex is at the end of the input stream, TeX just wants to set it as the character string book.tex, not reading in the file book.tex, hence the use of \input{book}.

• The quotes are for bash. Without them, bash treats the \ as an escape character.... – vy32 2 days ago
• @vy32: Sure. It was not apparent from your post. – Werner 2 days ago
• I clarified it. It was actually in a Makefile, but it runs the commands through bash... – vy32 2 days ago
• my problem was that I had book.tex on the command line rather than \input{book}. Apparently it you provide the filename, it is automatically input, but if you provide TeX commands, you need to do the \input` yourself. – vy32 2 days ago