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.



\newcommand\atEndOfSection{(Default end of section)}%
      {% HIDE 
      % Set \atEndOfSection to end the comment.
      \renewcommand{\atEndOfSection}{\endcomment (End of hidden section)}
      }{% DON'T HIDE 
      % Reset \atEndOfSection.
      \renewcommand{\atEndOfSection}{(End of non-hidden section)}


\section{My Section (No Hide)}

\section[hide]{My Section (Hide)}

\section{My Section (No Hide)}



enter image description here

  • If you don't want to use sub-documents., for the output ... nothing better that limit the range of pages in the printer options. Make whatever in a section content is complicated because is not an argument, nor an environment. However, not for the output, but for the edition, there are LaTeX editors that can show interactive TOCs in real time, or open/close sections contents without touch your source code. With a GUI, LyX not only show a navigable ToC but you can also modify it to reorganize the contents without copy and paste.
    – Fran
    Sep 9, 2023 at 6:58
  • Hiding part of a document with arbitrary content is not trivial. Half hiding them (but keeping labels and toc entries) even less (there is a reason why the second is done traditionally with explicit files which write their own aux-files). If you really want this, the first thing you should do is to change your input syntax and to define a simple environment that allows to write sections consequently as \begin{mysection}{title} ... \end{mysection}. Then you can try to redefine this environment. Sep 9, 2023 at 8:00
  • The problem, as you have noticed, is turning off hiding once your start. I would use \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.) Sep 9, 2023 at 13:48
  • See also tex.stackexchange.com/questions/610570/… Sep 9, 2023 at 13:53
  • Hmm... that approach doesn't support referencing labels unless you do the Tikz hack that you describe in tex.stackexchange.com/a/610573/153678.
    – Paul Wintz
    Sep 10, 2023 at 5:20


You must log in to answer this question.

Browse other questions tagged .