5

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.

  • In this specific instance you can use \NewDocumentCommand{\thirdofthree}{ m m m }{#3} \NewDocumentCommand{\getEnvName}{ m }{\expandafter\expandafter\expandafter\thirdofthree\csname #1\endcsname} to retrieve Theorem when you issue \getEnvName{thm}. If you use other theorem-related packages, the process may not work as expected. – Werner Oct 04 '23 at 21:16

2 Answers2

1

There are a number of ways to tackle this. Easiest is to use \protected@xdef:

\makeatletter% https://tex.stackexchange.com/q/8351/5764
\NewDocumentEnvironment{restate}{ m m m b }{%
  \expandafter\protected@xdef\csname #2\endcsname{#4}%
  \begin{#1}\label{#3}#4\end{#1}%
}{}
\makeatother

But you can also avoid expansion of content (and therefore fix them in the definition) by temporarily \letting them to \relax:

\NewDocumentEnvironment{restate}{ m m m b }{%
  \begin{#1}\label{#3}#4\end{#1}%
  % Temporarily deactivate macros for definition following below
  \let\mathbb\relax
  \expandafter\xdef\csname #2\endcsname{#4}%
}{}
Werner
  • 603,163
1

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

egreg
  • 1,121,712
  • Thank you for your solution. I still have a question : imagine I also have something like \newtheorem{prop}[thm]{Proposition}. Now the problem is that there is no prop counter and thus the the#1 would fail. – Quentin G Oct 05 '23 at 08:43
  • @QuentinG I don't think that anything that doesn't qualify as a theorem is worth restating. – egreg Oct 05 '23 at 08:45
  • Maybe but in my particular case there is a proposition that is useful to understand the point of my work while not being a strong result. Therefore, it is stated in the introduction and proved later. For the sake of readability, I prefer to restate it before the proof. – Quentin G Oct 05 '23 at 08:51
  • 1
    @QuentinG Look at the added code to see whether it fulfills your expectations. – egreg Oct 05 '23 at 09:04
  • @egreg It might also be worth looking at equation numbers inside the theorem. I do have a solution that works with your other restate env, but it uses regex so it a little slow. (here we opted to replace all labels inside the body of the thm by \tag{\ref{key}} assuming that they are all in math envs. – daleif Oct 05 '23 at 09:13