# Easy-to-use reference-able environment for restating theorem-like environments?

This question is a development of the same issue as this one; following my own answer and later comment there, I decided it deserves a spin-off.

I would like to define an environment for restating theorems(/lemmata/propositions/etc), which makes the following code work:

\documentclass{article}
\usepackage{ntheorem}

% Magic goes here

\newtheorem{theorem}{Theorem}[section]
\newtheorem{prop}[theorem]{Proposition}

\begin{document}
\begin{theorem}\label{myfirst}
This is the first environment.
\end{theorem}
\begin{prop}\label{mysecond}
This is the second environment.
\end{prop}
\begin{theorem}\label{mythird}
This is the third environment.
\end{theorem}
\clearpage
\begin{restatement}[restated]{mysecond}
\label{mysecond-restated}
This is a restatement of the second environment.
\end{restatement}
\begin{theorem}\label{myfourth}
This is the fourth environment.
\end{theorem}
a \verb|ref| to the second environment: \ref{mysecond} \\
a \verb|ref| to its restatement: \ref{mysecond-restated} \\
a \verb|pageref| to the second environment: \pageref{mysecond} \\
a \verb|pageref| to its restatement: \pageref{mysecond-restated}
\end{document}


Requirements:

1. The environment only requires the label of the theorem you're referencing, nothing else.
2. You should be able to reference the restatement rather than the original statement.
3. You can't assume the environment's counter has the same name as the environment (e.g. the user may have \newtheorem{proposition}[theorem]{Proposition}, so propositions use the theorem counter).

Optional, Nice-to-Have features:

1. No reliance on hyperref, cleveref or thmtools. (This was "Requirement 5" which the bounty mentions)
2. Code which works with amsthm as well.
3. \autorefing or \namecrefing the restatement work when hyperref or cleveref are used.
4. Code doesn't break when writing right-to-left documents (Hebrew, Arabic)
5. Possibility of restating unnumbered theorems.

An initial solution

My initial work has evolved into a basically-satisfactory solution, see below, with the help I got asking lead-up questions. But that solution suffers from several problems:

The problems with this are:

• I have no idea what \cref@constructprefix in ntheorem does, and what's the intended use of p@somecounter, so I basically copy-pasted that part blindly and maybe I'm doing something wrong.
• The code is already not so short.
• Code likely to break if cleveref or ntheorem change slightly.

And it does not meet most of the useful want-to-have's above. So improvements are quite welcome for your bounty!

-
You seem to be requiring a package that does the same as cleveref and some other packages. –  egreg Dec 22 '11 at 15:20
The next question is: how many of these "restated theorems" do you have? Writing a bunch of macros to solve a problem that can be better treated "locally" is pointless. –  egreg Dec 22 '11 at 15:32
@egreg: I have about four of these in my PhD thesis - but I'm writing a document class based on it and I want it to have this capability... –  einpoklum Dec 22 '11 at 15:35
I think the problem of your question based on the requiremnts. As egreg wrote you want to create a package and you didn't give any preliminary work. –  Marco Daniel Dec 24 '11 at 22:22
I mean the initial work. So far every helper must start his work at the point zero. Maybe it is easier to split the questions und to present some ideas. –  Marco Daniel Dec 25 '11 at 8:27

