使用不同的文件名排版讲义

使用不同的文件名排版讲义

我有一个 latex 文档,有时我想以“讲义”形式打印,具体来说是四页合一。我一直在使用

\usepackage{pgfpages}
\pgfpagesuselayout{4 on 1}

在序言中,这样一次打印 4 页,效果很好。通过在序言中注释掉这些命令,我​​可以排版文件的“普通”版本或“讲义”版本。

我的问题是:知道源文件的名称是,是否有一种简单的方法可以排版成名为的而不是 的filename.texpdf 文件?filename_handout.pdffilename.pdf

目前,我需要转到包含输出文件的文件夹filename.pdf并手动更改文件名。我希望可以省去这一步,因为我需要为许多文件创建“讲义”版本。

为了以防万一,我在 Mac 上使用 TeXShop(并且安装了 MikTeX)。

答案1

感谢@LoopSpace的建议。我按照你提供的链接和其中的各种链接进行操作。我找不到解决上述请求的解决方案。建议的解决方案需要处理多个文件或从命令行进行 LaTeXing,而这些我都不想做。唯一接近我想要的解决方案是在前言中包含命令

\immediate\write18{pdflatex -jobname=\jobname_handout\space\jobname}

这将产生一个输出文件filename_handout.pdf 此外filename.pdf(假设源是filename.tex)。我还没有找到阻止创建的方法filename.pdf。不过,我发现这个解决方案比没有(以及我研究过的替代方案)要好。具体来说,通过有选择地注释掉代码

\immediate\write18{pdflatex -jobname=\jobname_handout\space\jobname}
\usepackage{pgfpages}
\pgfpagesuselayout{4 on 1}

在序言中,我可以打开和关闭 4 合 1 讲义的创建。唯一的问题是,当此代码打开时,排版也会在文件 filename.pdf 中创建相同的讲义,因此我总是需要通过排版文档并注释掉上述命令来结束该过程。这样,我就可以生成两个文件,filename.pdf并且filename_handout.pdf来自单个源文件

答案2

假设您希望您的非讲义文档为文件名.pdf并且您希望您的讲义文档为文件名_讲义.pdf

通过 .tex-source 文件维护两个版本文件名.tex但编译文件名.tex仅用于获取非分发文件,其名称将为文件名.pdf

% \providecommand does define a command only in case it is 
% not already defined.
\providecommand\Handoutmode{01}%<-Not in Handout-Mode

\newcommand\Handoutfork{}%    
\expandafter\let\expandafter\Handoutfork\expandafter=%
\csname @\if\Handoutmode first\else second\fi oftwo\endcsname

\if\Handoutmode
  % load whatever you need for the handout but don't need for
  % the main document:
  \documentclass{article}
  \newcommand\DifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
\else
  % load whatever you need for the main document nut not for
  % the main document:
  \documentclass{article}
  \newcommand\DifferingBehaviorBetweenMainDocumentAndHandoutCommand{main document}
\fi

\begin{document}

You are looking at the \DifferingBehaviorBetweenMainDocumentAndHandoutCommand.

This is text that is both in the main document and in the handout.

\Handoutfork{This is Text that goes into the handout only.}%
            {This is Text that goes into the main document only.}

This is text that is both in the main document and in the handout.

\if\Handoutmode
\begin{verbatim}
Verbatim text in handout.
\end{verbatim}
\else
\begin{verbatim}
Verbatim text in main document.
\end{verbatim}
\fi

This is text that is both in the main document and in the handout.

\end{document}

获取讲义如下文件名_讲义.pdf维护在同一个文件夹/目录中并编译另一个名为 .tex-input 文件文件名_讲义.tex其内容由两行组成:

\providecommand\Handoutmode{00}%<-In Handout-Mode
\input filename.tex

通过这两个例子,文件名.pdf将:

在此处输入图片描述

文件名_讲义.pdf将:

在此处输入图片描述


