# How to easily write conditional pages? [closed]

Is there any easy way to make latex write only some of the pages?

My current idea is to make a function using the ifthen-package in the following way:

\newcommand{\WhatDoYouWantToWrite}[1]{
\ifthenelse{\equal{#1}{All}}{
text that will be written if all is wanted
}
\ifthenelse{\equal{#1}{PartA}{
text that will be written if part A is wanted
}
}
{Wrong input chosen. Don't know what to write}


This works, but now my problem is that if I'm changing the text I have to do it several places, which is frustrating. Then my idea was to make a separate file and use \input, but how can I then use the \ifthenelse-statement?

I have tried using the comment-package, but then I have to keep moving around in the text turning on and off \begin{comment} \end{comment}. Can it be used in a clever way in this setup?

It looked like the exam class, where one can choose if one wants to show solutions of problems, but then there is only two options of output, with or without solutions. I want more!

My hope is to just use one function in the beginning of the document which turns on/off the parts that I want/don't want.

## closed as unclear what you're asking by user31729, egreg, user13907, Mensch, Sean AllredJun 11 '15 at 19:04

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Why not include your files using \include and then at the top of your document your could write \includeonly{<insert files that you want}. – Andrew May 28 '15 at 8:32
• @Andrew: The (undesireful) feature of \include is that it always uses a new page. This is perhaps not desired by the OP – user31729 May 28 '15 at 8:48
• This is possible but it would be easier to do what you want if you gave slightly more detail. Do you want 5 options, or 10 or ...? – Andrew Jun 11 '15 at 15:49
• I think it's in OP's best interest if he tells us why this is needed. As someone who's taken advantage of TeX's scriptability, I feel like there's a better way to go about this. – Sean Allred Jun 11 '15 at 19:04
• User7919, there are now three answers to your question. Do any of them do what you want? Please clarify. – Andrew Jun 12 '15 at 6:11

Here is a flexible solution using pgfkeys that allows for arbitrary switches that are given as keys to the command/environment. I define two (user) commands \SelectCommentsToPrint and \SelectiveComment. The first command controls which comments will be printed and the second command defines the comments and specifies under what conditions they should be printed. Both commands take arbitrary "keys". For example.

\SelectCommentsToPrint{one,three,five}


specifies that comments labeled "one", "three" or "five" should be printed and all other comments should be ignored. After this a comment can be given as

\SelectiveComment[one,four,eleven]{This is a silly comment}


and this comment will be printed only when any one of "one", "four" and "eleven" have been set using \SelectCommentsToPrint. At any point you can issue another \SelectCommentsToPrint to add extra comments to print. You can also use

\SelectCommentsToPrint{all}


\SelectCommentsToPrint{unset/four}%      stop four from being printed


to stop "four" etc from being printed. There is also a SelectiveEnvironment environment for printing longer comments, this uses the environ package. The syntax is similar:

\begin{SelectiveEnvironment}[one,four]% print if one or four is set
some nice stuff
\end{SelectiveEnvironment}


Here is the output from the MWE:

If a comment is not printed in the MWE then a ? is printed instead. The remarks after the --- say why something was printed or not printed. The words in blue indicate when the printing of comments is turned on or off.

Here is the code:

\documentclass{article}
\usepackage{pgf,pgffor}
\usepackage{environ}
\usepackage{xcolor}% only needed for highlighting selections in MWE

% https://tex.stackexchange.com/questions/15204/is-there-a-way-to-set-a-global-key-value-using-pgfkeys
\makeatletter
\newcommand{\pgfkeysgsetvalue}[2]{\pgfkeys@temptoks{#2}\expandafter\xdef\csname pgfk@#1\endcsname{\the\pgfkeys@temptoks}}
\makeatother

\pgfkeys{/SelectiveComment/.is family,/SelectiveComment,
set/.unknown/.code={
\pgfkeysgsetvalue{/SelectiveComment/\pgfkeyscurrentname}{1}
},
set/unset/.unknown/.code={
\pgfkeysgsetvalue{/SelectiveComment/\pgfkeyscurrentname}{0}
}
}

\newif\ifPrintComment% print comment if true
\textcolor{blue}{Setting: #1!}% delete when using properly
\foreach \key in {#1}{\pgfkeys{/SelectiveComment,set/\key} }
}
\newcommand\MakeSelection[1]{
\foreach \key in {#1,all} {
\pgfkeysifdefined{/SelectiveComment/\key}%
{\pgfkeysgetvalue{/SelectiveComment/\key}{\temp}
\ifnum\temp=1\global\PrintCommenttrue\fi% print if key=1
}{}
}
}

\newcommand\SelectiveComment[2][]{\MakeSelection{#1}\ifPrintComment#2\fi}
\NewEnviron{SelectiveEnvironment}[1][]{\MakeSelection{#1}\ifPrintComment\BODY\fi }

\begin{document}\obeylines% onlyto decrease space taken up by MWE
\SelectCommentsToPrint{one, two, four}% print one, two or four
\SelectiveComment[one]{This is one}\hfil--- printed as one set
\SelectiveComment[two]{This is two}\hfil--- printed as two set
\SelectiveComment[three]{This is three}\hfil? --- NOT printed as three not set
\SelectiveComment[three, four]{This is three/four}\hfil--- printed as four set
\SelectiveComment[four]{This is four}\hfil? --- NOT printed as four not set
\SelectiveComment[four]{This is four}\hfil--- printed as four set
\SelectiveComment[five]{This is five}\hfil--- printed as all set
\SelectiveComment[six]{This is six}\hfil? --- NOT printed as six not set
\begin{SelectiveEnvironment}[one,four]
Inside an environment with one/four
\end{SelectiveEnvironment}\hfil--- printed as one and four set
\begin{SelectiveEnvironment}[three,six]% will NOT  print environment
Inside an environment with three/six
\end{SelectiveEnvironment}\hfil? --- NOT printed as three and six not set
\end{document}


The command \SelectCommentsToPrint calls keys inside /SelectiveComment/set that, in turn, sets the key /SelectiveComment/<key> equal to 1. The unset commands calls /SelectiveComment/set/unset and this then sets /SelectiveComment/<key> equal to 0. I did it this way to stop the keys from printing their values when they are being set and unset. The actual printing is controlled by the switch \ifPrintComment. By default this is off and all the keys do is issue \PrintCommenttrue if one of the keys for the comment is set to 1.

Both the /SelectiveComment/set and /SelectiveComment/set/unset keys are controlled using the .unknown key handler. This allows arbitrary key names to be used. I use a hack of Martin Scharrer's to set all of the pgfkeys globally.

The \textcolor{blue}{Setting: #1!} line in the definition of \SelectCommentsToPrint is only there to make the output from the MWE easier to understand. It should be removed if you want to use this.

The question is a little bit vague, but I tried to provide a solution, showing the \include(only) method and a \InputOnCondition way, where external files are fetched using \input. (The \InputOnCondition command can be extended of course, but the best would be to use a naming scheme for external files such that \InputOnCondition generates the file name in its test branches)

\documentclass{book}

\usepackage{blindtext}
\usepackage{ifthen}
\newcommand{\InputOnCondition}[1]{%
\ifthenelse{\equal{#1}{All}}{%
\input{mychap1}%
\input{mychap2}%
}{%
\ifthenelse{\equal{#1}{partA}}{%
\input{mychap1}%
}{%
\input{mychap2}%
}%
}%
}

\begin{filecontents}{mychap1.tex}

\chapter{First}
\section{First}
\blindtext[10]
\end{filecontents}

\begin{filecontents}{mychap2.tex}
\chapter{Second}
\section{First of Second}
\subsection{First}
\blindtext[5]
\end{filecontents}

\includeonly{mychap2}

\begin{document}

\tableofcontents

\include{mychap1}
\include{mychap2}

% Now with \InputOnCondition

\InputOnCondition{All}

\InputOnCondition{partA}

\InputOnCondition{partB}

\end{document}

• could you not \input{\ifthenelse{…}}? (or you might need to \expandafter\input\expandafter{\ifthenelse{…}…) – Sean Allred Jun 11 '15 at 19:02

Your approach here is to avoid having

\WhatDoYouWantToWrite{All}


throughout the document and then having to change every All to PartA (or something else) at a later stage. It's better to not pass an argument at all, and set something at the global level.

Here is one such approach using TeX \if-statements:

\documentclass{article}
\newif\ifALL% \ALLfalse
\newif\ifPARTA% \PARTAfalse
\newif\ifPARTB% \PARTBfalse

\newcommand{\ConditionalWrite}{%
\ifALL
Text that will be written if ALL is wanted.
\else
\ifPARTA
Text that will be written if PARTA is wanted.
\fi
\ifPARTB
Text that will be written if PARTB is wanted.
\fi
\fi
}

\begin{document}

\ALLtrue
\ConditionalWrite

\ALLfalse
\PARTAtrue
\ConditionalWrite

\PARTAfalse
\PARTBtrue
\ConditionalWrite

\end{document}


You can set \<cond>true or \<cond>false in the preamble. The example above just shows how the selection is conditioned.

The same can be done using etoolbox toggles.

• The \ifAll seems redundant. More than this, by making this a separate switch it is necessary to type all text govnerned by "All" twice. If using this approach rather than defining \ifAll it would be better to drop the \ifAll and type \PARTAtrue\PARTBtrue etc at the top of the document instead. – Andrew Jun 12 '15 at 10:03
• @Andrew: Sure. I show one application of the conditionals that echoed the OP's intent. Other suggestions and configurations are also possible. – Werner Jun 12 '15 at 16:25