# How to automatically create multiple versions of a document with different paper sizes & reflecting this in the generated PDF filename?

I am using the moderncv class to prepare my resume for applying to jobs in the US (where the standard paper size is letter), and in UK (where the standard paper size is A4).

Here is a MWE file called main.tex.

% some preamble stuff before documentclass line
% blah blah (preamble)
% more blah blah (preamble)

\documentclass[11pt,a4paper,sans]{moderncv}  % <----- paper size is set here.

% a lot more preamble lines after documentclass line
\moderncvstyle{classic}
\usepackage{graphicx}

\firstname{John}
\familyname{Doe}

\phone[mobile]{123 456 7890}
\email{someone@xyz.com}
\photo[64pt][0.5pt]{example-image-a}

\begin{document}
\makecvtitle
\end{document}


As shown in the MWE, some of my custom preamble precedes the documentclass line, whilst some others comes after it.

I am using latexmk to compile the document with lualatex. How can I maintain just a single tex source and automatically build two different versions of the PDF document each appropriately named Resume_A4paper.pdf and Resume_letterpaper.pdf? I am happy to use any directlua magic and/or external tools to facilitate this.

### Rationale

With the present content, I have manually checked that the page layout is okay for both A4/letter sizes. I just want to automate this process for future minor tweaks (modifying one or two bullet points in the whole CV to tailor to each job description). It would be tedious to manually produce 2 different versions for each type of job/industry I am applying for.

• In my view a CV should be carefully checked for layout changes, so automatically changing paper size is quite a huge step to take. But why don't you simply set up a one-line file, read its content before \documentclass and then pass it as option? – TeXnician Feb 18 at 15:01
• @TeXnician I have manually checked the layout with the present content, and the page layout is okay for both A4/letter sizes. I just want to automate this process for any minor tweaks (modifying one or two bullet points in the whole CV to tailor to each job description). Sorry, but I do not understand your suggestion in the 2nd half of your comment. – Krishna Feb 18 at 15:04
• Why don't put your personal data in another file to input and create two main files with paper size in name and compile both? – Sigur Feb 18 at 15:11
• @Sigur Ok. Can latexmk be used here? Also, is this amenable to the suitable automatic naming of the output PDFs? – Krishna Feb 18 at 15:13
• You say that the content fits on both A4 and letterpaper. If it looks reasonable on both then I don't see any need to produce two different PDF files. – Peter Wilson Feb 18 at 18:45

When on my system I save the following example as test.tex and on the shell (via chdir or cd or whatever) change the active directory to the directory where test.tex is stored, and then compile test.tex by calling latexmk from the shell/command-line via

latexmk -cd -gg -pdflatex="lualatex --shell-escape %O %S" -pdf -dvi- -ps- test.tex

(maybe on your platform you need --enable-write18 instead of --shell-escape)

, then during the first lualatex-run initiated by latexmk, where aux-files don't exist in the beginning, another latexmk-run is initiated where the -jobname-option is used for attaching the phrase -letterpaper to the current expansion of \jobname.

In any case it is checked whether the expansion of the \jobname-primitive contains the phrase -letterpaper.

If so, the option "letterpaper" is passed to the documentclass.

If not so, the option "a4paper" is passed to the documentclass.

As a result you have one latexmk-run on test.tex which exactly once initiates another latexmk-run on test.tex with -jobname="test-letterpaper".

In the end you get test.pdf which is a4paper and test-letterpaper.pdf which is letterpaper.

If you wish, you can provide the -jobname-option on the shell/command-line also and e.g., do

latexmk -cd -gg -jobname="test-a4paper" -pdflatex="lualatex --shell-escape %O %S" -pdf -dvi- -ps- test.tex

(maybe on your platform you need --enable-write18 instead of --shell-escape)

in order to get test-a4paper.pdf and test-letterpaper.pdf.

