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}
\address{83 Fancy Avenue}{Noweheresville}{Gotham City 24061}
\phone[mobile]{123 456 7890}
\email{someone@xyz.com}
\photo[64pt][0.5pt]{example-image-a}
\begin{document}
\makecvtitle
\end{document}
\documentclassand then pass it as option? – TeXnician Feb 18 '19 at 15:01latexmkbe used here? Also, is this amenable to the suitable automatic naming of the output PDFs? – Dr Krishnakumar Gopalakrishnan Feb 18 '19 at 15:13pdflatex main-a4.tex main-letter.texfor example. – Sigur Feb 18 '19 at 15:17latexmkfor this purpose? – Dr Krishnakumar Gopalakrishnan Feb 18 '19 at 15:30latexmk. – Sigur Feb 18 '19 at 15:32