# Check if an environment is empty

I have got a custom environment which (sometimes) may not have anything inside it. Under those circumstances I need to take some special actions. Is there a way to check whether my environment is empty?

Here is the first idea, though not very useful I am afraid.

\documentclass{article}

\newenvironment{abletocheckempty}
{
% Do this if the environment is not empty
Not empty

% And do this if the environment is empty
Empty
}
{
% No change in closing behavior
}

\usepackage{lipsum}

\begin{document}

\begin{abletocheckempty}
There is some text or even command here.
\lipsum[1]
\end{abletocheckempty}

\begin{abletocheckempty}
\end{abletocheckempty}

\end{document}


If we take it a step further, and use the environ package, we can formulate a code like this.

\documentclass{article}

\usepackage{environ}

\usepackage{etoolbox}
\makeatletter
\NewEnviron{abletocheckempty}
{\notblank{\BODY}{Not empty}{Empty}}
\makeatother

\usepackage{lipsum}

\begin{document}

\begin{abletocheckempty}
There is some texts or even commands here.
\lipsum[1]
\end{abletocheckempty}

\begin{abletocheckempty}
\end{abletocheckempty}

\end{document}


The above code compiles fine but I get,

Not empty
Not empty


as output.

What is it I could be missing here?

I understand that as stated in the manual for etoolbox,

\ifblank{<string>}{<true>}{<false>}

Expands to <true> if the <string> is blank (empty or spaces), and to <false> otherwise. The <string> is not expanded in the test.

So, I tried using an \expandafter before \notblank, without any avail.

• You want \ifdefempty\BODY{<true code>}{<false code>}. When you do \ifblank{\BODY} it counts the tokens in the curly braces which is obviously not empty because there is \BODY even though the replacement text of \BODY would be empty. – Henri Menke Oct 12 '16 at 8:10

You could use other etoolbox features as noted in comments, or just test \BODY directly, this makes not empty, empty, empty

\documentclass{article}

\usepackage{environ}

\usepackage{etoolbox}
\makeatletter
\NewEnviron{abletocheckempty}
{\ifx\BODY\@empty
Empty%
\else
Not empty%
\fi}
\makeatother

\usepackage{lipsum}

\begin{document}

\begin{abletocheckempty}
There is some texts or even commands here.
\lipsum[1]
\end{abletocheckempty}

\begin{abletocheckempty}
\end{abletocheckempty}

\begin{abletocheckempty}\end{abletocheckempty}

\end{document}

• The lipsum text doesn't show up for me, only the empty/not empty. – Richard Jul 12 at 16:28
• It was a silly mishap. It turns out I needed to replace Not Empty with \BODY. I've edited the answer to express this. – Richard Jul 12 at 19:35
• Separately, I wonder if you have a suggestion of how to handle cases where the \BODY reduces to whitespace, putting a blank line between the section opening and closing breaks your solution. – Richard Jul 12 at 19:36
• the site really doesn't support asking new questions as comments on old questions, would be better to ask a new question, but you could add a test for \ifx\BODY\zzpar where zzpar is defined by \def\zzpar{\par} – David Carlisle Jul 12 at 19:45
• sorry my initial reply to your lipsum comment was misleading, the enviornment body is not typeset here, as requested in the question it just outputs debug output of empty or not empty (I rolled back the edit) – David Carlisle Jul 12 at 19:52