当然,你可以自动创建文件名_讲义.pdf无需额外文件名_讲义.tex通过\write18调用文件名.pdf使用选项
--job-name=\jobname_handout(MiKTeX)分别
--jobname=\jobname_handout(TeXLive)。

当这样做时,您可以例如评估的扩展是否\jobname有尾随短语_handout,并相应地定义您的 handout-forking-macros。

\jobname我在其他帖子中没有看到这个评估建议。

顺便一提:

对于文档的每个变体,你可能需要多次运行 LaTeX 来编译它,虽然不能确保对于文档的每个变体,你都需要相同数量的 LaTeX 运行来编译它,但如果有 MiKTeX 可用,我不会调用pdflatex-程序,但特克化-program 与--pdf-option 一起传递给pdflatex--job-name=\jobname_handout-通过编程选项特克化--tex-option
--tex-option="--job-name=\jobname_handout"

我会这样做 \write18-call 到特克化仅在第一次运行 LaTeX 时文件名.tex当不处于讲义模式时。

通过检查辅助文件是否存在,可能就能检测出第一次 LaTeX 运行。

下面是代码。

(如果您愿意,您可以将一些代码放入包中,然后在分叉要使用的文档类之前通过\RequirePackage或通过加载该包。)\input

%--------------------------------------------------------------------
% The following lines could go into a separate package that is to be
% loaded via \input or (in case of also having package options)
% via \RequirePackage before writing any other preamble-thinglie like
% \documantclass...
%--------------------------------------------------------------------
% Check whether the current \jobname has a trailing phrase "_handout"
% and set \Handoutmde accordingly.
\begingroup
\makeatletter
%
\def\testA{_handout}%
\@onelevel@sanitize\testA
%
\expandafter\long\expandafter\def\expandafter\testB
\expandafter#\expandafter1\testA{}%
%
\edef\testA{%
  \expandafter\expandafter
  \expandafter            \testB
  \expandafter\jobname
  \testA
}%
\def\testB{}%
\ifx\testA\testB
   \expandafter\@firstoftwo
\else
  \expandafter\@secondoftwo
\fi
{\endgroup\providecommand\Handoutmode{01}}%<-Not in Handout-Mode
{\endgroup\providecommand\Handoutmode{00}}%<-In Handout-Mode

\newcommand\Handoutfork{}%    
\expandafter\let\expandafter\Handoutfork\expandafter=%
\csname @\if\Handoutmode first\else second\fi oftwo\endcsname

% (If you put the code into a package by itself, you can create a
% package option for deciding whether to create or not to create
% the handout.)
%
% Create the handout:
\if\Handoutmode\else
% TeXLive-variant, without texify. Perhaps latexmk... Here the option is --jobname:
% \immediate\write18{pdflatex --jobname=\jobname_handout \jobname.tex}%
% MiKTeX-Variant, here the option is --job-name.:
\immediate\write18{pdflatex --job-name=\jobname_handout \jobname.tex}%
% With MiKTeX better might be:
% \IfFileExists{\jobname.aux}{}{%
%   \immediate\write18{texify --pdf --clean --run-viewer --tex-option="--job-name=\jobname_handout" \jobname.tex}%
% }%
\fi
%--------------------------------------------------------------------
% From here on, things cannot go into a separate package any
% more.
%--------------------------------------------------------------------

\if\Handoutmode
  % load whatever you do need for the handout but do not need for
  % the main document:
  \documentclass{article}
  \newcommand\DifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
\else
  % load whatever you do need for the main document but do not need for
  % the main document:
  \documentclass{article}
  \newcommand\DifferingBehaviorBetweenMainDocumentAndHandoutCommand{main document}
\fi

\begin{document}

You are looking at the \DifferingBehaviorBetweenMainDocumentAndHandoutCommand.

This is text that is both in the main document and in the handout.

\Handoutfork{This is Text that goes into the handout only.}%
            {This is Text that goes into the main document only.}

This is text that is both in the main document and in the handout.

