1

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:


enter image description here


I thougth of two "simple" solutions:

  1. Manually set \measuring@true and
  2. redefine \label during 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 \savebox instead 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}
  • May be it's answered in your other questions (in that case, sorry for asking again), but why exactly do you need boxes? Couldn't you do some \edef expansion and check if it's empty, or a full expansion (with \romannumeral-`\Q trick) and check if the result is empty? – Manuel Sep 8 '16 at 11:11
  • @Manuel: Such a solution did not show up at tex.stackexchange.com/questions/44919/…, where I had attempted to make a MWE. Perhaps you could post one there. – Peter Grill Sep 8 '16 at 11:35
  • Too old to answer it probably, but, in any case, if you separated “value” from “output” (in that question, for instance, the \xspace inside \SetName should 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 8 '16 at 11:55
3

I have some difficulties in understanding the reason for such a macro, but the customer's always right.

\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}
  • Works great for the given MWE. But, what about other numbered equation environments? Is there not a more general solution to this? – Peter Grill Sep 8 '16 at 11:38
  • @PeterGrill I forgot to remove that line, remaining from an earlier experiment. – egreg Sep 8 '16 at 22:13
  • @PeterGrill You're right! I was thinking to \addtocounter! – egreg Sep 9 '16 at 9:07

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.