0

I have a latex document that I sometimes want to print in "handout" form, specifically four pages in one. I've been using

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

in the preamble, thus printing 4 pages in one, which works just fine. By commenting out these commands in the preamble, I am able to typeset either the "normal" version of my file or the "handout" version.

My question: knowing that the source file's name is filename.tex, is there an easy way of typesetting into a pdf file called filename_handout.pdf rather than filename.pdf?

Currently, I need to go to the folder containing the output file filename.pdf and manually change the file name. I was hoping I could save that step since I need to create "handout" versions of many of my files.

In case it matters, I use TeXShop on a Mac (and I have MikTeX installed).

1

Thank you @LoopSpace for your suggestion. I followed the link you provided and various links in there. I was unable to find a solution that addresses the request above. The proposed solutions require working with several files or LaTeXing from the command line, none of which I want to do. The only solution that comes close to what I am looking for is to include in the preamble the command

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

This produces an output file filename_handout.pdf in addition to filename.pdf (assuming the source is filename.tex). I have not found a way of preventing filename.pdf from being created. Still, I find that this solution is better than nothing (and the alternatives I looked at). Specifically, by selectively commenting out the code

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

in the preamble, I can toggle on and off the creation of a 4 in 1 handout. The only problem is that, when this code is on, typesetting it also creates the same handout in the file filename.pdf, so I always need to end the process by typesetting the document with the above commands commented out. In this way, I am able to produce two files, filename.pdf and filename_handout.pdf, from a single source file.

0

Let's say, you want your non-handout-document as filename.pdf and you want your handout-document as filename_handout.pdf.

Maintain both versions via the .tex-source file filename.tex but compile filename.tex only for obtaining the non-handout-document whose name will be filename.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}

For obtaining the handout as filename_handout.pdf maintain in the same folder/directory and compile another .tex-input file whose name is filename_handout.tex and whose content consists of two lines:

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

With the two examples, filename.pdf will be:

enter image description here

And filename_handout.pdf will be:

enter image description here


Of course you can automatize the creating of filename_handout.pdf without an extra filename_handout.tex via \write18-calls to filename.pdf with option
--job-name=\jobname_handout (MiKTeX) respective
--jobname=\jobname_handout (TeXLive).

When doing this, you can, e.g., evaluate whether the expansion of \jobname has a trailing phrase _handout and define your handout-forking-macros accordingly.

I did not see this proposition of evaluating \jobname in the other threads.

By the way:

As with each variant of the document you might need several LaTeX-runs for compiling it, while it isn't ensured that with each variant of the document you need the same amount of LaTeX-runs for compiling it, I would, in case of MiKTeX being available, not call the pdflatex -program but the texify-program with the --pdf-option and with passing on to the pdflatex-program the option --job-name=\jobname_handout via texify's --tex-option as
--tex-option="--job-name=\jobname_handout".

I would do that \write18-call to texify only during the first LaTeX-run on filename.tex when not in handout-mode.

Detecting the first LaTeX run can probably be done by checking the existence of the aux-file(s).

Here comes the code.

(If you wish, you can put some of the code into a package which you can load via \RequirePackage or via \input before forking what documentclass to use etc.)

%--------------------------------------------------------------------
% 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}

A starting point for a separate package AddHandout as mentioned above could be something like this—I decided to give the package a bizarre name because I do not intend to develop/maintain it any further and therefore I did not want to block a "good" package name:

AddHandoutBizarrePackageName.sty:

%% This is file `AddHandoutBizarrePackageName.sty',
%%
%% Copyright (C) 2018 by Ulrich Diez <ud.usenetcorrespondence@web.de>
%%
%% 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

You can load the package via \RequirePackage before writing any other component of the preamble.

For forking depending on whether currently producing the handout or the main document,

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

and

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

are available.

If you load the package without any option, only the file ⟨filename⟩.pdf but no no file ⟨filename⟩_handout.pdf will be produced.

If you load the package with option "platform=Handout-MiKTeX-texify", the file ⟨filename⟩.pdf will be produced and it will be attempted to produce ⟨filename⟩_handout.pdf by means of "\write18`-calls suitable for the texify-program of the TeX-Distribution "MiKTeX".

If you load the package with option "platform=Handout-MiKTeX", the file ⟨filename⟩.pdf will be produced and it will be attempted to produce ⟨filename⟩_handout.pdf by means of "\write18`-calls suitable for the pdflatex-program of the TeX-Distribution "MiKTeX".

If you load the package with option "platform=Handout-TeXLive", the file ⟨filename⟩.pdf will be produced and it will be attempted to produce ⟨filename⟩_handout.pdf by means of "\write18`-calls suitable for the pdflatex-program of the TeX-Distribution "TeXLive".

This way you can within one single \RequirePackage-line of the document specify if you wish to also obtain a handout.
Besides this you have facilities for both in the preamble and in the document forking depending on whether currently the main document or the handout is typeset.

Usage-example, e.g., filename.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}

In any case you use the same .tex-input-code for creating different variants of a document.

Be aware that you need to pay special attention in case the .tex-input-code is also used for having something done where randomness plays a role.

E.g., having via \pgfrandom created some numbers at random and henceforth having LaTeX use these random numbers within the document that is about to be created.

When compiling the one variant of the document, the random-number-generator might produce random values that differ from those produced by the random-number-generator when compiling the other variant of the document. Thus in such a scenario the content of the two documents might not be identical any more. ...

  • Thank you for your suggestion @UlrichDiez. @LoopSpace also links to a variety of solutions that are based on the idea of having multiple source files as you suggest. What would be really neat is to have a system whereby you can produce a filename.pdf file or filename_handout.pdf file from the same source file, simply by toggling on and off some text in the preamble. For this to happen, the only thing that I am missing is to change the pdf output file name from within the tex source file – LMBC Aug 23 '18 at 19:52
  • @LMBC Searching on this site, my conclusion is that it is impossible to change the pdf file name from within the tex source. By the time the tex source starts being parsed, the pdf file name is set and cannot be changed. – Loop Space Aug 23 '18 at 20:11
  • @LoopSpace In this link masu claims that it can be done, but I could not get this to work, even when typesetting from the command line. – LMBC Aug 23 '18 at 20:55
  • @LMBC I have now edited my answer and added an example where both filename.pdf and filename_handout.pdf will be created from the same source without an additional .tex-input file. But you need \write18-feature. You said you have MiKTeX. If you have MiKTeX' texify available I would do a \write18-call on texify rather than on the pdflatex-program. Details in the that part of my answer that I have just added. With the example, <jobname>_handout.pdf will be created in any case. Of course you can nest the \writ18-call into something like \IfIWantHandout..\else..\fi. – Ulrich Diez Aug 23 '18 at 21:05
  • @LoopSpace Yes: It is impossible to change the pdf-file from within a latex-run. Redefining \jobname does not change file-names but just turn \jobname from the primitive into some macro... But using \write18 you can call LaTeX again, on the most TeX-platforms with some jobname-option for telling it what names to use for creating .log-file, .pdf-file, .aux-files etc... – Ulrich Diez Aug 23 '18 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.