1

If I have a chunk of code which I'd like to hide when compiling, is there something like

\noshow{chunk of code}

So this chunk of code is still compiled but just not showing in the PDF? I know I can comment this part but problem is if I edit my own commands, the commented codes are not compiled and checked.

  • I've linked one solution, but if you search here for hide there are plenty of others, depending on whether you want to keep the space occupied by that section. – Chris H Feb 26 '18 at 9:53
  • \setbox0\vbox{<your-code-here>} – Skillmon Feb 26 '18 at 10:07
  • Another related question: Hide content to show later – Ignasi Feb 26 '18 at 10:19
  • As far as I can tell, the linked duplicate is not a duplicate as this doesn't compile the code but gobbles it. The OP of this question is asking for a solution which actually compiles, but shows no ouptut. – Skillmon Feb 26 '18 at 12:24
  • on level of files you can use combination \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
1

We need to know more: how long is the hidden text? Will you provide the PDF to others or only a printout? And so on.

For example, you could simply use white »coloured« text:

\documentclass{article}
\usepackage{xcolor}

\begin{document}
This is text.
\textcolor{white}{This text will not be printed. However, you can copy it out of
  the pdf.}

  \llap{\textcolor{white}{invisible text, can't be copied.}}{However, you can do
    funny things, but only without a linebreak.}
\end{document}
  • As far as I understood he wants to run custom commands even if those aren't used for debugging. – Skillmon Feb 26 '18 at 13:05
  • @Skillmon We do not know enough, let us wait until the OP provides more detailes. – Keks Dose Feb 26 '18 at 13:21
1

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}

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.