\makeatletter
\RequirePackage{shellesc}% more recent releases of LuaTeX don't provide
% \write18 any more. This package makes sure
% under such LuaTeX \write18 is emulated by
% calls to \directlua.
\newcommand\CheckWhetherNull[1]{%
\if\relax\detokenize{#1}\relax
\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}%
\newcommand\letterpaperphrase{-letterpaper}\@onelevel@sanitize\letterpaperphrase
\newcommand\Afourpaperphrase{-a4paper}\@onelevel@sanitize\Afourpaperphrase
%
\newcommand\gobbletoletterpaperphrase{}%
\expandafter\long\expandafter\def
\expandafter\gobbletoletterpaperphrase\expandafter#\expandafter1\letterpaperphrase{}%
%
\newcommand\gobbletoAfourpaperphrase{}%
\expandafter\long\expandafter\def
\expandafter\gobbletoAfourpaperphrase\expandafter#\expandafter1\Afourpaperphrase{}%
%
\newcommand\keepBeforeAfourpaperphrase{}%
\expandafter\long\expandafter\def
\expandafter\keepBeforeAfourpaperphrase
\expandafter#\expandafter1\Afourpaperphrase#2\relax{#1}%
%
\newcommand\CheckWhetherJobNameHasLetterpaperphrase{}%
\expandafter\def
\expandafter\CheckWhetherJobNameHasLetterpaperphrase
\expandafter{%
\romannumeral0\expandafter\CheckWhetherNull\expandafter{%
\romannumeral0\@firstofone{\expandafter\expandafter\expandafter} %
\expandafter\gobbletoletterpaperphrase
\romannumeral0\@firstofone{\expandafter\expandafter\expandafter} %
\expandafter\jobname
\letterpaperphrase
}%
{ \@secondoftwo}%
{ \@firstoftwo}%
}%
\newcommand\CheckWhetherJobNameHasAfourpaperphrase{}%
\expandafter\def
\expandafter\CheckWhetherJobNameHasAfourpaperphrase
\expandafter{%
\romannumeral0\expandafter\CheckWhetherNull\expandafter{%
\romannumeral0\@firstofone{\expandafter\expandafter\expandafter} %
\expandafter\gobbletoAfourpaperphrase
\romannumeral0\@firstofone{\expandafter\expandafter\expandafter} %
\expandafter\jobname
\Afourpaperphrase
}%
{ \@secondoftwo}%
{ \@firstoftwo}%
}%
\CheckWhetherJobNameHasLetterpaperphrase{%
\PassOptionsToClass{letterpaper}{moderncv}%
}{%
\PassOptionsToClass{a4paper}{moderncv}%
}%
\IfFileExists{\jobname.tex}{%
\newcommand\myinfile{\jobname.tex}%
}{%
\newcommand\myinfile{%
\CheckWhetherJobNameHasAfourpaperphrase
{\expandafter\keepBeforeAfourpaperphrase\jobname\relax}%
{\jobname}.tex%
}%
}%
\newcommand\createletterdocument{%
\ShellEscape{%
latexmk -cd
-gg
-jobname="\CheckWhetherJobNameHasAfourpaperphrase
{\expandafter\keepBeforeAfourpaperphrase\jobname\relax}%
{\jobname}%
\letterpaperphrase"
-pdflatex="lualatex \@percentchar O \@percentchar S"
-pdf -dvi- -ps-
\myinfile
}%
}%
\AtBeginDocument{%
\begingroup
\immediate\write\@mainaux{\string\global\string\let\string\createletterdocument=\string\empty}%
\endgroup
\CheckWhetherJobNameHasLetterpaperphrase{}{\createletterdocument}%
}%
\makeatother

% some preamble stuff before documentclass line
% blah blah (preamble)
% more blah blah (preamble)

\documentclass[11pt,sans]{moderncv}  % <----- paper size is _not_
% set here but is set via \PassOptionsToClass.

\makeatletter
\@ifundefined{pdfpagewidth}{}{\pdfpagewidth=\paperwidth}%
\@ifundefined{pdfpageheight}{}{\pdfpageheight=\paperheight}%
\@ifundefined{pagewidth}{}{\pagewidth=\paperwidth}%
\@ifundefined{pageheight}{}{\pageheight=\paperheight}%
\makeatother

% a lot more preamble lines after documentclass line
\moderncvstyle{classic}
\usepackage{graphicx}

\firstname{John}
\familyname{Doe}