1

I have a file containing a number of custom environments numbered by a counter "exercisesheetnumber". I would like to use a second counter "printonly" to hide all instances of the environment but the one given in "printonly".

This may be related to the question Hide custom environment content based on boolean, but I could not figure out how to single out one of the environments which should not be hidden.

Soenke
  • 13

1 Answers1

1

Well, this is built up on an answer to Hide proof environment for restructuring. I gotta go now, I'll comment the code later.

\documentclass{article}

\newcounter{exsheetnumber}
\newcounter{onlysheet}

% this is (moreorless) whatever environment defition you want
\newenvironment{innerexsheet}{
  \paragraph{Sheet no.\@ \arabic{exsheetnumber}}
}{\textit{End of sheet.}}

\makeatletter
% `killcontents` environment removes all its contents
\usepackage{environ}
\NewEnviron{killcontents}{}
% `xkillcontents` does the same as `killcontents`
% and additionally it restores the correct definition of `exsheet`
\newenvironment{xkillcontents}
  {\killcontents}
  {\endkillcontents
    \global\let\exsheet\origexsheet
    \global\let\endexsheet\endorigexsheet
  }
% the environment `exsheet` itself:
\newenvironment{exsheet}{
  \stepcounter{exsheetnumber}
  \ifnum\value{onlysheet}=\value{exsheetnumber}
    % if we wabt to display the environment, we just use it
    \innerexsheet
    % and we use its end as well
    \let\endexsheet\endinnerexsheet
    % this is for the rest to work
    \let\reserved@a\relax
  \else
    % if we don't want to display anything, we step out of `exsheet`,
    \end{exsheet}
    % then we re-define `exsheet` to be `xkillcontents`
    \let\exsheet\xkillcontents
    \let\endexsheet\endxkillcontents
    % and we step in `exsheet` again, just we have to do it after `\fi`
    \def\reserved@a{\begin{exsheet}}
  \fi
  \reserved@a
}{}
% and we save the correct definition of `exsheet` as `origexsheet`
\let\origexsheet\exsheet
\let\endorigexsheet\endexsheet
\makeatother

\begin{document}

\setcounter{onlysheet}{5}

\begin{exsheet} ONE \end{exsheet}
\begin{exsheet} TWO \end{exsheet}
\begin{exsheet} THREE \end{exsheet}
\begin{exsheet} FOUR \end{exsheet}
\begin{exsheet} FIVE \end{exsheet}
\begin{exsheet} SIX \end{exsheet}
\begin{exsheet} SEVEN \end{exsheet}

\end{document}
yo'
  • 51,322
  • Based on your code I cooked up a similar solution using a boolean test to change how \NewEnviron handles the body, but this didn't work due to restrictions on what can be in the body (e.g. verbatim environments seemed to give problems). Is there a way to do what you did in a more Latex-like manner?. I know very little Tex and would like to understand what I do. Is \reserved@a just a "variable"? Thanks! – Soenke Nov 04 '13 at 15:09
  • You can use \thisismyrandomlynamedcontrolsequence instead of \reserved@a, and it's advised to use your own command name for this. I use that one as a habit. Some things can't be easily done in "purely LaTeX way" and it's simpler to "hack" it :) – yo' Nov 05 '13 at 12:42