I have cases where certain text may end up being empty based on various flag settings. Thus, I adapted solutions from Proper way to detect empty/blank text and Missing Item number in enumerate to typset the content into a box and check if the width of the box is zero.
Since, I retypeset the content (instead of using the \savebox I measured), the equation numbering is incorrect:
I thougth of two "simple" solutions:
- Manually set
\measuring@trueand - redefine
\labelduring the empty check.
Sadly, these turn out to be so simple that they do not work!! The frist results in an error condition, and the second seems to have no effect.
Notes:
- In this MWE I could reuse the
\saveboxinstead of re-typesetting it, but that is not possible in my actual use case as I disable other macros (which do not impact the check for empty text) during the "measuring" phase.
Code:
\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}

\edefexpansion and check if it's empty, or a full expansion (with\romannumeral-`\Qtrick) and check if the result is empty? – Manuel Sep 08 '16 at 11:11\xspaceinside\SetNameshould not be there, and be only in the output macro\ShowNameandAdress), so that it's easy to check if the variable is empty or not, you would have much simple macros to compare to empty or blank strings. – Manuel Sep 08 '16 at 11:55