Typeset handout under a different filename











up vote
0
down vote

favorite
1












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).










share|improve this question














bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1




    There are a variety of methods to achieve this. Take a look at tex.stackexchange.com/q/5899/86 (and related questions) to see if any would be suitable for you.
    – Loop Space
    Aug 23 at 16:29















up vote
0
down vote

favorite
1












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).










share|improve this question














bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 1




    There are a variety of methods to achieve this. Take a look at tex.stackexchange.com/q/5899/86 (and related questions) to see if any would be suitable for you.
    – Loop Space
    Aug 23 at 16:29













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





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).










share|improve this question













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).







pgfpages handout






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 23 at 16:03









LMBC

684




684





bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 1




    There are a variety of methods to achieve this. Take a look at tex.stackexchange.com/q/5899/86 (and related questions) to see if any would be suitable for you.
    – Loop Space
    Aug 23 at 16:29














  • 1




    There are a variety of methods to achieve this. Take a look at tex.stackexchange.com/q/5899/86 (and related questions) to see if any would be suitable for you.
    – Loop Space
    Aug 23 at 16:29








1




1




There are a variety of methods to achieve this. Take a look at tex.stackexchange.com/q/5899/86 (and related questions) to see if any would be suitable for you.
– Loop Space
Aug 23 at 16:29




There are a variety of methods to achieve this. Take a look at tex.stackexchange.com/q/5899/86 (and related questions) to see if any would be suitable for you.
– Loop Space
Aug 23 at 16:29










2 Answers
2






active

oldest

votes

















up vote
0
down vote













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



immediatewrite18{pdflatex -jobname=jobname_handoutspacejobname}


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



immediatewrite18{pdflatex -jobname=jobname_handoutspacejobname}
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.






