# Why does xifthen not work with this beamer code?

I am attempting to use \isempty to decide if a macro argument is empty. It works fine with book, but gives an error message with beamer:

! Undefined control sequence.
<write> ...[]{Outline0.0.1.1}{NOT empty}{\Parent2
}% 1


MWE:

%\documentclass{book}
\documentclass{beamer}
\usepackage{xifthen}

\newcommand{\MyMacro}[2][]{
\ifthenelse{\isempty{#1}}{\subsection{empty}}{\subsection{NOT empty}}
}
\begin{document}

\MyMacro[1]{First}
hello
\end{document}


I do have a higher level unit 'section' like \Mysection{title1}{}{title2} and now I found the (maybe) reason. It looks like there is a 'warning' (I was looking for 'undefined', and it follows the 'warning')

starting section (./Demo.toc)pdfTeX warning (ext4):
, duplicate ignored
\relax
l.4 ...title1}{}{title2}
pdfTeX warning (ext4): destin
ation with the same identifier (name{page.1}) has been already used, duplicate
ignored
\relax
l.4 ...title1}{}{title2}


This line refers to my 'section'.

My document contains source printable both in 'book' form, where the highest unit is 'chapter', and 'beamer' form, where the same text is at 'section' level. This is why I need to use my own sectioning unit, and transform it to either 'chapter' or 'section'. Both native units have optional argument (short title).

\MEchapter}[2][]{
\section[#1]{#2} % Results in error if #1 empty
\ifthenelse{\isempty{#1}}{\section{#2}}{\section[#1]{#2}}   % runs OK
}


I.e. it looks like the error is that I am attempting to pass an empty argument to section, but the square brackets are present. If the square backets are omitted, it works fine.

I see no reason why it is so.

beamer defines a \Parent for each sectional unit type. There is no \section - level 1 - defined in your code for your \subsection - level 2.

Either use

\newcommand{\MyMacro}[2][]{%
\ifthenelse{\isempty{#1}}{\section{empty}}{\section{NOT empty}}
}


or

\section{A section}
\MyMacro[1]{First}
hello


to specify the parent explicitly.

References:

Contrary to article, book etc. the beamer class does not work if \subsection is used without a parent \section, that's the cause of the \Parent2 error message.

Just use a \section before \MyMacro (given that \MyMacro is really needed at all!)

\documentclass{beamer}

\usepackage{xifthen}

\newcommand{\MyMacro}[2][]{%
\ifthenelse{%
\isempty{#1}%
}{%
\subsection{empty}%
}{%
\subsection{NOT empty}%
}%
}
\begin{document}

\section{Foo}
\MyMacro[1]{First}

\MyMacro{First}

hello
\end{document}