Problem expanding math in arguments of en environment

environmentsexpandafterexpansionmath-modenewtheorem

I want to redefine by hand a "restatable" environment for theorems. I have to do so because for some obscure reason the jloganal class (for the Journal of Logic and Analysis) mess it up: when restated, theorems have a new number corresponding to the section they are restated in. The journal suggests to "fake the environment" and rewrite it entirely. I do not like this "solution". By the way, this exercise is a good occasion to improve my Latex skills.

Here is an attempt:

        \documentclass[10pt,a4paper]{article}
        \usepackage[utf8]{inputenc}
        \usepackage[T1]{fontenc}
        \usepackage{amsmath}
        \usepackage{amssymb}
        \usepackage{graphicx}
        \usepackage{xparse}

        \NewDocumentEnvironment{restate}{mmmb}{
            \expandafter\xdef\csname #2\endcsname{#4} 
        \begin{#1}\label{#3}#4\end{#1}
        }{}
        \newtheorem{thm}{Theorem}

        \begin{document}
        \begin{restate}{thm}{thmun}{thmlabel}
           test-text $\forall x\in\mathbb{R}\quad f(x)=1$
        \end{restate}
        \thmun
        \end{document}

My problem is that \mathbb{R} leads to an error:

26: Undefined control sequence. \end
26: Undefined control sequence. \end
26: Undefined control sequence. \end
26: Undefined control sequence. \end
27: Missing { inserted. \thmun
: File ended while scanning text of \errhelp.

Also I am looking for a command that would behave like this: \getEnvName{thm} would write Theorem.

Thank you for your help.

Best Answer

Why \xdef? You just want \gdef.

A better way would be to store alse \begin{thm} and \end{thm} and also locally redefining \thethm in the restatement, so the number agree.

\documentclass[10pt,a4paper]{article}
%\usepackage[utf8]{inputenc}% no longer needed
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{xparse}% no longer needed

\NewDocumentEnvironment{restate}{mmmb}{%
  \ExpandArgs{c}\gdef{#2}{%
    \begingroup
    \ExpandArgs{c}\renewcommand{the#1}{\ref{#3}}%
    \begin{#1}#4\end{#1}%
    \addtocounter{#1}{-1}%
    \endgroup
  }%
  \begin{#1}\label{#3}#4\end{#1}
}{}
\newtheorem{thm}{Theorem}

\begin{document}

\begin{restate}{thm}{thmun}{thmlabel}
   test-text $\forall x\in\mathbb{R}\quad f(x)=1$
\end{restate}

\thmun

\end{document}

enter image description here

Note that inputenc with utf8 is no longer needed, nor is xparse.

I used the “modern” \ExpandArgs{c}\gdef{...} instead of the clumsier

\expandafter\gdef\csname...\endcsname

For your particular setting, I wouldn't look for greater generality.

\documentclass[10pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amssymb}

\newtheorem{thm}{Theorem}
\newtheorem{restatable}[thm]{\protect\restatablename}
\newcommand{\restatablename}{}% initialize

\NewDocumentEnvironment{restate}{mmmb}{%
  % #1 = theorem header, #2 = symbolic name, #3 = label, #4 = contents
  \renewcommand{\restatablename}{#1}%
  \ExpandArgs{c}\gdef{#2}{%
    \begingroup
    \renewcommand{\thethm}{\ref{#3}}%
    \renewcommand{\restatablename}{#1}%
    \begin{restatable}#4\end{restatable}%
    \addtocounter{thm}{-1}%
    \endgroup
  }%
  \begin{restatable}\label{#3}#4\end{restatable}
}{}

\begin{document}

\section{Introduction}

\begin{restate}{Theorem}{mainthm}{thmlabel}
   test-text $\forall x\in\mathbb{R}\quad f(x)=1$
\end{restate}

\begin{restate}{Proposition}{importantprop}{proplabel}
   test-text $0=0$
\end{restate}

\section{Proofs}

\mainthm

\importantprop

\end{document}

enter image description here