# Error using \ifthenelse from xifthen in theorem optional arg, with package thm-restate loaded

I want to put a conditional of the form \ifthenelse{\isempty{…}}{…}{…} in the optional argument of an amsthm-style theorem. Ordinarily, this works fine, but the package thm-restate (from thmtools) seems to break it.

I’ve tried adding brackets, adding \protect in various places, and so on, but can’t find a way to make this work. Can anyone suggest a way to get around this? A MWE is the following:

\documentclass{article}

\usepackage{xifthen}
\usepackage{amsthm}
\usepackage{thm-restate}

\newtheorem{theorem}{Theorem}

\begin{document}

\begin{theorem}[\ifthenelse{\isempty{foo}}{bar}{baz}]
\end{theorem}

\end{document}


This gives Undefined control sequence. on the \isempty (and several further errors). It compiles fine when thm-restate is removed.

Replacing \isempty with the other conditional commands of xifthen (like \isin, \isequal and so on) gives similar errors.

The above example minimally illustrates the error. However, my actual use-case has an extra complication which may be relevant: it’s wrapped into another environment, which passes an argument in to \isempty. It’s something like the following (which can be appended to the earlier MWE):

\newenvironment{mytheorem}[1][]
{\begin{theorem}[Note\ifthenelse{\isempty{#1}}{}{~arg: #1}]}
{\end{theorem}}

\begin{mytheorem}[foo]
Theorem text.
\end{mytheorem}


So I am really hoping for a solution which works with this not-quite-minimal example also.

You need an expandable test:

\documentclass{article}

\usepackage{xifthen}
\usepackage{amsthm}
\usepackage{thm-restate}

\newtheorem{theorem}{Theorem}
\ExplSyntaxOn
\cs_set_eq:NN\tlifempty \tl_if_empty:nTF
\ExplSyntaxOff
\begin{document}

\begin{theorem}[\tlifempty{foo}{bar}{baz}]
\end{theorem}

\begin{theorem}[\tlifempty{}{bar}{baz}]
\end{theorem}

\end{document}



• Thankyou! Just a note: to make this compile, I need to add the xparse package; otherwise I get ERROR: Undefined control sequence. at \ExplSyntaxOn. Is that the right way to make \ExplSyntaxOn etc available, or is there a more canonical way? – Peter LeFanu Lumsdaine Jul 1 '20 at 10:16
• In a current tex system expl3 is loaded by latex, but if your system is older you should load xparse or expl3 in your document. – Ulrike Fischer Jul 1 '20 at 10:18
• thanks! Loading it automatically must be pretty recent though: I’m on TexLive 2019, so not much out-of-date. – Peter LeFanu Lumsdaine Jul 1 '20 at 10:28
• it is recent, but a final texlive 2019 should have it too. – Ulrike Fischer Jul 1 '20 at 10:31