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:

text that will be written if all is wanted
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.

  • Why not include your files using \include and then at the top of your document your could write \includeonly{<insert files that you want}. – user30471 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 ...? – user30471 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. – user30471 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.


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


to print all comments and

\SelectCommentsToPrint{unset/four}%      stop four from being printed
\SelectCommentsToPrint{unset/all,five}%  turn off all, add five - previously set comments will print

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

Here is the output from the MWE:

enter image description here

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:

\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
\newcommand{\pgfkeysgsetvalue}[2]{\pgfkeys@temptoks{#2}\expandafter\xdef\csname pgfk@#1\endcsname{\the\pgfkeys@temptoks}}

\pgfkeys{/SelectiveComment/.is family,/SelectiveComment,

\newif\ifPrintComment% print comment if true
   \textcolor{blue}{Setting: #1!}% delete when using properly
   \foreach \key in {#1}{\pgfkeys{/SelectiveComment,set/\key} }
  \PrintCommentfalse% comments off by default
  \foreach \key in {#1,all} {
         \ifnum\temp=1\global\PrintCommenttrue\fi% print if key=1

\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
  \SelectCommentsToPrint{unset/four}% stop printing four
  \SelectiveComment[four]{This is four}\hfil? --- NOT printed as four not set
  \SelectCommentsToPrint{four}% restart printing four
  \SelectiveComment[four]{This is four}\hfil--- printed as four set
  \SelectCommentsToPrint{all}% print everything
  \SelectiveComment[five]{This is five}\hfil--- printed as all set
  \SelectCommentsToPrint{unset/all}% revert to printing only selected comments
  \SelectiveComment[six]{This is six}\hfil? --- NOT printed as six not set
     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

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.

| improve this answer | |

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)





\section{First of Second}





% Now with \InputOnCondition




| improve this answer | |
  • 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


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:

enter image description here

\newif\ifALL% \ALLfalse
\newif\ifPARTA% \PARTAfalse
\newif\ifPARTB% \PARTBfalse

    Text that will be written if ALL is wanted.
      Text that will be written if PARTA is wanted.
      Text that will be written if PARTB is wanted.






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.

| improve this answer | |
  • 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. – user30471 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

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