1

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
\define@cmdkeys{head}[head@]{level,label,role}{}
\presetkeys{head}{%
  label        = \@empty,
  role         = \@empty,
}{}
 \newcommand{\genHead}[1]{
    \@nameuse{\head@level}{#1}
}
\newcommand{\Headi}[2][]{
    \setkeys{head}{#1, level=section}
    \genHead{#2}
}
\newcommand{\Headii}[2][]{
    \setkeys{head}{#1, level=subsection}
    \genHead{#2}
}
\makeatother
\begin{document}
\def\currentrole{first} % defining what to stay
\Headi[role=first]{Section title to stay}
text
\Headi[role=second]{Section to disappear}
more text
  \Headii{subsection should disappear}
   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
1

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
\define@cmdkeys{head}[head@]{level,label,role}{}
\presetkeys{head}{%
  label        = \@empty,
  role         = \relax,
}{}
 \newcommand{\genHead}[1]{
    \@nameuse{\head@level}{#1}
}
\newif\ifrole@commentsection
\newif\ifrole@commentsubsection
\newcommand{\applyCommented}{%
    \csname ifrole@comment\head@level\endcsname%
        \clearpage%
        \global\csname role@comment\head@level false\endcsname%
    \fi%
    \ifx\head@role\currentrole%
    \else%
        \expandafter\ifx\head@role\relax%
        \else%
            \clearpage%
            \global\csname role@comment\head@level true\endcsname%
        \fi%
    \fi%
}
\newcommand{\Headi}[2][]{%
    \setkeys{head}{#1, level=section}%
    \applyCommented%
    \genHead{#2}%
}
\newcommand{\Headii}[2][]{%
    \setkeys{head}{#1, level=subsection}%
    \applyCommented%
    \genHead{#2}%
}
\AtBeginShipout{%
    \ifrole@commentsection%
        \AtBeginShipoutDiscard%
    \else\ifrole@commentsubsection%
        \AtBeginShipoutDiscard%
    \fi\fi%
}
\makeatother
\begin{document}
\def\currentrole{first} % defining what to stay
\Headi[role=first]{Section title to stay}
text
\Headi[role=second]{Section to disappear}
more text
  \Headii{subsection should disappear}
   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
\define@cmdkeys{head}[head@]{level,label,role}{}
\let\shipout@bak\shipout
\presetkeys{head}{%
  label        = \@empty,
  role         = \relax,
}{}
 \newcommand{\genHead}[1]{
    \@nameuse{\head@level}{#1}
}
\newif\ifrole@commentsection
\newif\ifrole@commentsubsection
\let\refstepcounterbak\refstepcounter
\let\stepcounterbak\stepcounter
\newcommand{\applyCommented}{%
    \csname ifrole@comment\head@level\endcsname%
        \ifcsname tmp@pgcount\head@level\endcsname%
            \setcounter{page}{\csname tmp@pgcount\head@level\endcsname}%
            \setcounter{table}{\csname tmp@tabcount\head@level\endcsname}%
            \setcounter{figure}{\csname tmp@figcount\head@level\endcsname}%
            \setcounter{equation}{\csname tmp@eqcount\head@level\endcsname}%
        \fi%
        \let\refstepcounter\refstepcounterbak
        \let\stepcounter\stepcounterbak
        \clearpage%
        \global%
        \expandafter\global\csname role@comment\head@level false\endcsname%
    \fi%
    \ifx\head@role\currentrole%
    \else%
        \expandafter\ifx\head@role\relax%
        \else%
            \global\expandafter\newcount\csname tmp@pgcount\head@level\endcsname%
            \global\csname tmp@pgcount\head@level\endcsname=\value{page}%
            \global\expandafter\newcount\csname tmp@tabcount\head@level\endcsname%
            \global\csname tmp@tabcount\head@level\endcsname=\value{table}%
            \global\expandafter\newcount\csname tmp@figcount\head@level\endcsname%
            \global\csname tmp@figcount\head@level\endcsname=\value{figure}%
            \global\expandafter\newcount\csname tmp@eqcount\head@level\endcsname%
            \global\csname tmp@eqcount\head@level\endcsname=\value{equation}%
            \clearpage%
            \let\refstepcounter\relax
            \let\stepcounter\relax
            \expandafter\global\csname role@comment\head@level true\endcsname%
        \fi%
    \fi%
}
\newcommand{\Headi}[2][]{
    \setkeys{head}{#1, level=section}%
    \applyCommented%
    \genHead{#2}
}
\newcommand{\Headii}[2][]{
    \setkeys{head}{#1, level=subsection}%
    \applyCommented%
    \genHead{#2}
}
\AtBeginShipout{%
    \ifrole@commentsection%
        \AtBeginShipoutDiscard%
    \else\ifrole@commentsubsection%
        \AtBeginShipoutDiscard%
    \fi\fi%
}
\makeatother
\begin{document}
\def\currentrole{first} % defining what to stay
\Headi[role=first]{Section title to stay}
text
\Headi[role=second]{Section to disappear}
more text
  \Headii{subsection should disappear}
   sub text
\Headi{this one should turn back on}
text
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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