# Testing if \BODY is empty in Environ

I would like to test whether the string in \expandafter\long\expandafter\xdef\csname\currfilebase\Introduction is empty so that the section is not displayed. This environment introduction is defined in a file in P/Fourier.tex. Of course, what I did here does not work. I have tried many if the \if... commands from etoolbox but all seem to not work. (tried here).

\documentclass[11pt]{scrreprt}

\usepackage{currfile}
\usepackage{filehook}
\usepackage{environ}

\usepackage{etoolbox}

\def\Introduction{Introduction}

\NewEnviron{introduction}{%
\expandafter\long\expandafter\xdef\csname\currfilebase\Introduction \endcsname{\expandafter\unexpanded\expandafter{\BODY}}%
}%

\AtEndOfIncludes{%
\ifcurrfiledir{P/}{%
\ifdefequal{\csname\currfilebase\Introduction\endcsname}{ }{True}{False}%
\section{Introduction}%
\csname \currfilebase \Introduction \endcsname}%
}%

\begin{document}
\include{P/Fourier}
\end{document}


And P/Fourier.tex contains

\begin{introduction}
Test
\end{introduction}


I have a couple of those environment and I wish that they are not shown in the final pdf. So I have something like this if the file is empty:

Edit: Fixed the missing } and removed an irrelevant line.

Edit #2: I have changed this to the following code (with etoolbox):

\ifcsvoid{\currfilebase\Introduction}{}{%
\section{Introduction}%
\csname\currfilebase\Introduction\endcsname}%


This seems to work! Is this a good way of doing things? Thanks guys, now I have also learned \show which helped a lot!

Edit #3: Since I am already using etoolbox I can do use all others commands as well anyway, so

\newcommand{\makesection}[1]{\ifcsvoid{\currfilebase#1}{}%
{%
\section{#1}%
\csuse{\currfilebase#1}%
}%
}%


(I have done the same in the rest too)

-
could you remove the \include{P/Fourier} in your code and replace it by a small section of document that shows the problem? as it is, we can't run it. –  David Carlisle Jul 22 '12 at 9:55
I think \expandafter\ifx\expandafter\relax\csname\currfilebase\Introduction\endcsname\r‌​elax TRUE \else FALSE \fi could do the trick. But I can't test it right now. –  canaaerus Jul 22 '12 at 10:10
My apologies,I manually shortened the file. Will fix it in a couple a hours when I can access a real computer. –  Jonas Teuwen Jul 22 '12 at 11:45
Should be working now. –  Jonas Teuwen Jul 22 '12 at 14:30

That is already quite nice, what you did there, Jonas, and would probably have justified a self-answer. I only added some small changes:

\documentclass[11pt]{scrreprt}

\usepackage{currfile}
\usepackage{filehook}
\usepackage{environ}

\usepackage{etoolbox}

\def\introductionname{Introduction}
\def\methodsname{Methods}
\def\ideasname{Ideas}

\newcommand\defenviron[1]{\NewEnviron{#1}{\expandafter\long\expandafter\xdef\csname\currfilebase #1\endcsname{\expandafter\unexpanded\expandafter{\BODY}}}}

\defenviron{introduction}
\defenviron{methods}
\defenviron{ideas}

\newcommand\makesection[1]{\ifcsvoid{\currfilebase #1}{}%
{%
\section{\csname#1name\endcsname}%
\csuse{\currfilebase #1}%
}%
}%

\AtEndOfIncludes{%
\ifcurrfiledir{P/}{%
\makesection{introduction}%
\makesection{methods}%
\makesection{ideas}%
}{}%
}%

\begin{document}
\include{P/Fourier}
\end{document}


First of all I put those environment definitions in their own command \defenviron for convenience. Second I changed the naming scheme just a litte. Then I also added the missing false case for \ifcurrfiledir. But that's about it.

You could probably also loop over the environments in those two places, if the code is really this uniform.

-
Great. This is what I have changed in the end! I also added the capitalize of the stringstring package. One issue was that I would like to mix the commands makesection and defenviron. The problem is of course that after multiple inputs I will get errors! Perhaps I need to have a test that checks if this is already done. After that I can iterate over an etoolbox list. That worked but gave errors about something not being found while it was typeset. In any case, that would be a new question! –  Jonas Teuwen Jul 22 '12 at 20:04

You have a missing } in your code, if I fix that and then arrange the thing so it works in my directory setup and add

\expandafter\show\csname\currfilebase\Introduction\endcsname


Just before your test then I get

\env2-aIntroduction=\relax.

If there is no introduction at all and

> \env2-aIntroduction=\long macro:
->\ignorespaces .


If there is an empty introduction.

So you need to test if it has a definition just consisting of \ignorespaces (or don't add \ignorespaces unconditionally in your definition.

I then stopped at

! Undefined control sequence.
\scr@ds@tocentry ->\convertchar


which is I assume an unrelated local macro? so am not including a working version of the document as I would normally do.

-
You forgot to mention when the second case occurs. Also citation and code markup seem not consistent. –  canaaerus Jul 22 '12 at 10:27
@canaaerus I didn't look at what else the doc was doing, I gave up after explaining the main csname test question as there were other errors. If you have a more complete fix feel free to post a full answer and I'll probably delete this one. –  David Carlisle Jul 22 '12 at 10:31
@David: Thanks. I think I have fixed it now. So I remove \ignorespaces and fix it elsewhere? What test would work? –  Jonas Teuwen Jul 22 '12 at 16:12