有可能编译但不显示某一部分吗?

有可能编译但不显示某一部分吗?

如果我有一段代码想在编译时隐藏,那么有没有类似

\noshow{chunk of code}

所以这部分代码仍然经过编译,只是没有显示在 PDF 中?我知道我可以评论这部分,但问题是,如果我编辑自己的命令,注释的代码不会被编译和检查。

答案1

实现此目的的一种方法是将内容排版到您永远不会打印的框内。但这对任何内容都不起作用。在下面的示例中,浮动是不允许的,并且会引发错误。

\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}

以下也会评估内容,但仅限于\AtEndDocument,因此如果您需要在放置内容的位置评估内容,请不要使用此功能。优点是,任何内容都可以在此处工作而不会产生错误。

\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}

一个环境统治一切:

您可以使用可选参数来决定评估点。0表示显示,1表示此时\vbox(没有可用的浮点数),2表示\AtEndDocument(并且因为\AtEndDocument添加了\AtEndDocument,所以应该真的很晚)浮点数应该在这里工作 - 但标题不会到达\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}

答案2

我们需要了解更多信息:隐藏的文本有多长?您会将 PDF 提供给其他人还是只提供打印件?等等。

例如,您可以简单地使用白色»彩色«文本:

\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}

相关内容