0

Consider the following document (test.tex):

\documentclass{article}
\usepackage{blindtext}

\newif \ifoption
\optiontrue                                            % (*)

\begin{document}
\ifoption
% Print some blind text
\blindtext
\else
% Print lots of blind text
\Blindtext
\fi
\end{document}

As-is, it prints a paragraph of blind text. If the indicated line is changed to \optionfalse, it prints several paragraphs of blind text instead.

I would like to configure my compiler task (i.e. what happens when I press Ctrl+T in TeXworks) to produce three PDF files at once:

  • One called test-optiontrue.pdf that has only one paragraph of blind text
  • One called test-optionfalse.pdf that has several paragraphs of blind text
  • One called test.pdf that matches the setting that actually appears in test.tex

Is this possible? For example, is there a way to modify the call to pdfLaTeX so that it compiles the document once as-is, then overrides the \if with each of the alternative values and compiles those cases too?

If this is not possible, what is my alternative? The most important thing is that the body of my document is already written in the \ifoption ... \else ... \fi format, so I would strongly prefer a solution that only requires modifying the header of my document.

Babel is not an option because it is incompatible with the other packages I am using.

1

2 Answers 2

1

Solution for pdflatex on Unix-based systems

Gather the \newif declaration one line, say line 3, of the test.tex:

\documentclass{article}
\usepackage{blindtext}
\newif\ifoption\optiontrue         % Line 3

\begin{document}
\ifoption
% Print some blind text
\blindtext
\else
% Print lots of blind text
\Blindtext
\fi
\end{document}

You can put this early in the document, even before the \documentclass{article}, so that its line number will never have to change.

Next, create a shell script compile.sh in the same directory as test.tex with the following contents:

sed '3 c\
\\newif\\ifoption\\optiontrue' test.tex  | pdflatex -jobname=test-optiontrue
sed '3 c\
\\newif\\ifoption\\optionfalse' test.tex | pdflatex -jobname=test-optionfalse

The sed command replaces line 3 with the appropriate \newif declaration, and then we pipe this to pdflatex. The -jobname option gives you different filenames. Note the use of the escape character \.

Now you can run bash compile.sh from a terminal to produce the desired output.

Obviously, using Babel is preferable to this hacky solution.

0

Reading Max's answer, I am totally unfamiliar with Babel, as it relates to Latex. So, you have to consider this response, in that light.

The stated problem specifically requires a solution that can be invoked in conjunction with Texworks. A case may be made that the reason that you have encountered this problem is that the entire approach of using a text editor like Texworks for Latex documents is flawed.

This is a very controversial assertion. Many, perhaps even most users are better off with canned software, like Texworks. The alternative approach is to use a programmer's editor instead, such as Vi/Vim or Emacs. These editors are virtually infinitely programmable, to any (bizarre) internal word processing task that you might want. Further, Emacs is definitely available in (for example) a Windows operating system environment as well as in Linux. My guess is that some variant of Vi/Vim is similarly available. I also guess that some analogous programmer's editor would be available in the Apple operating system environment.

The whole point of this response is to allow various readers to make an informed decision. That is, suppose that you are using canned software rather than a programmer's editor. As you encounter challenges like the one in this posting, you can consider whether to stay with (for example) Texworks, and use a somewhat inelegant solution, for this lone situation. The alternative is to invest the significant time/energy into researching which programmer's editor that you are going to commit to, and then tackling the corresponding learning curve.


The remainder of this response uses the specific challenge presented by the OP (i.e. original poster) as an illustration of how the situation may be handled when using a programmer's editor.

Pdflatex permits command line options, such as jobname, for specifying the output file name. You can google on "pdflatex jobname" to find pertinent discussion/examples.

When researching your posting, I tried to determine if the corresponding line from Max's answer could be overridden/set on the Pdflatex command line:

\newif\ifoption\optiontrue

My cursory research was inconclusive. Research and experimentation is needed here. Part of the research would probably involve googling "pdflatex set document options".

Suppose that everything can be controlled from the pdflatex command line. The programmer's editor (e.g. Emacs, Vi/Vim) will programmatically know the filename, and will be able to programmatically format and invoke the pdflatex command line. Further, the programmer's editor (i.e. Emacs anyway) allows a gui interface where you customize the menubar that is present when the document has the *.tex rather than *.txt extension.

Assuming that the desired blind text option can be set on the pdflatex command line, then you can invoke the desired processsing, creating 3 separate pdf files, each output file appropriately named, by simply mouse-clicking the menu option that you created.


The remainder of this response discusses how to use the programmer's editor to handle a situation that exceeds the boundary of what can be handled by pdflatex command line options. The specific challenge presented by the OP will be used as an example.

Note
I have never actually used the following approach. So I may not be specifying the Latex syntax correctly. However, the idea will be obvious.

I would alter the strategy that the OP used to craft test.tex, as follows:

...
\usepackage{blindtext}
\newboolean{blindtext}
\setboolean{blindtext}{false}
...
% Editor-flag-1
% \setboolean{blindtext}{true}
\if{\boolean{blindtext}}{\blindtext-1}
\setboolean{blindtext}{false} 
...
% Editor-flag-2
% \setboolean{blindtext}{true}
\if{\boolean{blindtext}}{\blindtext-2}
\setboolean{blindtext}{false} 
...   

I am actually unfamiliar with both the blindtext package and the creation and use of booleans in a latex document. In the above example, blindtext-1 and blind-text-2 are intended to symbolize that you would be invoking different paragraphs of text at that place in the printing.

Within the programmer's editor (i.e. in Emacs anyway) it is a simple matter to create a very customized function and invoke the function from a customized menu bar, inside a gui. You could (for example) have a function that does the following:

  • Interrogate a variable internal to the programmer's editor to determine the source file name (e.g. test.tex).

  • Find the string "Editor-flag-1".
    Go to the next line.
    Uncomment that line.
    Save the file.
    Invoke Pdflatex, specifying the source file name and Jobname.
    Re-comment the line.

  • Find the string "Editor-flag-2".
    Go to the next line.
    Uncomment that line.
    Save the file.
    Invoke Pdflatex, specifying the source file name and Jobname.
    Re-comment the line.

  • Save the file.
    Invoke Pdflatex, specifying the source file name and Jobname.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .