如何自动创建具有不同纸张尺寸的文档的多个版本并将其反映在生成的 PDF 文件名中?

如何自动创建具有不同纸张尺寸的文档的多个版本并将其反映在生成的 PDF 文件名中?

我正在使用该moderncv课程来准备我的简历,以便申请美国(标准纸张尺寸为letter)和英国(标准纸张尺寸为A4)的工作。

这里有一个MWE名为 的文件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}

\address{83 Fancy Avenue}{Noweheresville}{Gotham City 24061} 
\phone[mobile]{123 456 7890}
\email{[email protected]}
\photo[64pt][0.5pt]{example-image-a}


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

如图所示MWE,我的一些自定义序言位于documentclass行之前,而另一些位于行之后。

我正在使用latexmk来编译文档lualatex。我如何才能保持单身的 tex并自动生成两个不同版本的 PDF 文档恰当地命名 Resume_A4paper.pdf并且Resume_letterpaper.pdf?我很乐意使用任何directlua魔法和/或外部工具来实现这一点。

基本原理

对于目前的内容,我已手动检查页面布局是否适合 A4/letter 尺寸。我只想让这个过程自动化,以便将来进行细微调整(修改整个简历中的一两个要点以适应每个职位描述)。为我申请的每种类型的工作/行业手动制作两个不同的版本会很繁琐。

答案1

在我的系统上,我将以下示例保存为测试.tex并在 shell 上(通过chdircd或其他方式)将活动目录更改为目录测试.tex存储,然后编译测试.tex通过调用latexmk通过 shell/命令行

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

(也许在你的平台上你需要的--enable-write18是 而不是--shell-escape

,然后在第一个路拉泰克斯-run 发起者latexmk,其中辅助文件在开始时不存在,另一个latexmk-run 启动,其中-jobname-option 用于将短语附加-letterpaper到 的当前扩展\jobname

无论如何,都会检查\jobname-primitive的扩展是否包含短语-letterpaper

如果是的话,选项“letterpaper”就会被传递给文档类。

如果不是这样,则选项“a4paper”将传递给文档类。

因此你有一个latexmk-运行测试.tex恰好一次引发另一次latexmk-运行测试.tex-jobname="test-letterpaper"

最后你得到测试.pdf这是 A4 纸,测试信纸.pdf也就是信纸。

如果你愿意,你-jobname也可以在 shell/命令行上提供 -option,例如,

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

(也许在你的平台上你需要的--enable-write18是 而不是--shell-escape

为了得到测试-a4试卷.pdf测试信纸.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{[email protected]}
\photo[64pt][0.5pt]{example-image-a}


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

相关内容