检查空文本时禁用公式编号

检查空文本时禁用公式编号

我遇到过某些文本可能最终因各种标志设置而为空的情况。因此,我改编了以下解决方案: 检测空文本的正确方法枚举中缺少项目编号 将内容排版到一个框中并检查框的宽度是否为零。

由于我重新输入了内容(而不是使用\savebox我测量的),所以方程编号不正确:


在此处输入图片描述


我想到两个“简单”的解决方案:

  1. 手动设置\measuring@true
  2. \label在空检查时重新定义。

遗憾的是,这些方法太过简单,以至于不是有效!第一个结果出现错误,第二个似乎没有任何效果。

笔记:

  • 在这个 MWE 中,我可以重复使用\savebox而不是重新排版它,但这在我的实际使用情况下是不可能的,因为我在“测量”阶段禁用了其他宏(这不会影响空文本的检查)。

代码:

\documentclass{article}
\usepackage{mathtools}
\usepackage{etoolbox}

\makeatletter
%% Adapted from egreg's solutions at 
%%     https://tex.stackexchange.com/questions/44919/proper-way-to-detect-empty-blank-text
%%     https://tex.stackexchange.com/questions/57268/missing-item-number-in-enumerate
\newsavebox{\@NonEmptyTestBox}%
\newcommand{\DoIfNonEmptyText}[1]{% Note: Actually takes 2 parameters
    \begingroup%
    \EnableCheckingForEmptyText% 
    \savebox{\@NonEmptyTestBox}{\hfuzz=1000pt\everypar{}\vbox{#1}}% 
    \ifdim\wd\@NonEmptyTestBox=\z@\relax% 
            \DisableCheckingForEmptyText%
            \endgroup\expandafter\@gobble%
        \else%
            \DisableCheckingForEmptyText%
            \endgroup\expandafter\@firstofone%
        \fi%
    }%

%\newtoggle{CheckingForEmptyText}% <-- Commented out as it is not used in MWE, but is in actual use case.
\newcommand*{\EnableCheckingForEmptyText}{%
    %\toggletrue{CheckingForEmptyText}% 
    %\measuring@true% <-- Error: "Multiple \label's:"
    \renewcommand*{\label}[1]{}% <--- Seems to have no effect
}%
\newcommand*{\DisableCheckingForEmptyText}{%
    %\togglefalse{CheckingForEmptyText}%
    %\measuring@false%
}%
\makeatother 


\newcommand*{\EquationsText}{%
    Some interesting equations include
    \begin{align}
        F &= ma \label{eqn:newton} \\
        E &= mc^2 \label{eqn:einstein}
    \end{align}
    But there are others as well.%
}


\begin{document}
\DoIfNonEmptyText{\EquationsText}{%
    \section{Equations}
    \EquationsText%
}%
\end{document}

答案1

我有些难以理解这种宏的原因,但顾客永远是对的。

\documentclass{article}
\usepackage{mathtools}
\usepackage{etoolbox}

\makeatletter
%% Adapted from egreg's solutions at 
%%     http://tex.stackexchange.com/questions/44919/proper-way-to-detect-empty-blank-text
%%     http://tex.stackexchange.com/questions/57268/missing-item-number-in-enumerate
\newsavebox{\@NonEmptyTestBox}
\newcommand{\DoIfNonEmptyText}[1]{% Note: Actually takes 2 parameters
  \begingroup
  \let\stepcounter\@gobble
  \let\label@in@display\@gobble % maybe other macros, such as \label
  \savebox{\@NonEmptyTestBox}{\hfuzz=1000pt\everypar{}\vbox{#1}}% 
  \ifdim\wd\@NonEmptyTestBox=\z@
    \endgroup\expandafter\@gobble
  \else
    \endgroup\expandafter\@firstofone
  \fi
}

%\newtoggle{CheckingForEmptyText}% <-- Commented out as it is not used in MWE
\makeatother 


\newcommand*{\EquationsText}{%
  Some interesting equations include
  \begin{align}
    F &= ma \\% \label{eqn:newton} \\
    E &= mc^2 \label{eqn:einstein}
  \end{align}
  But there are others as well.
}
\newcommand{\EmptyEq}{\sbox0{whatever}}

\begin{document}
\DoIfNonEmptyText{\EquationsText}{%
    \section{Equations}
    \EquationsText
}

\DoIfNonEmptyText{\EmptyEq}{%
    \section{Equations}
    \EmptyEq
}
\end{document}

相关内容