Well, with a little help from friends in other questions (this one and this one, it looks like I have a solution satisfying the basics:

Edit: Now supporting hyperref, but for that we need counter aliasing, see this question for the reason we need that.

\documentclass{article}
\usepackage{etoolbox}
\usepackage{ntheorem}
\usepackage{hyperref}
% cleveref after hyperref!
\usepackage{cleveref}
\usepackage{aliascnt}

\makeatletter

% usage:
% same as you would use \newtheorem using an existing counter
\def\newaliasedtheorem#1[#2]#3{%
\newaliascnt{#1@alt}{#2}
\newtheorem{#1}[#1@alt]{#3}
\expandafter\newcommand\csname #1@altname\endcsname{#3}
}

% usage:
% \begin{restatement}[comment]{label-of-original-statement}
%
\newenvironment{restatement}[2][]{%
\begingroup%
\@ifundefined{r@cref@#2}{%
\GenericWarning{}{Reference #2' undefined, using environment \restatement@defaultenv' for restatement.}
\def\restatement@envname{\restatement@defaultenv}
}{%
\cref@gettype{#2}{\restatement@envname}%
\cref@getlabel{#2}{\restatement@label}%
\edef\restatement@countername{\getenvcountername{\restatement@envname}}
\patchcmd{\@thm}%
{\@ifnextchar}%
{\@namedef{the\restatement@countername}{\restatement@label}%
\protected@edef\@currentlabel%
{\csname p@\restatement@countername\endcsname \restatement@label}%
\cref@getcounter{#2}{\restatement@counterval}%
\cref@constructprefix{\restatement@envname}{\cref@result}%
\protected@xdef\cref@currentlabel{%
[\restatement@envname][\restatement@counterval][\cref@result]%
\csname p@\restatement@countername\endcsname \restatement@label}%
\@ifnextchar}%
{}{}%
}%
\def\restatement@comment{#1}%
\ifx\restatement@comment\@empty%
\begin{\restatement@envname}%
\else%
\begin{\restatement@envname}[#1]%
\fi%
\@ifundefined{r@cref@#2}{}{%
}%
}%
{%
\end{\restatement@envname}%
\endgroup%
}

% Suggestion at:
% http://tex.stackexchange.com/a/39328/5640
% this works with all of amsthm, ntheorem and thmtools (!)
\def\getenvcountername#1{%
{\expandafter\expandafter\expandafter\@getenvcountername@kernel
\csname\ifcsname thmt@original@#1\endcsname thmt@original@\fi#1\endcsname}
\def\@getenvcountername@kernel#1#2#3{#2}
\def\@getenvcountername@ntheorem\csname#1\endcsname#2#3#4#5{#4}

\newcommand{\restatement@defaultenv}{quote}
\makeatother

\newtheorem{theorem}{Theorem}[section]
\newaliasedtheorem{prop}[theorem]{Proposition}
\begin{document}
\begin{theorem}\label{myfirst}
This is the first environment.
\end{theorem}
\begin{prop}\label{mysecond}
This is the second environment.
\end{prop}
\begin{theorem}\label{mythird}
This is the third environment.
\end{theorem}
\clearpage
\begin{restatement}[restated]{mysecond}
\label{mysecond-restated}
This is a restatement of the second environment.
\end{restatement}
\begin{theorem}\label{myfourth}
This is the fourth environment.
\end{theorem}
a \verb|ref| to the second environment: \ref{mysecond} \\
a \verb|ref| to its restatement: \ref{mysecond-restated} \\
a \verb|pageref| to the second environment: \pageref{mysecond} \\
a \verb|pageref| to its restatement: \pageref{mysecond-restated} \\
a \verb|autoref| to the second environment: \autoref{mysecond} \\
a \verb|autoref| to its restatement: \autoref{mysecond-restated}
\end{document}

-
This does not work for me. On the second page, I get: [restated] This is a restatement of the second environment. There seems to be something wrong with the restatement environment. –  Marco Breitig Jun 24 at 16:24
@MarcoBreitig: I don't currently have the time to do debugging (not writing any LaTeX this exact moment, and super-busy) - but can you put an MWE on some pastebin-ish site, and post a link to that? –  einpoklum Jun 24 at 19:54
I already got the results I wanted by piecing together various functionalities. But for my PhD thesis I have similar requirements as you had and was just looking if I could re-use your posted example. So no need to rework your code if your time is a scarce resource. –  Marco Breitig Jun 25 at 15:15
@MarcoBreitig: I'd still like you to post a failing example somewhere, so I can get around to it. Also, maybe you can post what you did as an answer here... –  einpoklum Jun 25 at 17:32