Often, when I am editing a document, I have long sections that I am not working on at the moment. I want to hide such sections from the output document so that I can focus on the relevant part.
I know this can be done with \includeonly or the comment environment (from the verbatim or comment packages). The problem with \includeonly is that it requires moving each section to separate files, which I find too cumbersome. Using a comments environment is better, but I would rather not need to insert \begin{comment} and \end{comment} separated by dozens or even hundreds of lines of code.
How I want to hide a section is simply by adding an option to the section command, such as \section[hide]{My Witty Section Title}. This would hide all of the content until the next \section command. Similarly, \subsection[hide]{My Trite Subsection Title} would hide everything until the next subsection.
A few considerations in the design of the solution:
- I want the section titles to be visible---only the contents are hidden.
- It would be great if hidden labels could still be referenced (similar to
\includeonly). - Things like section references and TOCs should still work.
Edit: First Attempt
I have a first attempt at redefining \section that introduces a comment environment from the verbatim package, but the \endcomment command doesn't work. It seems too, like we'd need a different approach than using a comment environment in order for references from the hidden section to be defined.
\documentclass{article}
\usepackage{etoolbox}
\usepackage{verbatim}
\usepackage{lipsum}
\let\oldsection=\section%
\newcommand\atEndOfSection{(Default end of section)}%
\renewcommand{\section}[2][]{%
\atEndOfSection%
\oldsection{#2}%
\ifstrequal{#1}{hide}%
{% HIDE
% Set \atEndOfSection to end the comment.
\renewcommand{\atEndOfSection}{\endcomment (End of hidden section)}
\comment
}{% DON'T HIDE
% Reset \atEndOfSection.
\renewcommand{\atEndOfSection}{(End of non-hidden section)}
}
}%
\begin{document}
\section{My Section (No Hide)}
\lipsum[1]
\section[hide]{My Section (Hide)}
\lipsum[2]
\section{My Section (No Hide)}
\lipsum[3]
\end{document}

\begin{mysection}{title} ... \end{mysection}. Then you can try to redefine this environment. – Ulrike Fischer Sep 09 '23 at 08:00\start{lrbox}{0}\begin{minipage}{\textwidth} ... \end{minipage}\end{lrbox}. The contents are formatted and never used. (Counters will still be incremented, and it won't save any time.) – John Kormylo Sep 09 '23 at 13:48\tinyand light gray; 2. Change character codes so that nothing is interpreted as character tokens. 3. Use thelrboxapproach but somehow place them in an appendix so they are out of the way; 4. Add another macro argument to\sectionthat gobbles everything until the next\section(à la https://tex.stackexchange.com/a/442913/153678) – Paul Wintz Sep 10 '23 at 05:27filecontentsenvironment to write the section content to an external file, then use\include+\includeonly to selectively display it. – Paul Wintz Sep 10 '23 at 06:43