share|improve this answer




























    up vote
    0
    down vote













    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.
    providecommandHandoutmode{01}%<-Not in Handout-Mode

    newcommandHandoutfork{}%
    expandafterletexpandafterHandoutforkexpandafter=%
    csname @ifHandoutmode firstelse secondfi oftwoendcsname

    ifHandoutmode
    % load whatever you need for the handout but don't need for
    % the main document:
    documentclass{article}
    newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
    else
    % load whatever you need for the main document nut not for
    % the main document:
    documentclass{article}
    newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

    ifHandoutmode
    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:



    providecommandHandoutmode{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
    %
    deftestA{_handout}%
    @onelevel@sanitizetestA
    %
    expandafterlongexpandafterdefexpandaftertestB
    expandafter#expandafter1testA{}%
    %
    edeftestA{%
    expandafterexpandafter
    expandafter testB
    expandafterjobname
    testA
    }%
    deftestB{}%
    ifxtestAtestB
    expandafter@firstoftwo
    else
    expandafter@secondoftwo
    fi
    {endgroupprovidecommandHandoutmode{01}}%<-Not in Handout-Mode
    {endgroupprovidecommandHandoutmode{00}}%<-In Handout-Mode

    newcommandHandoutfork{}%
    expandafterletexpandafterHandoutforkexpandafter=%
    csname @ifHandoutmode firstelse secondfi oftwoendcsname

    % (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:
    ifHandoutmodeelse
    % TeXLive-variant, without texify. Perhaps latexmk... Here the option is --jobname:
    % immediatewrite18{pdflatex --jobname=jobname_handout jobname.tex}%
    % MiKTeX-Variant, here the option is --job-name.:
    immediatewrite18{pdflatex --job-name=jobname_handout jobname.tex}%
    % With MiKTeX better might be:
    % IfFileExists{jobname.aux}{}{%
    % immediatewrite18{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.
    %--------------------------------------------------------------------

    ifHandoutmode
    % load whatever you do need for the handout but do not need for
    % the main document:
    documentclass{article}
    newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
    else
    % load whatever you do need for the main document but do not need for
    % the main document:
    documentclass{article}
    newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

    ifHandoutmode
    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
    %
    deftestA{_handout}%
    @onelevel@sanitizetestA
    %
    expandafterlongexpandafterdefexpandaftertestB
    expandafter#expandafter1testA{}%
    %
    edeftestA{%
    expandafterexpandafter
    expandafter testB
    expandafterjobname
    testA
    }%
    deftestB{}%
    %
    ifxtestAtestB
    expandafter@firstoftwo
    else
    expandafter@secondoftwo
    fi
    {endgroupprovidecommandHandoutmode{01}}%<-Not in Handout-Mode
    {endgroupprovidecommandHandoutmode{00}}%<-In Handout-Mode
    %
    newcommandHandoutfork{}%
    expandafterletexpandafterHandoutforkexpandafter=%
    csname @ifHandoutmode firstelse secondfi oftwoendcsname
    %
    SetupKeyvalOptions{family=@currname, prefix=@currname @, setkeys=kvsetkeys}
    DeclareStringOption[no_handout]{platform}%
    DeclareDefaultOption{@unknownoptionerror}%
    ProcessLocalKeyvalOptions{@currname}%

    begingroup
    deftestA{Handout-MiKTeX-texify}%
    ifxtestAAddHandoutBizarrePackageName@platform
    ifHandoutmodeelse
    IfFileExists{jobname.aux}{}{%
    immediatewrite18{texify --pdf --clean --run-viewer --tex-option="--job-name=jobname_handout" jobname.tex}%
    }%
    fi
    else
    deftestA{Handout-MiKTeX}%
    ifxtestAAddHandoutBizarrePackageName@platform
    ifHandoutmodeelse
    immediatewrite18{pdflatex --job-name=jobname_handout jobname.tex}%
    fi
    else
    deftestA{Handout-TeXLive}%
    ifxtestAAddHandoutBizarrePackageName@platform
    ifHandoutmodeelse
    immediatewrite18{pdflatex --jobname=jobname_handout jobname.tex}%
    fi
    else
    deftestA{no_handout}%
    ifxtestAAddHandoutBizarrePackageName@platform
    else
    deftestA{}%
    ifxtestAAddHandoutBizarrePackageName@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,



    ifHandoutmode .. 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 &langle;filename&rangle;.pdf but no no file &langle;filename&rangle;_handout.pdf will be produced.



    If you load the package with option "platform=Handout-MiKTeX-texify", the file &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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 &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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 &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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}

    ifHandoutmode
    % load whatever you do need for the handout but do not need for
    % the main document:
    documentclass{article}
    newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
    else
    % load whatever you do need for the main document but do not need for
    % the handout:
    documentclass{article}
    newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

    ifHandoutmode
    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. ...






    share|improve this answer























    • 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 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 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 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 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 at 21:08













    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "85"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f447357%2ftypeset-handout-under-a-different-filename%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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



    immediatewrite18{pdflatex -jobname=jobname_handoutspacejobname}


    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



    immediatewrite18{pdflatex -jobname=jobname_handoutspacejobname}
    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.






    share|improve this answer

























      up vote
      0
      down vote













      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



      immediatewrite18{pdflatex -jobname=jobname_handoutspacejobname}


      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



      immediatewrite18{pdflatex -jobname=jobname_handoutspacejobname}
      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.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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



        immediatewrite18{pdflatex -jobname=jobname_handoutspacejobname}


        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



        immediatewrite18{pdflatex -jobname=jobname_handoutspacejobname}
        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.






        share|improve this answer












        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



        immediatewrite18{pdflatex -jobname=jobname_handoutspacejobname}


        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



        immediatewrite18{pdflatex -jobname=jobname_handoutspacejobname}
        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 23 at 18:13









        LMBC

        684




        684






















            up vote
            0
            down vote













            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.
            providecommandHandoutmode{01}%<-Not in Handout-Mode

            newcommandHandoutfork{}%
            expandafterletexpandafterHandoutforkexpandafter=%
            csname @ifHandoutmode firstelse secondfi oftwoendcsname

            ifHandoutmode
            % load whatever you need for the handout but don't need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
            else
            % load whatever you need for the main document nut not for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

            ifHandoutmode
            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:



            providecommandHandoutmode{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
            %
            deftestA{_handout}%
            @onelevel@sanitizetestA
            %
            expandafterlongexpandafterdefexpandaftertestB
            expandafter#expandafter1testA{}%
            %
            edeftestA{%
            expandafterexpandafter
            expandafter testB
            expandafterjobname
            testA
            }%
            deftestB{}%
            ifxtestAtestB
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi
            {endgroupprovidecommandHandoutmode{01}}%<-Not in Handout-Mode
            {endgroupprovidecommandHandoutmode{00}}%<-In Handout-Mode

            newcommandHandoutfork{}%
            expandafterletexpandafterHandoutforkexpandafter=%
            csname @ifHandoutmode firstelse secondfi oftwoendcsname

            % (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:
            ifHandoutmodeelse
            % TeXLive-variant, without texify. Perhaps latexmk... Here the option is --jobname:
            % immediatewrite18{pdflatex --jobname=jobname_handout jobname.tex}%
            % MiKTeX-Variant, here the option is --job-name.:
            immediatewrite18{pdflatex --job-name=jobname_handout jobname.tex}%
            % With MiKTeX better might be:
            % IfFileExists{jobname.aux}{}{%
            % immediatewrite18{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.
            %--------------------------------------------------------------------

            ifHandoutmode
            % load whatever you do need for the handout but do not need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
            else
            % load whatever you do need for the main document but do not need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

            ifHandoutmode
            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
            %
            deftestA{_handout}%
            @onelevel@sanitizetestA
            %
            expandafterlongexpandafterdefexpandaftertestB
            expandafter#expandafter1testA{}%
            %
            edeftestA{%
            expandafterexpandafter
            expandafter testB
            expandafterjobname
            testA
            }%
            deftestB{}%
            %
            ifxtestAtestB
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi
            {endgroupprovidecommandHandoutmode{01}}%<-Not in Handout-Mode
            {endgroupprovidecommandHandoutmode{00}}%<-In Handout-Mode
            %
            newcommandHandoutfork{}%
            expandafterletexpandafterHandoutforkexpandafter=%
            csname @ifHandoutmode firstelse secondfi oftwoendcsname
            %
            SetupKeyvalOptions{family=@currname, prefix=@currname @, setkeys=kvsetkeys}
            DeclareStringOption[no_handout]{platform}%
            DeclareDefaultOption{@unknownoptionerror}%
            ProcessLocalKeyvalOptions{@currname}%

            begingroup
            deftestA{Handout-MiKTeX-texify}%
            ifxtestAAddHandoutBizarrePackageName@platform
            ifHandoutmodeelse
            IfFileExists{jobname.aux}{}{%
            immediatewrite18{texify --pdf --clean --run-viewer --tex-option="--job-name=jobname_handout" jobname.tex}%
            }%
            fi
            else
            deftestA{Handout-MiKTeX}%
            ifxtestAAddHandoutBizarrePackageName@platform
            ifHandoutmodeelse
            immediatewrite18{pdflatex --job-name=jobname_handout jobname.tex}%
            fi
            else
            deftestA{Handout-TeXLive}%
            ifxtestAAddHandoutBizarrePackageName@platform
            ifHandoutmodeelse
            immediatewrite18{pdflatex --jobname=jobname_handout jobname.tex}%
            fi
            else
            deftestA{no_handout}%
            ifxtestAAddHandoutBizarrePackageName@platform
            else
            deftestA{}%
            ifxtestAAddHandoutBizarrePackageName@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,



            ifHandoutmode .. 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 &langle;filename&rangle;.pdf but no no file &langle;filename&rangle;_handout.pdf will be produced.



            If you load the package with option "platform=Handout-MiKTeX-texify", the file &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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 &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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 &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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}

            ifHandoutmode
            % load whatever you do need for the handout but do not need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
            else
            % load whatever you do need for the main document but do not need for
            % the handout:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

            ifHandoutmode
            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. ...






            share|improve this answer























            • 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 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 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 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 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 at 21:08

















            up vote
            0
            down vote













            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.
            providecommandHandoutmode{01}%<-Not in Handout-Mode

            newcommandHandoutfork{}%
            expandafterletexpandafterHandoutforkexpandafter=%
            csname @ifHandoutmode firstelse secondfi oftwoendcsname

            ifHandoutmode
            % load whatever you need for the handout but don't need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
            else
            % load whatever you need for the main document nut not for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

            ifHandoutmode
            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:



            providecommandHandoutmode{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
            %
            deftestA{_handout}%
            @onelevel@sanitizetestA
            %
            expandafterlongexpandafterdefexpandaftertestB
            expandafter#expandafter1testA{}%
            %
            edeftestA{%
            expandafterexpandafter
            expandafter testB
            expandafterjobname
            testA
            }%
            deftestB{}%
            ifxtestAtestB
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi
            {endgroupprovidecommandHandoutmode{01}}%<-Not in Handout-Mode
            {endgroupprovidecommandHandoutmode{00}}%<-In Handout-Mode

            newcommandHandoutfork{}%
            expandafterletexpandafterHandoutforkexpandafter=%
            csname @ifHandoutmode firstelse secondfi oftwoendcsname

            % (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:
            ifHandoutmodeelse
            % TeXLive-variant, without texify. Perhaps latexmk... Here the option is --jobname:
            % immediatewrite18{pdflatex --jobname=jobname_handout jobname.tex}%
            % MiKTeX-Variant, here the option is --job-name.:
            immediatewrite18{pdflatex --job-name=jobname_handout jobname.tex}%
            % With MiKTeX better might be:
            % IfFileExists{jobname.aux}{}{%
            % immediatewrite18{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.
            %--------------------------------------------------------------------

            ifHandoutmode
            % load whatever you do need for the handout but do not need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
            else
            % load whatever you do need for the main document but do not need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

            ifHandoutmode
            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
            %
            deftestA{_handout}%
            @onelevel@sanitizetestA
            %
            expandafterlongexpandafterdefexpandaftertestB
            expandafter#expandafter1testA{}%
            %
            edeftestA{%
            expandafterexpandafter
            expandafter testB
            expandafterjobname
            testA
            }%
            deftestB{}%
            %
            ifxtestAtestB
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi
            {endgroupprovidecommandHandoutmode{01}}%<-Not in Handout-Mode
            {endgroupprovidecommandHandoutmode{00}}%<-In Handout-Mode
            %
            newcommandHandoutfork{}%
            expandafterletexpandafterHandoutforkexpandafter=%
            csname @ifHandoutmode firstelse secondfi oftwoendcsname
            %
            SetupKeyvalOptions{family=@currname, prefix=@currname @, setkeys=kvsetkeys}
            DeclareStringOption[no_handout]{platform}%
            DeclareDefaultOption{@unknownoptionerror}%
            ProcessLocalKeyvalOptions{@currname}%

            begingroup
            deftestA{Handout-MiKTeX-texify}%
            ifxtestAAddHandoutBizarrePackageName@platform
            ifHandoutmodeelse
            IfFileExists{jobname.aux}{}{%
            immediatewrite18{texify --pdf --clean --run-viewer --tex-option="--job-name=jobname_handout" jobname.tex}%
            }%
            fi
            else
            deftestA{Handout-MiKTeX}%
            ifxtestAAddHandoutBizarrePackageName@platform
            ifHandoutmodeelse
            immediatewrite18{pdflatex --job-name=jobname_handout jobname.tex}%
            fi
            else
            deftestA{Handout-TeXLive}%
            ifxtestAAddHandoutBizarrePackageName@platform
            ifHandoutmodeelse
            immediatewrite18{pdflatex --jobname=jobname_handout jobname.tex}%
            fi
            else
            deftestA{no_handout}%
            ifxtestAAddHandoutBizarrePackageName@platform
            else
            deftestA{}%
            ifxtestAAddHandoutBizarrePackageName@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,



            ifHandoutmode .. 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 &langle;filename&rangle;.pdf but no no file &langle;filename&rangle;_handout.pdf will be produced.



            If you load the package with option "platform=Handout-MiKTeX-texify", the file &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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 &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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 &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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}

            ifHandoutmode
            % load whatever you do need for the handout but do not need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
            else
            % load whatever you do need for the main document but do not need for
            % the handout:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

            ifHandoutmode
            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. ...






            share|improve this answer























            • 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 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 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 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 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 at 21:08















            up vote
            0
            down vote










            up vote
            0
            down vote









            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.
            providecommandHandoutmode{01}%<-Not in Handout-Mode

            newcommandHandoutfork{}%
            expandafterletexpandafterHandoutforkexpandafter=%
            csname @ifHandoutmode firstelse secondfi oftwoendcsname

            ifHandoutmode
            % load whatever you need for the handout but don't need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
            else
            % load whatever you need for the main document nut not for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

            ifHandoutmode
            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:



            providecommandHandoutmode{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
            %
            deftestA{_handout}%
            @onelevel@sanitizetestA
            %
            expandafterlongexpandafterdefexpandaftertestB
            expandafter#expandafter1testA{}%
            %
            edeftestA{%
            expandafterexpandafter
            expandafter testB
            expandafterjobname
            testA
            }%
            deftestB{}%
            ifxtestAtestB
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi
            {endgroupprovidecommandHandoutmode{01}}%<-Not in Handout-Mode
            {endgroupprovidecommandHandoutmode{00}}%<-In Handout-Mode

            newcommandHandoutfork{}%
            expandafterletexpandafterHandoutforkexpandafter=%
            csname @ifHandoutmode firstelse secondfi oftwoendcsname

            % (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:
            ifHandoutmodeelse
            % TeXLive-variant, without texify. Perhaps latexmk... Here the option is --jobname:
            % immediatewrite18{pdflatex --jobname=jobname_handout jobname.tex}%
            % MiKTeX-Variant, here the option is --job-name.:
            immediatewrite18{pdflatex --job-name=jobname_handout jobname.tex}%
            % With MiKTeX better might be:
            % IfFileExists{jobname.aux}{}{%
            % immediatewrite18{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.
            %--------------------------------------------------------------------

            ifHandoutmode
            % load whatever you do need for the handout but do not need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
            else
            % load whatever you do need for the main document but do not need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

            ifHandoutmode
            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
            %
            deftestA{_handout}%
            @onelevel@sanitizetestA
            %
            expandafterlongexpandafterdefexpandaftertestB
            expandafter#expandafter1testA{}%
            %
            edeftestA{%
            expandafterexpandafter
            expandafter testB
            expandafterjobname
            testA
            }%
            deftestB{}%
            %
            ifxtestAtestB
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi
            {endgroupprovidecommandHandoutmode{01}}%<-Not in Handout-Mode
            {endgroupprovidecommandHandoutmode{00}}%<-In Handout-Mode
            %
            newcommandHandoutfork{}%
            expandafterletexpandafterHandoutforkexpandafter=%
            csname @ifHandoutmode firstelse secondfi oftwoendcsname
            %
            SetupKeyvalOptions{family=@currname, prefix=@currname @, setkeys=kvsetkeys}
            DeclareStringOption[no_handout]{platform}%
            DeclareDefaultOption{@unknownoptionerror}%
            ProcessLocalKeyvalOptions{@currname}%

            begingroup
            deftestA{Handout-MiKTeX-texify}%
            ifxtestAAddHandoutBizarrePackageName@platform
            ifHandoutmodeelse
            IfFileExists{jobname.aux}{}{%
            immediatewrite18{texify --pdf --clean --run-viewer --tex-option="--job-name=jobname_handout" jobname.tex}%
            }%
            fi
            else
            deftestA{Handout-MiKTeX}%
            ifxtestAAddHandoutBizarrePackageName@platform
            ifHandoutmodeelse
            immediatewrite18{pdflatex --job-name=jobname_handout jobname.tex}%
            fi
            else
            deftestA{Handout-TeXLive}%
            ifxtestAAddHandoutBizarrePackageName@platform
            ifHandoutmodeelse
            immediatewrite18{pdflatex --jobname=jobname_handout jobname.tex}%
            fi
            else
            deftestA{no_handout}%
            ifxtestAAddHandoutBizarrePackageName@platform
            else
            deftestA{}%
            ifxtestAAddHandoutBizarrePackageName@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,



            ifHandoutmode .. 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 &langle;filename&rangle;.pdf but no no file &langle;filename&rangle;_handout.pdf will be produced.



            If you load the package with option "platform=Handout-MiKTeX-texify", the file &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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 &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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 &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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}

            ifHandoutmode
            % load whatever you do need for the handout but do not need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
            else
            % load whatever you do need for the main document but do not need for
            % the handout:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

            ifHandoutmode
            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. ...






            share|improve this answer














            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.
            providecommandHandoutmode{01}%<-Not in Handout-Mode

            newcommandHandoutfork{}%
            expandafterletexpandafterHandoutforkexpandafter=%
            csname @ifHandoutmode firstelse secondfi oftwoendcsname

            ifHandoutmode
            % load whatever you need for the handout but don't need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
            else
            % load whatever you need for the main document nut not for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

            ifHandoutmode
            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:



            providecommandHandoutmode{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
            %
            deftestA{_handout}%
            @onelevel@sanitizetestA
            %
            expandafterlongexpandafterdefexpandaftertestB
            expandafter#expandafter1testA{}%
            %
            edeftestA{%
            expandafterexpandafter
            expandafter testB
            expandafterjobname
            testA
            }%
            deftestB{}%
            ifxtestAtestB
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi
            {endgroupprovidecommandHandoutmode{01}}%<-Not in Handout-Mode
            {endgroupprovidecommandHandoutmode{00}}%<-In Handout-Mode

            newcommandHandoutfork{}%
            expandafterletexpandafterHandoutforkexpandafter=%
            csname @ifHandoutmode firstelse secondfi oftwoendcsname

            % (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:
            ifHandoutmodeelse
            % TeXLive-variant, without texify. Perhaps latexmk... Here the option is --jobname:
            % immediatewrite18{pdflatex --jobname=jobname_handout jobname.tex}%
            % MiKTeX-Variant, here the option is --job-name.:
            immediatewrite18{pdflatex --job-name=jobname_handout jobname.tex}%
            % With MiKTeX better might be:
            % IfFileExists{jobname.aux}{}{%
            % immediatewrite18{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.
            %--------------------------------------------------------------------

            ifHandoutmode
            % load whatever you do need for the handout but do not need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
            else
            % load whatever you do need for the main document but do not need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

            ifHandoutmode
            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
            %
            deftestA{_handout}%
            @onelevel@sanitizetestA
            %
            expandafterlongexpandafterdefexpandaftertestB
            expandafter#expandafter1testA{}%
            %
            edeftestA{%
            expandafterexpandafter
            expandafter testB
            expandafterjobname
            testA
            }%
            deftestB{}%
            %
            ifxtestAtestB
            expandafter@firstoftwo
            else
            expandafter@secondoftwo
            fi
            {endgroupprovidecommandHandoutmode{01}}%<-Not in Handout-Mode
            {endgroupprovidecommandHandoutmode{00}}%<-In Handout-Mode
            %
            newcommandHandoutfork{}%
            expandafterletexpandafterHandoutforkexpandafter=%
            csname @ifHandoutmode firstelse secondfi oftwoendcsname
            %
            SetupKeyvalOptions{family=@currname, prefix=@currname @, setkeys=kvsetkeys}
            DeclareStringOption[no_handout]{platform}%
            DeclareDefaultOption{@unknownoptionerror}%
            ProcessLocalKeyvalOptions{@currname}%

            begingroup
            deftestA{Handout-MiKTeX-texify}%
            ifxtestAAddHandoutBizarrePackageName@platform
            ifHandoutmodeelse
            IfFileExists{jobname.aux}{}{%
            immediatewrite18{texify --pdf --clean --run-viewer --tex-option="--job-name=jobname_handout" jobname.tex}%
            }%
            fi
            else
            deftestA{Handout-MiKTeX}%
            ifxtestAAddHandoutBizarrePackageName@platform
            ifHandoutmodeelse
            immediatewrite18{pdflatex --job-name=jobname_handout jobname.tex}%
            fi
            else
            deftestA{Handout-TeXLive}%
            ifxtestAAddHandoutBizarrePackageName@platform
            ifHandoutmodeelse
            immediatewrite18{pdflatex --jobname=jobname_handout jobname.tex}%
            fi
            else
            deftestA{no_handout}%
            ifxtestAAddHandoutBizarrePackageName@platform
            else
            deftestA{}%
            ifxtestAAddHandoutBizarrePackageName@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,



            ifHandoutmode .. 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 &langle;filename&rangle;.pdf but no no file &langle;filename&rangle;_handout.pdf will be produced.



            If you load the package with option "platform=Handout-MiKTeX-texify", the file &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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 &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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 &langle;filename&rangle;.pdf will be produced and it will be attempted to produce &langle;filename&rangle;_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}

            ifHandoutmode
            % load whatever you do need for the handout but do not need for
            % the main document:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{handout}
            else
            % load whatever you do need for the main document but do not need for
            % the handout:
            documentclass{article}
            newcommandDifferingBehaviorBetweenMainDocumentAndHandoutCommand{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.

            ifHandoutmode
            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. ...







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 24 at 13:38

























            answered Aug 23 at 19:20









            Ulrich Diez

            3,765515




            3,765515












            • 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 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 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 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 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 at 21:08




















            • 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 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 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 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 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 at 21:08


















            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 at 19:52




            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 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 at 20:11




            @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 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 at 20:55




            @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 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 at 21:05






            @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 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 at 21:08






            @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 at 21:08




















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f447357%2ftypeset-handout-under-a-different-filename%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt