How to NOT render a part of a document (pure LaTeX)

Just an attempt which may work if everything you want to exclude is within the argument of a command or wrapped in an environment. And if you know exactly what you want to exclude.

The left image is without hiding, the right one is with.

enter image description here enter image description here

The idea is to redefine all commands and environments to swallow the contents of the arguments but still do the counter arithmetic.

\documentclass{article}
\usepackage{environ}
\newtheorem{theorem}{Theorem}
\newenvironment{hide}%
  {\renewcommand\section[1]{\refstepcounter{section}}%
   \renewcommand\subsection[1]{\refstepcounter{subsection}}%
   \RenewEnviron{theorem}{\refstepcounter{theorem}}%
  }%
  {}
\begin{document}
% i want to hide from here ...
\begin{hide}
\section{Section 1}
\subsection{Subsection 1.1}
\begin{theorem}
First
\end{theorem}
\end{hide}
% ... to here
\begin{theorem}
Second
\end{theorem}
\subsection{Subsection 1.2}
\section{Section 2}
\begin{theorem}
Third
\end{theorem}
\end{document}

If you run this document then uncomment the includeonly and run it again, you get a single page document with (just) theorem 2 on page 2.

enter image description here

\begin{filecontents}{zzzz1.tex}
\section{Section 1}
\subsection{Subsection 1.1}
\begin{theorem}
First
\end{theorem}

\end{filecontents}
\begin{filecontents}{zzzz2.tex}
\begin{theorem}
Second
\end{theorem}
\end{filecontents}

\documentclass{article}
\newtheorem{theorem}{Theorem}

%\includeonly{zzzz2}

\begin{document}
% i want to hide from here ...
\include{zzzz1}
% ... to here
\include{zzzz2}
\end{document}

I like gernot's answer, but I'd already started on this before seeing his, so might as well post it:

\documentclass{article}
\newif\ifskipstuff

\skipstufffalse
%\skipstufftrue

\usepackage{amsmath}
\begin{document}

\ifskipstuff
    \refstepcounter{section}
    \refstepcounter{subsection}
    \refstepcounter{equation}
\else
    \section{Section 1}
    \subsection{Subsection 1.1}
    \begin{equation}
        First
    \end{equation}
\fi

\section{A Section}
\subsection{Subsection}
\begin{equation}
    Second
\end{equation}

\end{document}

The drawback is that you need to increase all counters manually, always. Not very nice (particularly if you have a lot of things in the \else clause. Maybe that could be further refined (by including the \ifskipstuff checks inside redefined environments, mayhaps, so that they do their own checking). The upside is that you can globally set if you want to include your material or not by changing between \skipstufffalse and \skipstufftrue, respectively. Well, it might be an upside, depending on your use case.

\skipstufftrue:

skipstufftrue

\skipstufffalse:

skipstufffalse

Tags:

Rendering