选择性地抑制排版输出的生成

选择性地抑制排版输出的生成

是否可以将 TeX 置于“静音”模式,以抑制所有排版?

\documentclass{article}
\pagestyle{empty}
\begin{document}
  \suppress
  This text will not be seen in the generated document.
  \enable
  But this text will.
  \suppress
  And this text won't again.
\end{document}

应该“照常”处理输入(维护计数器等),只是不输出任何内容。

编辑:当前的应用程序是创建一个 PDF 格式的文档,其中仅包含给定源中包含的图形。但是,最终我感兴趣的是,是否可以以通用方式静音输出,就像我要求的那样。如果我只想解决这个问题,我只需在 Acrobat 中打开 PDF 并提取图形即可。

答案1

灵感源自syntonly.sty

\documentclass{article}
\makeatletter
\font\dummyft@=dummy \relax
\def\suppress{%
  \begingroup\par
  \parskip\z@
  \offinterlineskip
  \baselineskip=\z@skip
  \lineskip=\z@skip
  \lineskiplimit=\maxdimen
  \dummyft@
  \count@\sixt@@n
  \loop\ifnum\count@ >\z@
    \advance\count@\m@ne
    \textfont\count@\dummyft@
    \scriptfont\count@\dummyft@
    \scriptscriptfont\count@\dummyft@
  \repeat
  \let\selectfont\relax
  \let\mathversion\@gobble
  \let\getanddefine@fonts\@gobbletwo
  \tracinglostchars\z@
  \frenchspacing
  \hbadness\@M}
\def\endsuppress{\par\endgroup}
\makeatother

\begin{document}
Some text.
\suppress
This text \emph{will} not be seen in the generated document.
\endsuppress
But this text will.
\suppress
And this text won't again.
\endsuppress
This will show.
\end{document}

然而,规则、根号、分数线和一些其他东西(主要是数学)可以逃脱。

答案2

我想到的一个想法是使用\nullfont

\documentclass{article}
\long\def\suppress#1\endsuppress{%
  \begingroup%
    \tracinglostchars=0%
    \let\selectfont=\nullfont
    \nullfont #1\endgroup}
\pagestyle{empty}
\begin{document}
  \suppress
  This text will not be seen in the generated document.
  \endsuppress
  But this text will.
  \suppress
  And this text won't again.
  \endsuppress
\end{document}

超级警告

  1. 数学不是隐藏的
  2. 我对 LaTeX 不太了解,因此无法判断我上面做的事情是否真的很愚蠢。

答案3

评论包应该做你想做的事:

...
\comment
This text will not be seen
\endcomment
But this text will!
...

答案4

这个答案确实不是完美地回答了这个问题,因为不是所有输入都照常处理,这也是大多数其他答案所关注的。然而,似乎其他答案都不能完全令人满意,并提到在某些情况下(例如数学模式),它们无法保证抑制输出。这个答案采用了不同的方法,妥协于照常处理所有内容,但保证抑制所有不需要的输出。

这种方法会忽略两个宏之间未明确排除的所有内容。我不会扩展未排除的宏,因此如果排除的宏隐藏在未排除的宏内,它将不是被处决。


我正在向前看\futurelet。如果下一个标记是结束标记(\enable如您所称),我什么也不做。否则,我会吞下下一个标记并\suppress再次调用,它再次检查并吞下标记,直到达到\enable

\documentclass{article}

\newcommand{\suppress}{\futurelet\nexttoken\doSuppress}

\newcommand{\doSuppress}{%
    \ifx \nexttoken \enable
        \let\do=\relax
    \else
        \let\do=\suppress
    \fi
    \GobbleOneTokenAndCall \do
}

% I am not using \expandafter\do\@gobble because that may cause trouble if
% the next token has catcode 1 (begin group), 2 (end group) or 10 (space).
\newcommand{\GobbleOneTokenAndCall}[1]{%
    \afterassignment#1%
    % The space after the equals is important if you want to gobble a space.
    % Without it the next token after the space would be gobbled, too.
    \let\gobbledToken= %
}

% The end token.
% This won't be expanded but the replacement text should be unique.
\newcommand*{\enable}{\PackageError{suppress}{\string\enable\space without \string\suppress}{}}


\begin{document}
    \suppress
    This text will not be seen in the generated document.
    \enable

    But this text will.
\end{document}

可以轻松扩展此代码以不抑制某些命令(如\end{document}和)\begin{figure}

下面的代码不是抑制环境内容figure,无论它是否在 a 之后\suppress。因此,数字的计数器照常增加。其他计数器,如部分计数器,不是\section如果命令被编辑,则实现会增加\suppress

\documentclass{article}

% Suppress code
% =============
\usepackage{etoolbox}

\newcommand{\suppress}{\futurelet\nexttoken\doSuppress}

\newcommand{\doSuppress}{%
    \ifx \nexttoken \enable
        \let\do=\relax
    \else\ifx \nexttoken \begin
        \let\do=\suppressCheckBegin
    \else\ifx \nexttoken \end
        \let\do=\suppressCheckEnd
    \else
        \let\do=\suppress
    \fi \fi \fi
    \GobbleOneTokenAndCall \do
}

\newcommand{\suppressCheckBegin}[1]{% #1: environment name
    \def\envName{#1}%
    \ifx \envName \envNameFigure
        \def\do{\beginAndSuppressAfterEnd{#1}}%
    \else
        \let\do=\suppress
    \fi
    \do
}

\newcommand{\beginAndSuppressAfterEnd}[1]{%
    \begin{#1}%
    \csappto{end#1}{\endgroup\suppress}%
}

\newcommand{\suppressCheckEnd}[1]{% #1: environment name
    \def\envName{#1}%
    \ifx \envName \envNameDocument
        \def\do{\end{document}}%
    \else
        \let\do=\suppress
    \fi
    \do
}

% Note the star, which means that this macro is not \long.
% Without this \ifx would always evaluate to false
% because it would compare a \long to a not \long macro.
\newcommand*{\envNameDocument}{document}
\newcommand*{\envNameFigure}{figure}

% I am not using \expandafter\do\@gobble because that may cause trouble if
% the next token has catcode 1 (begin group), 2 (end group) or 10 (space).
\newcommand{\GobbleOneTokenAndCall}[1]{%
    \afterassignment#1%
    % The space after the equals is important if you want to gobble a space.
    % Without it the next token after the space would be gobbled, too.
    \let\gobbledToken= %
}

% The end token.
% This won't be expanded but the replacement text should be unique.
\newcommand*{\enable}{\PackageError{suppress}{\string\enable\space without \string\suppress}{}}


% Test document
% =============
\usepackage{float}
\floatplacement{figure}{htbp}

% see https://www.ctan.org/pkg/latex2e-help-texinfo, section 5.6 "Floats"
\setcounter{topnumber}{10}
\setcounter{bottomnumber}{10}
\setcounter{totalnumber}{10}

\begin{document}
    \suppress
    suppressed~1.1

    \begin{figure}
        \centering
        ABC
        \caption{An example}
        \label{fig:abc}
    \end{figure}

    suppressed~1.2

    \enable

    \emph{not} suppressed~1.1

    \begin{figure}
        \centering
        DEF
        \caption{Another example}
        \label{fig:def}
    \end{figure}

    \emph{not} suppressed~1.2

    \suppress
    suppressed~2.1

    \begin{minipage}{.5\linewidth}
        Environments are not a problem.
    \end{minipage}

    suppressed~2.2

    \begin{figure}
        \centering
        GHI
        \caption{Yet another example}
        \label{fig:ghi}
    \end{figure}

    suppressed~2.3

\end{document}

相关内容