# Wrapping a New Environment Around Theorem Environment

I'm trying to make a new environment which wraps around a theorem environment defined by newtheorem in the amsthm package. However, passing in a optional argument into the environment and then into the theorem environment is creating an empty set of parenthesis. The following is a very minimal example of what I'm seeing

\documentclass[12pt, letter]{article}

\usepackage{lipsum}
\usepackage{xcolor}

\newtheorem{thm}{Theorem}

\newenvironment{thm2}[1][]{
% The following line doesn't work???
%\ifx&#1&{\begin{thm}}\else{\begin{thm}}\fi
% However, the following does work but has issues
\begin{thm}[#1]
}
{
\end{thm}
}

\begin{document}

\begin{thm2}[Optional Argument]
This one looks a little better.
\end{thm2}

\begin{thm2}
What's up with the empty argument?
\end{thm2}

\end{document}


Looking online, I found that I can test if #1 is empty or not, but testing this doesn't seem to do much because \ifx&#1&{\begin{thm}}\else{\begin{thm}}\fi simply doesn't compile. How should I resolve this?

• Why are you mentioning amsthm when your mwe does not load it at all? – daleif Oct 21 '18 at 9:36

You can use \ifstrempty from the etoolbox package:

## Code:

\documentclass[12pt, letter]{article}

\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{etoolbox}

\newtheorem{thm}{Theorem}

\newenvironment{thm2}[1][]{%
% The following line now works?
\ifstrempty{#1}{\begin{thm}}{\begin{thm}[#1]}%
}
{%
\end{thm}%
}

\begin{document}

\begin{thm2}[Optional Argument]
This one looks a little better.
\end{thm2}

\begin{thm2}
This also look good now as there is no empty argument.
\end{thm2}

\end{document}


Just expand \else and \fi before issuing \begin{thm}:

\makeatletter
\newenvironment{thm2}[1][]
{\if\relax\detokenize{#1}\relax % test for emptiness
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\begin{thm}}{\begin{thm}[1]}%
}
{\end{thm}}
\makeatother


On the other hand, this is easier with xparse:

\usepackage{xparse}

\NewDocumentEnvironment{thm2}{o}
{\IfNoValueTF{#1}{\begin{thm}}{\begin{thm}[#1]}
{\end{thm}}