14

This is a follow-up to How can I automate the workflow for producing multiple versions of a document?

Here is toy example of my problem. I want to automate the generation of two slightly different versions of a document. Each one should be generated using latexmk. In one version, no class option should be used:

\documentclass{article}
\begin{document}
foo
\end{document}

In the other, some class option should be used, e.g.:

\documentclass[twocolumn]{article}
\begin{document}
foo
\end{document}

Of course, I could

  1. compile once without the class option,
  2. edit my .tex file and manually add the class option,
  3. compile a second time,

but this approach lacks automation. Alternatively, I could use two files:

% withoutoption.tex
\documentclass{article}
\begin{document}
foo
\end{document}

and

% withoption.tex
\PassOptionsToClass{twocolumn}{article}
\input{withoutoption.tex}

but, ideally, I'd like to only have one file to maintain, and pass class options from outside the .tex file (in a makefile). From Martin Scharrer's answer, I know I can pass a class option without having to edit the file, like so:

pdflatex "\PassOptionsToClass{twocolumn}{article}\input{foo}"

I'd like to use latexmk instead of pdflatex, though, and I can't figure out how the approach above would translate to latexmk. I must admit I'm not very familiar with the latexmk syntax, but I've scanned the documentation, and it appears latexmk can only accept a filename, not an argument of the form

"\PassOptionsToClass{twocolumn}{article}\input{foo}"

Did I miss something? Can latexmk be used in this fashion?

jub0bs
  • 58,916
  • What about the -latex="..." argument? You could pass that with an hard-coded call to latex with the appropriate arguments...I think latexmk "needs" files is that it tries to detect changes, if you onlyrun it once this should be fine... – Bordaigorl Oct 09 '14 at 13:06
  • @Bordaigorl Could you please elaborate in an answer? – jub0bs Oct 09 '14 at 13:15

1 Answers1

15

You could use the -latex or -pdflatex options. I'll use pdflatex for the examples but it should work with latex as well.

latexmk -pdf article

will look for article.tex, invoke the command associated with the pdflatex option which by default is

"pdflatex -interaction=nonstopmode -synctex=1 %S %O"

where %S is replaced by the filename and %O by the options.

So to produce the twocolumn version you could run

latexmk -pdf -pdflatex='pdflatex %O -interaction=nonstopmode -synctex=1 "\PassOptionsToClass{twocolumn}{article}\input{%S}"' article

and you would find the twocolumns version in article.pdf. The nice thing is that you can create an alias for latexmk -pdf -pdflatex='pdflatex -interaction=nonstopmode -synctex=1 "\PassOptionsToClass{twocolumn}{article}\input{%S}" %O' and run it on different tex files to obtain the twocolumn version of each.

I moved %O before the other options because this seems to enable using -jobname=name as an option of latexmk (see comments).

Now this is not enough because if you then re run latexmk -pdf article without making changes to article.tex, latexmk would fail to recognise the options have changed and will not recompile the document. So to force that you can add the -g option.

Bordaigorl
  • 15,135
  • Let me try that :) – jub0bs Oct 09 '14 at 13:30
  • Great. Exactly what I wanted. There is only one weird thing. If I want to use a different jobname (foo, say), I have to use -jobname=foo in two different places:latexmk -jobname=foo -pdf -pdflatex='pdflatex -jobname=foo -interaction=nonstopmode -synctex=1 "\PassOptionsToClass{twocolumn}{article}\input{%S}" %O' article. Otherwise, I get an error saying (Pdf)LaTeX failed to generate the expected log file 'foo.log'. – jub0bs Oct 09 '14 at 14:23
  • @Jubobs strange, passing -jobname=foo to latexmk only seems to work for me (version 4.35 on Ubuntu)...I think passing that would forward it to the command via %O. – Bordaigorl Oct 09 '14 at 14:30
  • although using it into the -pdflatex argument would allow you to use it as -jobname=twocols-%S – Bordaigorl Oct 09 '14 at 14:31
  • Using %O earlier seems to do the trick: I got it to work with latexmk -jobname=foo -pdf -pdflatex='pdflatex %O -interaction=nonstopmode -synctex=1 "\PassOptionsToClass{twocolumn}{article}\input{%S}"' article – jub0bs Oct 09 '14 at 14:36
  • Yes, should have told you I tried with %O as first arg! – Bordaigorl Oct 09 '14 at 14:39
  • No worries. Thanks for the help! Getting more familiar with latexmk is now on my "laundry list". – jub0bs Oct 09 '14 at 14:42
  • Thanks again. You can expect a bounty :) – jub0bs Oct 09 '14 at 15:56
  • glad I could help! =D – Bordaigorl Oct 09 '14 at 16:01