# change section to comment

I have some sections that I want to not show up under certain conditions. I know about the comment package, but that requires a begin/end to open and close the environment.

What I'd like is to have a switch for a section that would make it (and possibly subsections of the section) into a comment.

I have a document like this:

\documentclass{memoir}
\usepackage{xkeyval}
\makeatletter
label        = \@empty,
role         = \@empty,
}{}
}
}
}
\makeatother
\begin{document}
\def\currentrole{first} % defining what to stay
text
more text
sub text
\Headi{this one should turn back on}
text
\end{document}


In my real doc I do more processing and have more keys (in case you wonder why I go to all the trouble of overriding section creation).

So can I do anything to make a complete section into a comment?

• How about putting it inside a \iffalse...\fi? – Skillmon May 2 '17 at 15:04
• that would certainly work, as would a begin/end comment environment around the section, but that means altering the tex files (which I want to avoid) and instead have latex/tex code to do the job for me. – Tim A May 2 '17 at 15:06
• Should it be another optional key to the \Head-commands or how do you want this to work? – Skillmon May 2 '17 at 15:09
• The role= is the key--in my example, \currentrole is set to 'first', so the first section would appear since it matches, the second section would not, and the third section would appear since no role is defined. – Tim A May 2 '17 at 15:16

If putting a \clearpage when a sectioning command with the wrong role appears is okay for you, the below code should do what you want with the help of the atbegshi-package. Note that all counters you increase inside a section which has the wrong role still are increased, so using a table or figure inside those sections might result in unwanted behavior regarding the labels of those environments. This includes the pagenumber.

If a \clearpage is no good, you might have a look at the comment-package which has the useful comment functions \includecomment{versiona}\excludecomment{versionb}. The usage of these might need a retouch to your files but only to put a \begin{versiona}...\end{versiona} or \begin{versionb}...\end{versionb} around the sections which should be in the corresponding versions. After that you would only have to use the \includecomment/\excludecomment macros to turn them on/off.

\documentclass{memoir}
\usepackage{xkeyval}
\usepackage{atbegshi}
\makeatletter
label        = \@empty,
role         = \relax,
}{}
}
\newcommand{\applyCommented}{%
\clearpage%
\fi%
\else%
\else%
\clearpage%
\fi%
\fi%
}
\applyCommented%
}
\applyCommented%
}
\AtBeginShipout{%
\fi\fi%
}
\makeatother
\begin{document}
\def\currentrole{first} % defining what to stay
text
more text
sub text
\Headi{this one should turn back on}
text
\end{document}


EDIT: If you want the counters to stay the same, the below code does use additional counts to revert the counters to the correct states (applying to figure, table, page and equation). You might add more counters if you need them (and want to use this code instead of the comment-package).

\documentclass{memoir}
\usepackage{xkeyval}
\usepackage{atbegshi}
\makeatletter
\let\shipout@bak\shipout
label        = \@empty,
role         = \relax,
}{}
}
\let\refstepcounterbak\refstepcounter
\let\stepcounterbak\stepcounter
\newcommand{\applyCommented}{%
\fi%
\let\refstepcounter\refstepcounterbak
\let\stepcounter\stepcounterbak
\clearpage%
\global%
\fi%
\else%
\else%
\clearpage%
\let\refstepcounter\relax
\let\stepcounter\relax
\fi%
\fi%
}
\applyCommented%
}
\applyCommented%
}
\AtBeginShipout{%
\fi\fi%
}
\makeatother
\begin{document}
\def\currentrole{first} % defining what to stay