\if\Handoutmode
\begin{verbatim}
Verbatim text in handout.
\end{verbatim}
\else
\begin{verbatim}
Verbatim text in main document.
\end{verbatim}
\fi

This is text that is both in the main document and in the handout.

\end{document}

单独包的起点添加讲义如上所述,可能是这样的——我决定给这个包起一个奇怪的名字,因为我不打算进一步开发/维护它,因此我不想阻止一个“好的”包名称:

添加讲义BizarrePackageName.sty

%% This is file `AddHandoutBizarrePackageName.sty',
%%
%% Copyright (C) 2018 by Ulrich Diez <[email protected]>
%%
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.3 of this license
%% or (at your option) any later version.  The latest version of this
%% license is in:
%%
%%    http://www.latex-project.org/lppl.txt
%%
%% and version 1.3 or later is part of all distributions of LaTeX version
%% 2003/12/01 or later.
%%
%% This file forms a "work" according to the terms of the 
%% LaTeX Project Public License.
%%
%% This "work" is not maintained.
%%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{AddHandoutBizarrePackageName}[2018/08/23 V.0.1beta -- A \write18-hook for handout-compilation]
\RequirePackage{kvoptions}

% Check whether the current \jobname has a trailing phrase "_handout"
% and set \Handoutmde accordingly.
\begingroup
%
\def\testA{_handout}%
\@onelevel@sanitize\testA
%
\expandafter\long\expandafter\def\expandafter\testB
\expandafter#\expandafter1\testA{}%
%
\edef\testA{%
  \expandafter\expandafter
  \expandafter            \testB
  \expandafter\jobname
  \testA
}%
\def\testB{}%
%
\ifx\testA\testB
   \expandafter\@firstoftwo
\else
  \expandafter\@secondoftwo
\fi
{\endgroup\providecommand\Handoutmode{01}}%<-Not in Handout-Mode
{\endgroup\providecommand\Handoutmode{00}}%<-In Handout-Mode
%
\newcommand\Handoutfork{}%    
\expandafter\let\expandafter\Handoutfork\expandafter=%
\csname @\if\Handoutmode first\else second\fi oftwo\endcsname
%
\SetupKeyvalOptions{family=\@currname, prefix=\@currname @, setkeys=\kvsetkeys}
\DeclareStringOption[no_handout]{platform}[]%
\DeclareDefaultOption{\@unknownoptionerror}%
\ProcessLocalKeyvalOptions{\@currname}%

\begingroup
\def\testA{Handout-MiKTeX-texify}%
\ifx\testA\AddHandoutBizarrePackageName@platform
  \if\Handoutmode\else
    \IfFileExists{\jobname.aux}{}{%
      \immediate\write18{texify --pdf --clean --run-viewer --tex-option="--job-name=\jobname_handout" \jobname.tex}%
    }%
  \fi
\else
  \def\testA{Handout-MiKTeX}%
  \ifx\testA\AddHandoutBizarrePackageName@platform
    \if\Handoutmode\else
      \immediate\write18{pdflatex --job-name=\jobname_handout \jobname.tex}%
    \fi
  \else
    \def\testA{Handout-TeXLive}%
    \ifx\testA\AddHandoutBizarrePackageName@platform
      \if\Handoutmode\else
        \immediate\write18{pdflatex --jobname=\jobname_handout \jobname.tex}%
      \fi
    \else
      \def\testA{no_handout}%
      \ifx\testA\AddHandoutBizarrePackageName@platform
      \else
        \def\testA{}%
        \ifx\testA\AddHandoutBizarrePackageName@platform
          \PackageWarningNoLine{\@currname}{%
             You need tp specify a value for the option "platform". 
             Handout will not be created%
          }%
        \else
          \PackageWarningNoLine{\@currname}{%
            The platform '\AddHandoutBizarrePackageName@platform'
            is unknown. Handout will not be created%
          }%
        \fi
      \fi
    \fi
  \fi
\fi
\endgroup

\endinput

您可以在编写序言的任何其他组件之前通过 \RequirePackage 加载该包。

根据当前是否生成讲义或主文档进行分叉,

\if\Handoutmode .. \else .. \fi  

 \Handoutfork{This is Text that goes into the handout only.}%
             {This is Text that goes into the main document only.}%

可用。

如果你加载包时没有任何选项,则只有文件⟨文件名⟩.pdf但没有文件⟨文件名⟩_handout.pdf将会生产。

如果你使用选项“platform=Handout-MiKTeX-texify”加载包,则文件⟨文件名⟩.pdf将被生产,并将尝试生产⟨文件名⟩_handout.pdf通过“\write18”调用适合特克化-TeX-Distribution“MiKTeX”的程序。

如果你使用选项“platform=Handout-MiKTeX”加载包,则文件⟨文件名⟩.pdf将被生产,并将尝试生产⟨文件名⟩_handout.pdf通过“\write18”调用适合pdflatex-TeX-Distribution“MiKTeX”的程序。

如果你使用选项“platform=Handout-TeXLive”加载包,则文件⟨文件名⟩.pdf将被生产,并将尝试生产⟨文件名⟩_handout.pdf通过“\write18”调用适合pdflatex-TeX-Distribution“TeXLive”的程序。

这样,您可以在文档的一行内\RequirePackage指定是否还希望获得讲义。
除此之外,您还可以在序言和文档分叉中使用此功能,具体取决于当前是主文档还是讲义已排版。

使用示例,例如,文件名.tex

%      Produce only <filename>.pdf, no <filename>_handout.pdf:
%\RequirePackage{AddHandoutBizarrePackageName}
%      Produce only <filename>.pdf, no <filename>_handout.pdf:
%\RequirePackage[platform=no_handout]{AddHandoutBizarrePackageName}%
%      Produce <filename>.pdf and try to produce <filename>_handout.pdf 
%      via \write18 calls suitable for MiKTeX' texify:
\RequirePackage[platform=Handout-MiKTeX-texify]{AddHandoutBizarrePackageName}
%      Produce <filename>.pdf and try to produce <filename>_handout.pdf 
%      via \write18 calls suitable for MiKTeX' pdflatex:
%\RequirePackage[platform=Handout-MiKTeX]{AddHandoutBizarrePackageName}
%      Produce <filename>.pdf and try to produce <filename>_handout.pdf 
%      via \write18 calls suitable for TeXLive' pdflatex:
%\RequirePackage[platform=Handout-TeXLive]{AddHandoutBizarrePackageName}

\if\Handoutmode
  % load whatever you do need for the handout but do not need for
  % the main document:
  \documentclass{article}
  \newcommand\DifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
\else
  % load whatever you do need for the main document but do not need for
  % the handout:
  \documentclass{article}
  \newcommand\DifferingBehaviorBetweenMainDocumentAndHandoutCommand{main document}
\fi

\begin{document}

You are looking at the \DifferingBehaviorBetweenMainDocumentAndHandoutCommand.

This is text that is both in the main document and in the handout.

\Handoutfork{This is Text that goes into the handout only.}%
            {This is Text that goes into the main document only.}

This is text that is both in the main document and in the handout.

\if\Handoutmode
\begin{verbatim}
Verbatim text in handout.
\end{verbatim}
\else
\begin{verbatim}
Verbatim text in main document.
\end{verbatim}
\fi

This is text that is both in the main document and in the handout.

\end{document}

无论如何,您都可以使用相同的 .tex-input-code 来创建文档的不同变体。

请注意,如果 .tex-input-code 也用于完成某些需要随机性才能发挥作用的事情,则需要特别注意。

例如,通过\pgfrandom随机创建一些数字,然后让 LaTeX 在即将创建的文档中使用这些随机数。

在编译文档的一个版本时,随机数生成器可能会生成与编译文档的另一个版本时随机数生成器生成的随机值不同的随机值。因此,在这种情况下,两个文档的内容可能不再相同。...

相关内容