One way to accomplish this could be to typeset the stuff inside a box which you
never print. This does however not work for any content. In the below example
floats are not allowed and throw an error.
\documentclass[]{article}
\begin{document}
This shows up
\thesection% here it is 0
\setbox0\vbox{This is evaluated but never shows up, additional spaces
however might be a problem, which is why I put the \% at the end of the
lines. To show that it's evaluated, I run this: \stepcounter{section}}% no space here
\thesection% here it is 1
\end{document}
The following does also evaluate the contents, but only \AtEndDocument, so if
you need the contents to be evaluated where you put those, don't use this. The
advantage is, that anything should work here which doesn't produce an error.
\documentclass[]{article}
\usepackage{environ,atbegshi}
\makeatletter
\newcommand*\contents@DontShowMe{}
\newcommand\addto@DontShowMe[1]
{%
\xdef\contents@DontShowMe{\unexpanded\expandafter{\contents@DontShowMe#1}}%
}
\NewEnviron{DontShowMe}[1][1]
{%
\ifnum#1=0
\expandafter\BODY
\else
\expandafter\addto@DontShowMe\expandafter{\BODY}%
\fi
}
\AtEndDocument
{%
\clearpage
\AtBeginShipout{\AtBeginShipoutDiscard}%
\contents@DontShowMe
}
\makeatother
\begin{document}
This shows up
\thesection% here it is 0
\begin{DontShowMe}
This code doesn't show up and is evaluated at the end of the document. To show
this: \stepcounter{section}
\begin{figure}test\end{figure}
To produce an error to check that it does evaluate uncomment this: %\PackageError{foo}{foo}{foo}
\end{DontShowMe}
\thesection% here it is still 0
\end{document}
One environment to rule them all:
You can use the optional argument to decide the point of evaluation. 0 means show, 1 means in a \vbox at this point (no floats usable), 2 means \AtEndDocument (and because the \AtEndDocument is added \AtEndDocument, it should be really late) floats should work here -- but captions don't make it to the \listof....
\documentclass[]{article}
\usepackage{environ,atbegshi}
\makeatletter
\newif\ifAtEnd@DontShowMe
\newcommand*\contents@DontShowMe{}
\newcommand\addto@DontShowMe[1]
{%
\xdef\contents@DontShowMe{\unexpanded\expandafter{\contents@DontShowMe#1}}%
}
\NewEnviron{DontShowMe}[1][2]
{%
\ifcase#1
\expandafter\BODY
\or
\setbox0\vbox{\BODY}%
\or
\global\AtEnd@DontShowMetrue
\expandafter\addto@DontShowMe\expandafter{\BODY}%
\fi
}
\AtEndDocument
{%
\ifAtEnd@DontShowMe% only include this piece of code if it was used
\AtEndDocument
{%
\clearpage
\AtBeginShipout{\AtBeginShipoutDiscard}%
\contents@DontShowMe
}%
\fi
}
\makeatother
\begin{document}
\listoffigures
This shows up
\thesection% here it is 0
\begin{DontShowMe}
This code doesn't show up and is evaluated at the end of the document.
To show this: \stepcounter{section}
\begin{figure}test\caption{test}\end{figure}
To produce an error to check that it does evaluate uncomment this:
%\PackageError{foo}{foo}{foo}
\end{DontShowMe}
\thesection% here it is still 0
\end{document}
\setbox0\vbox{<your-code-here>}– Skillmon Feb 26 '18 at 10:07\includeonly{...}and\include{...}, for only part of text: as far as i know, is not possible. own command should be in preamble and not hidden from compilation. – Zarko Feb 26 '18 at 12:40