# Macro to write new environments named by arguments

I'm writing a sty which makes many theorems. I have made a (until this point working) macro to speed up the process for each new theorem. For your convenience this is a minimal example of my problem without the sty or any other junk I defined.

What is happening is I get an error

Illegal parameter in definition of \endtestd

As you can see I'm trying to define a macro which defines not just a theorem but also a similar environment named by appending d' and functions with a dash. Note that this kind of code generally works when I use a typed environment name; the other defintion of thmd shows this does usually work (you can comment out the mynewtheorem definition, \mynewtheorem{test}{Test} and begin/end{test} to see that it works).

\documentclass{amsart}

\usepackage{hyperref}

\author{Richard}
\title{Test}

\newtheorem{thm}{Theorem}[section]

%Some sort of problem
\newcommand{\mynewtheorem}[2]{%token, name,
\newtheorem{#1}[thm]{#2}
\newenvironment{#1d}[1]
{
\renewcommand{\thethm}{\ref*{#1}$'$}%
\begin{##1}\label{#1'}
}
{\end{##1}}
}

%Works
\newenvironment{thmd}[1]
{\renewcommand{\thethm}{\ref*{#1}$'$}%
\begin{thm}\label{#1'}}
{\end{thm}}

\mynewtheorem{test}{Test}

\begin{document}

\maketitle

\section{Annoying tests}

%\begin{test}
% A good test?
%\end{test}

\begin{thm}\label{mylabel}
A theorem.
\end{thm}

\begin{thmd}{mylabel}
A theorem'. This works nicely!
\end{thmd}

\end{document}


This reminds me of the macro name by \csname problem but I don't see where to go on this.

• I think your problem is with \end{##1} as ##1 is not available at the end of an environment. You can use the environ package or use a \gdef to save ##1 globally so that is can be used in the \end{}. Example: gdef\Foo{##1}\begin{\Foo}\label{#1'} and \end{\Foo}. – Peter Grill Sep 20 '19 at 6:26
• You can easily resolve the problem with matching \begin{..}..\end{...}-pairs. But if you do that you still get a bunch of error-messages from the hyperref-package about missing/duplicate destinations. Please tell exactly how things should behave/what behavior is intended. – Ulrich Diez Sep 20 '19 at 9:46
• E.g., \mynewtheorem{test}{Test} beneath other things tries to define an environment testd wherein the macro \thethm is defined to refer to a referencing-label whose name is test. Where does this referencing-label come from? Is it a good idea to have a referencing-label whose name equals that of an environment? Also you have \addtocounter{...}{-1} without adjusting \theH<counter> for compensating duplicate generation of destination-names. – Ulrich Diez Sep 20 '19 at 9:54
• Actually not quite right @UlrichDiez. Look to the %Works example for what I intend and how it usually works. \thethm is being set to the input of the environment, NOT test or the input of the macro. Since we are living inside a newenvironment call, single # refers to arguments of the new environment (say testd) and double ## refers to arguments from my \newcommand. This is why I pasted the working code so you could see exactly how I intend things to work. Notice how the code is basically the same except thm and thmd are replaced by ##1' and ##1d. – Richard Birkett Sep 20 '19 at 16:05
• sorry... by ##1 and ##1d. Gosh I did find one problem with the counter. That should have been ##1 not a #1 for the counter! – Richard Birkett Sep 20 '19 at 16:12

You can do it more straightforwardly:

\documentclass{amsart}
\usepackage{refcount}
\usepackage{hyperref}

\newtheorem{unused}{Unused}[section] % base
\newcounter{dunused}

\makeatletter
\newcommand{\mynewtheorem}[2]{%token, name,
\newtheorem{#1}[unused]{#2}%
\newtheorem{inner#1d}[dunused]{#2}%
\newenvironment{#1d}[1]
{%
\renewcommand{\thedunused}{\ref*{##1}$'$}%
\begin{inner#1d}%
}
{\end{inner#1d}}%
}
\makeatother

\mynewtheorem{thm}{Theorem}

\author{Richard}
\title{Test}

\begin{document}

\maketitle

\ref{mylabel} and \ref{mylabelprime}

\section{Annoying tests}

\begin{thm}\label{mylabel}
A theorem.
\end{thm}

\begin{thmd}{mylabel}\label{mylabelprime}
A theorem'. This works nicely!
\end{thmd}

\end{document}


With some modifications, but the underlining idea is the same, we can make so that successive calls to \begin{thmd}{<label>} with the same label yield first a primed number, then a double primed number and so on.

For each called label an entry in a property list is made, containing the number of needed primes; this number is incremented at each call.

\documentclass{amsart}
\usepackage{refcount,xparse}
\usepackage{hyperref}

\newtheorem{unused}{Unused}[section] % base
\newcounter{dunused}

\ExplSyntaxOn
\NewDocumentCommand{\mynewtheorem}{mm}
{
\newtheorem{#1}[unused]{#2}%
\newtheorem{inner#1d}[dunused]{#2}%
\NewDocumentEnvironment{#1d}{m}
{
\prop_gput:Nnx \g_birkett_theorems_prop { ##1 }
{
\int_to_arabic:n { 0\prop_item:Nn \g_birkett_theorems_prop { ##1 } + 1 }
}
\renewcommand{\thedunused}{\ref*{##1}\birkett_theorems_primes:n{##1}}
\begin{inner#1d}
}
{\end{inner#1d}}
}
\prop_new:N \g_birkett_theorems_prop

\cs_new:Nn \birkett_theorems_primes:n
{
$\prg_replicate:nn { \prop_item:Nn \g_birkett_theorems_prop {#1} } { ' }$
}
\ExplSyntaxOff

\mynewtheorem{thm}{Theorem}

\author{Richard}
\title{Test}

\begin{document}

\maketitle

\ref{mylabel} and \ref{mylabelprime} and \ref{mylabeldoubleprime}

\section{Annoying tests}

\begin{thm}\label{mylabel}
A theorem.
\end{thm}

\begin{thmd}{mylabel}\label{mylabelprime}
A theorem'. This works nicely!
\end{thmd}

\begin{thmd}{mylabel}\label{mylabeldoubleprime}
A theorem''. This works nicely!
\end{thmd}

\end{document}


• @UlrichDiez Thanks for the input. Indeed, it can be done! ;-) – egreg Sep 21 '19 at 20:58
• That's funny: I just did more or less the same without expl3, not using a property-list but just some "helper-macro" whose name gets derived from the name of the environment and the name of the referencing-label. Both with your and with my approach you can also produce funny results, e.g., a second ''-theorem by providing a label which denotes a theorem were ' is already attached. To see what I mean, add to your code, e.g.,: \begin{thmd}{mylabelprime}\label{mylabelwhatsoeverprime}A theorem''. This works nicely!\end{thmd} – Ulrich Diez Sep 21 '19 at 21:26
• @UlrichDiez Priming a primed theorem is not something I'd like to try and support. :-) – egreg Sep 21 '19 at 21:53
• I fully agree. Nonetheless doing so is possible, thus it can be a pitfall. (That's why in the beginning I hesitated adding my referencing-label based approach to my answer.) If I understand things right, then priming a primed theorem is the only possibility of getting, e.g., a ''-theorem before a '-theorem into the text. This again is a thing which I would not do. But I cannot predict what other users might try. Just this: There will be all sorts of situations where at least one variant of a Murphy law applies. – Ulrich Diez Sep 21 '19 at 22:16

Even with the example from your own answer I still get some warnings into the .log-file:

pdfTeX warning (ext4): de
stination with the same identifier (name{test.1.1}) has been already used, dupl
icate ignored

[...]

pdfTeX warning (ext4): destination with the same identifier
(name{thm.1.2}) has been already used, duplicate ignored


In my opinion these "warnings" should be error-messages because they indicate severe problems with the way in which one's own code interferes with hyperref's mechanisms for automatically creating names for destinations/anchors for hyperlinks.

The crucial point is:

The mechanisms provided in the hyperref-package for automatically creating names for destinations/anchors for hyperlinks usually derive these names from the names and the current values of the corresponding counters. The hyperref-package does that by means of these nice \theH⟨counter⟩-macros when \refstepcounter is carried out. (Sectioning commands like \section or \caption or environments for numbered theorems and the like internally use \refstepcounter.)

Thus if you approach the matter with innocent mind and have LaTeX decrement a counter just like that, then you walk into one of these nice traps which hyperref keeps in stock for users of cheerful disposition:

After decrementing a counter and having it incremented again in terms of \refstepcounter, hyperref will one more time create the same destination-name/anchor-name. This in turn will lead to pdfTeX-warnings like the warnings above because names of destinations/anchors for hyperlinking should be unique. Uniqueness here means unambiguousness with regard to the question which target to head for when a hyperlink to a destination is clicked/is activated in whatsoever way.

It is still not clear to me what behavior exactly you have in mind.

You wish to re-use an equation number and have ′ or ′′ (etc) appended. (As far as I know the Unicode-name of the symbol ′ is 'PRIME' (U+2032).)

One approach could be:

Define another thm-like-environment which is "numbered within" the thm-environment and redefine its belonging \the⟨counter⟩-macro to expand to \thethm+⟨an amount of ′ which corresponds to the value of that counter⟩.

Then you can use that environment after a thm-environment in order to get equations which have the same number, with ′/′′/′′′/etc appended.

With this approach you are bound to having "prime-variants/derived variants" of theorems occur right behind the belonging "non-prime-variants/non-derived variants".

An implementation of this approach could look like this:

\documentclass{amsart}
\usepackage{hyperref}

\newtheorem{thm}{Theorem}[section]

\makeatletter
%------------------------------------------------------------------------------
% David Kastrup's replicate, see
% http://www.gust.org.pl/projects/pearls/2005p/david-kastrup/bachotex2005-david-kastrup-pearl3.pdf
%------------------------------------------------------------------------------
\newcommand\xii[2]{\if#2m#1\expandafter\xii\else\expandafter\@gobble\fi{#1}}
\@ifdefinable\xiii{\long\def\xiii#1\relax#2{\xii{#2}#1\relax}}
\newcommand\replicate[1]{\expandafter\xiii\romannumeral\number\number#1 000\relax}
%------------------------------------------------------------------------------
% Deliver prime-symbols:
%..............................................................................
% In case the pdf itself is encoded in PDFDocEncoding, there is no symbol "prime"
% available, thus usage of Right Single Quotation Mark. The octal number of
% the code-point of the Right Single Quotation Mark in PdfDocEncoding is 220,
% thus \220.
% In case the pdf itself is encoded in UTF-16BE, the symbol prime is available:
% The codepoint-number in unicode is: 8242(dec)=2032(hex)= 10000000110010(bin).
% Let's split the binary in two 8-bit=byte registers: 100000 00110010.
% Take each register for a number:
% 100000(bin)=32(dec)=40(oct)
% 00110010(bin)=50(dec)=62(oct)
% Let's express each octal number with three digits, i.e., add leading zeros,
% and with the octal representation of the higher byte prepend \9: \9040\062
%------------------------------------------------------------------------------
\DeclareRobustCommand\primesmultiplied[1]{%
\texorpdfstring{%
\ifmmode\expandafter\@firstofone\else\expandafter\ensuremath\fi
{^{\replicate{#1}{\prime}}}%
}{%
\ifHy@unicode\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\replicate{#1}{\9040\062}}{\replicate{#1}{\220}}%
}%
}%
\makeatother

\newcommand{\mynewtheorem}[2]{% #1 environment-name, #2 text-phrase
\newtheorem{#1}[thm]{#2}%
\newtheorem{#1d}{#2}[thm]%
\expandafter\renewcommand\csname the#1d\endcsname{%
\thethm\primesmultiplied{\number\value{#1d}}%
}%
}

\mynewtheorem{foo}{Foo}
\mynewtheorem{car}{Car}

\author{Me, myself and I}
\title{Test}

\begin{document}

\maketitle

\section{Exciting tests}

\begin{foo}\label{foo1.1}
This should yield Foo 1.1
\end{foo}
\vfill

\begin{food}\label{foo1.1'}
This should yield Foo 1.1\/$^{\prime}$
\end{food}
\vfill

\begin{food}\label{foo1.1''}
This should yield Foo 1.1\/$^{\prime\prime}$
\end{food}
\vfill

\begin{foo}\label{foo1.2}
This should yield Foo 1.2
\end{foo}
\vfill

\begin{food}\label{foo1.2'}
This should yield Foo 1.2\/$^{\prime}$
\end{food}
\vfill

\begin{food}\label{foo1.2''}
This should yield Foo 1.2\/$^{\prime\prime}$
\end{food}
\vfill

\begin{car}\label{car1.3}
This should yield Car 1.3
\end{car}
\vfill

\begin{card}\label{car1.3'}
This should yield Car 1.3\/$^{\prime}$
\end{card}
\vfill

\begin{card}\label{car1.3''}
This should yield Car 1.3\/$^{\prime\prime}$
\end{card}
\vfill

\begin{car}\label{car1.4}
This should yield Car 1.4
\end{car}
\vfill

\begin{card}\label{car1.4'}
This should yield Car 1.4\/$^{\prime}$
\end{card}
\vfill

\begin{card}\label{car1.4''}
This should yield Car 1.4\/$^{\prime\prime}$
\end{card}
\vfill

\newpage

referencing:

\verb|\ref{foo1.1}| yields: \ref{foo1.1}

\verb|\ref{foo1.1'}| yields: \ref{foo1.1'}

\verb|\ref{foo1.1''}| yields: \ref{foo1.1''}

\verb|\ref{foo1.2}| yields: \ref{foo1.2}

\verb|\ref{foo1.2'}| yields: \ref{foo1.2'}

\verb|\ref{foo1.2''}| yields: \ref{foo1.2''}

\verb|\ref{car1.3}| yields: \ref{car1.3}

\verb|\ref{car1.3'}| yields: \ref{car1.3'}

\verb|\ref{car1.3''}| yields: \ref{car1.3''}

\verb|\ref{car1.4}| yields: \ref{car1.4}

\verb|\ref{car1.4'}| yields: \ref{car1.4'}

\verb|\ref{car1.4''}| yields: \ref{car1.4''}

\end{document}


If you don't like being bound to having "prime-variants/derived variants" of theorems occur right behind the belonging "non-prime-variants", you need to provide exact rules for creating "prime-variants/derived variants" and for the order in which the sequences of ′ are to occur:

Assume you already have derived "prime-variants" Theorem 1.4′ and Theorem 1.4′′ from Theorem 1.4. This could look like

Theorem 1.4′. This theorem is derived from Theorem 1.4.

Theorem 1.4. This is theorem 1.4.

Theorem 1.4′′. This theorem is also derived from Theorem 1.4.

The question is: How to handle the case of having a "prime-variant" which is to be derived not from Theorem 1.4 but from Theorem 1.4′ ?

You already have a Theorem 1.4′′ derived from Theorem 1.4. So shall the new variant be 1.4′′′? (If so: How to handle the case of already having derived a theorem 1.4′′′ from one of the theorems 1.4/1.4′/1.4′′ ?)

Or shall it be Theorem 1.4′′ and the former Theorem 1.4′′ shall now be Theorem 1.4′′′, etc?

Probably clarification is needed regarding the exact rules for attaching prime-symbols ′ to numbers/labels of theorems when deriving such numbers/labels one from another.

Unfortunately one cannot see from your working example how you intend to have things turn out in such cases.

A starting point for an approach which is based on providing referencing-labels can be something like this:

\documentclass{amsart}
\usepackage{hyperref}
\usepackage{refcount}[2016/05/16]

\newtheorem{thm}{Theorem}[section]

\makeatletter
%------------------------------------------------------------------------------
% David Kastrup's replicate, see
% http://www.gust.org.pl/projects/pearls/2005p/david-kastrup/bachotex2005-david-kastrup-pearl3.pdf
%------------------------------------------------------------------------------
\newcommand\xii[2]{\if#2m#1\expandafter\xii\else\expandafter\@gobble\fi{#1}}
\@ifdefinable\xiii{\long\def\xiii#1\relax#2{\xii{#2}#1\relax}}
\newcommand\replicate[1]{\expandafter\xiii\romannumeral\number\number#1 000\relax}
%------------------------------------------------------------------------------
% Expandable incrementing of natural number formed by a sequence of
% explicit catcode-12-character-tokens-from-the-set {0,1,2,3,4,5,6,7,8,9}
%..............................................................................
% \UD@Increment{<natural number k as sequence of explicit catcode-12-character-
%                tokens from the set 0123456789>}
% ->
% <natural number (k+1) as sequence of explicit catcode-12-character-tokens
%  from the set 0123456789>
%------------------------------------------------------------------------------
\newcommand\UD@Increment[1]{%
\romannumeral0%
\UD@IncrementReverse{\UD@IncrementFork{}}{\relax}{}#1\relax
}%
\newcommand\UD@IncrementReverse[4]{%
\ifx\relax#4%
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{#1#3#2}{\UD@IncrementReverse{#1}{#2}{#4#3}}%
}%
\@ifdefinable\UD@IncrementSelect{%
\long\def\UD@IncrementSelect#10123456789\relax#2#3!!{#2}%
}%
\newcommand\UD@IncrementFork[2]{%
\UD@IncrementSelect
#2123456789\relax{\UD@IncrementReverse{ }{}{}#11}%
0#223456789\relax{\UD@IncrementReverse{ }{}{}#12}%
01#23456789\relax{\UD@IncrementReverse{ }{}{}#13}%
012#2456789\relax{\UD@IncrementReverse{ }{}{}#14}%
0123#256789\relax{\UD@IncrementReverse{ }{}{}#15}%
01234#26789\relax{\UD@IncrementReverse{ }{}{}#16}%
012345#2789\relax{\UD@IncrementReverse{ }{}{}#17}%
0123456#289\relax{\UD@IncrementReverse{ }{}{}#18}%
01234567#29\relax{\UD@IncrementReverse{ }{}{}#19}%
012345678#2\relax{\UD@IncrementFork{#10}}%
0123456789#2{\UD@IncrementReverse{ }{}{}#11\relax}%
0123456789\relax{\UD@IncrementReverse{ }{}{}#11#2}%
!!%
}%
%------------------------------------------------------------------------------
% Deliver prime-symbols:
%..............................................................................
% In case the pdf itself is encoded in PDFDocEncoding, there is no symbol "prime"
% available, thus usage of Right Single Quotation Mark. The octal number of
% the code-point of the Right Single Quotation Mark is 220, thus \220.
% In case the pdf itself is encoded in UTF-16BE, the symbol prime is available:
% The codepoint-number in unicode is: 8242(dec)=2032(hex)= 10000000110010(bin).
% Let's split the binary in two 8-bit=byte registers: 100000 00110010.
% Take each register for a number:
% 100000(bin)=32(dec)=40(oct)
% 00110010(bin)=50(dec)=62(oct)
% Let's express each number with three digits, i.e., add leading zeros, and
% with the higher byte prepend \9: \9040\062
%------------------------------------------------------------------------------
\DeclareRobustCommand\primesmultiplied[1]{%
\texorpdfstring{%
\ifmmode\expandafter\@firstofone\else\expandafter\ensuremath\fi
{^{\replicate{#1}{\prime}}}%
}{%
\ifHy@unicode\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\replicate{#1}{\9040\062}}{\replicate{#1}{\220}}%
}%
}%
%------------------------------------------------------------------------------
% Robust action in case label is undefined.
%..............................................................................
\DeclareRobustCommand\LabelUndefinedAction{%
\texorpdfstring{\nfss@text{\reset@font\bfseries??}}{??}%
}%
%------------------------------------------------------------------------------
% \mynewtheorem
%..............................................................................
\newcommand{\mynewtheorem}[2]{% #1 environment-name, #2 text-phrase
\newtheorem{#1}[thm]{#2}%
\newtheorem{#1dthm}{#2}%
\newenvironment{#1d}[1]{%
\@ifundefined{#1d@##1}{%
\global\@namedef{#1d@##1}{1}%
}{%
\expandafter\xdef\csname#1d@##1%
\expandafter\expandafter\expandafter\endcsname
\expandafter\expandafter\expandafter{%
\expandafter\expandafter\expandafter\UD@Increment
\expandafter\expandafter\expandafter{\csname#1d@##1\endcsname}}%
}%
\IfRefUndefinedBabel{##1}{\refused{##1}}{}%
\expandafter\renewcommand\csname the#1dthm\endcsname{%
\getrefbykeydefault{##1}{}{\LabelUndefinedAction}%
\primesmultiplied{\number\csname#1d@##1\endcsname}%
}%
\csname #1dthm\endcsname
}{%
\csname end#1dthm\endcsname
}%
}%
\makeatother

\mynewtheorem{foo}{Foo}
\mynewtheorem{car}{Car}

\author{Me, myself and I}
\title{Test}

\begin{document}

\maketitle

\section{Exciting tests}

\begin{foo}\label{foo1.1}
This should yield Foo 1.1
\end{foo}
\vfill

\begin{food}{foo1.1}\label{foo1.1'}
This should yield Foo 1.1\/$^{\prime}$
\end{food}
\vfill

\begin{food}{foo1.1}\label{foo1.1''}
This should yield Foo 1.1\/$^{\prime\prime}$
\end{food}
\vfill

\begin{foo}\label{foo1.2}
This should yield Foo 1.2
\end{foo}
\vfill

\begin{food}{foo1.2}\label{foo1.2'}
This should yield Foo 1.2\/$^{\prime}$
\end{food}
\vfill

\begin{food}{foo1.2}\label{foo1.2''}
This should yield Foo 1.2\/$^{\prime\prime}$
\end{food}
\vfill

\begin{card}{car1.3}\label{car1.3'}
This should yield Car 1.3\/$^{\prime}$
\end{card}
\vfill

\begin{card}{car1.3}\label{car1.3''}
This should yield Car 1.3\/$^{\prime\prime}$
\end{card}
\vfill

\begin{car}\label{car1.3}
This should yield Car 1.3
\end{car}
\vfill

\begin{card}{car1.4}\label{car1.4'}
This should yield Car 1.4\/$^{\prime}$
\end{card}
\vfill

\begin{card}{car1.4}\label{car1.4''}
This should yield Car 1.4\/$^{\prime\prime}$
\end{card}
\vfill

\begin{car}\label{car1.4}
This should yield Car 1.4
\end{car}
\vfill

\newpage

referencing:

\verb|\ref{foo1.1}| yields: \ref{foo1.1}

\verb|\ref{foo1.1'}| yields: \ref{foo1.1'}

\verb|\ref{foo1.1''}| yields: \ref{foo1.1''}

\verb|\ref{foo1.2}| yields: \ref{foo1.2}

\verb|\ref{foo1.2'}| yields: \ref{foo1.2'}

\verb|\ref{foo1.2''}| yields: \ref{foo1.2''}

\verb|\ref{car1.3}| yields: \ref{car1.3}

\verb|\ref{car1.3'}| yields: \ref{car1.3'}

\verb|\ref{car1.3''}| yields: \ref{car1.3''}

\verb|\ref{car1.4}| yields: \ref{car1.4}

\verb|\ref{car1.4'}| yields: \ref{car1.4'}

\verb|\ref{car1.4''}| yields: \ref{car1.4''}

\end{document}


egreg in his impressive answer already provided shorter code for doing things in a similar way by means of expl3.

But I am sceptical with this approach because it leaves room for ambiguities:

E.g., you can (with at least five compilations) produce two equations Foo 1.1'' and four equations Foo 1.1′′' as follows:

\documentclass{amsart}
\usepackage{hyperref}
\usepackage{refcount}[2016/05/16]

\newtheorem{thm}{Theorem}[section]

\makeatletter
%------------------------------------------------------------------------------
% David Kastrup's replicate, see
% http://www.gust.org.pl/projects/pearls/2005p/david-kastrup/bachotex2005-david-kastrup-pearl3.pdf
%------------------------------------------------------------------------------
\newcommand\xii[2]{\if#2m#1\expandafter\xii\else\expandafter\@gobble\fi{#1}}
\@ifdefinable\xiii{\long\def\xiii#1\relax#2{\xii{#2}#1\relax}}
\newcommand\replicate[1]{\expandafter\xiii\romannumeral\number\number#1 000\relax}
%------------------------------------------------------------------------------
% Expandable incrementing of natural number formed by a sequence of
% explicit catcode-12-character-tokens-from-the-set {0,1,2,3,4,5,6,7,8,9}
%..............................................................................
% \UD@Increment{<natural number k as sequence of explicit catcode-12-character-
%                tokens from the set 0123456789>}
% ->
% <natural number (k+1) as sequence of explicit catcode-12-character-tokens
%  from the set 0123456789>
%------------------------------------------------------------------------------
\newcommand\UD@Increment[1]{%
\romannumeral0%
\UD@IncrementReverse{\UD@IncrementFork{}}{\relax}{}#1\relax
}%
\newcommand\UD@IncrementReverse[4]{%
\ifx\relax#4%
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{#1#3#2}{\UD@IncrementReverse{#1}{#2}{#4#3}}%
}%
\@ifdefinable\UD@IncrementSelect{%
\long\def\UD@IncrementSelect#10123456789\relax#2#3!!{#2}%
}%
\newcommand\UD@IncrementFork[2]{%
\UD@IncrementSelect
#2123456789\relax{\UD@IncrementReverse{ }{}{}#11}%
0#223456789\relax{\UD@IncrementReverse{ }{}{}#12}%
01#23456789\relax{\UD@IncrementReverse{ }{}{}#13}%
012#2456789\relax{\UD@IncrementReverse{ }{}{}#14}%
0123#256789\relax{\UD@IncrementReverse{ }{}{}#15}%
01234#26789\relax{\UD@IncrementReverse{ }{}{}#16}%
012345#2789\relax{\UD@IncrementReverse{ }{}{}#17}%
0123456#289\relax{\UD@IncrementReverse{ }{}{}#18}%
01234567#29\relax{\UD@IncrementReverse{ }{}{}#19}%
012345678#2\relax{\UD@IncrementFork{#10}}%
0123456789#2{\UD@IncrementReverse{ }{}{}#11\relax}%
0123456789\relax{\UD@IncrementReverse{ }{}{}#11#2}%
!!%
}%
%------------------------------------------------------------------------------
% Deliver prime-symbols:
%..............................................................................
% In case the pdf itself is encoded in PDFDocEncoding, there is no symbol "prime"
% available, thus usage of Right Single Quotation Mark. The octal number of
% the code-point of the Right Single Quotation Mark is 220, thus \220.
% In case the pdf itself is encoded in UTF-16BE, the symbol prime is available:
% The codepoint-number in unicode is: 8242(dec)=2032(hex)= 10000000110010(bin).
% Let's split the binary in two 8-bit=byte registers: 100000 00110010.
% Take each register for a number:
% 100000(bin)=32(dec)=40(oct)
% 00110010(bin)=50(dec)=62(oct)
% Let's express each number with three digits, i.e., add leading zeros, and
% with the higher byte prepend \9: \9040\062
%------------------------------------------------------------------------------
\DeclareRobustCommand\primesmultiplied[1]{%
\texorpdfstring{%
\ifmmode\expandafter\@firstofone\else\expandafter\ensuremath\fi
{^{\replicate{#1}{\prime}}}%
}{%
\ifHy@unicode\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\replicate{#1}{\9040\062}}{\replicate{#1}{\220}}%
}%
}%
%------------------------------------------------------------------------------
% Robust action in case label is undefined.
%..............................................................................
\DeclareRobustCommand\LabelUndefinedAction{%
\texorpdfstring{\nfss@text{\reset@font\bfseries??}}{??}%
}%
%------------------------------------------------------------------------------
% \mynewtheorem
%..............................................................................
\newcommand{\mynewtheorem}[2]{% #1 environment-name, #2 text-phrase
\newtheorem{#1}[thm]{#2}%
\newtheorem{#1dthm}{#2}%
\newenvironment{#1d}[1]{%
\@ifundefined{#1d@##1}{%
\global\@namedef{#1d@##1}{1}%
}{%
\expandafter\xdef\csname#1d@##1%
\expandafter\expandafter\expandafter\endcsname
\expandafter\expandafter\expandafter{%
\expandafter\expandafter\expandafter\UD@Increment
\expandafter\expandafter\expandafter{\csname#1d@##1\endcsname}}%
}%
\IfRefUndefinedBabel{##1}{\refused{##1}}{}%
\expandafter\renewcommand\csname the#1dthm\endcsname{%
\getrefbykeydefault{##1}{}{\LabelUndefinedAction}%
\primesmultiplied{\number\csname#1d@##1\endcsname}%
}%
\csname #1dthm\endcsname
}{%
\csname end#1dthm\endcsname
}%
}%
\makeatother

\mynewtheorem{foo}{Foo}
\mynewtheorem{car}{Car}

\author{Me, myself and I}
\title{Test}

\begin{document}

\maketitle

\section{Exciting tests}

\begin{foo}\label{foo1.1}
This should yield Foo 1.1
\end{foo}
\vfill

\begin{food}{foo1.1}\label{foo1.1'}
This should yield Foo 1.1\/$^{\prime}$
\end{food}
\vfill

\begin{food}{foo1.1}\label{foo1.1''}
This should yield Foo 1.1\/$^{\prime\prime}$, the 1st.
\end{food}
\vfill

\begin{food}{foo1.1'}\label{foo1.1''b}
This should yield Foo 1.1\/$^{\prime\prime}$, the 2nd.
\end{food}
\vfill

\begin{food}{foo1.1}\label{foo1.1'''}
This should yield Foo 1.1\/$^{\prime\prime\prime}$, the 1st.
\end{food}
\vfill

\begin{food}{foo1.1'}\label{foo1.1'''b}
This should yield Foo 1.1\/$^{\prime\prime\prime}$, the 2nd.
\end{food}
\vfill

\begin{food}{foo1.1''}\label{foo1.1'''c}
This should yield Foo 1.1\/$^{\prime\prime\prime}$, the 3rd.
\end{food}
\vfill

\begin{food}{foo1.1''b}\label{foo1.1'''d}
This should yield Foo 1.1\/$^{\prime\prime\prime}$, the 4th..
\end{food}
\vfill

\end{document}


• I will analyse this excellent looking answer later today or tomorrow. Thank you. For now I have a basic question based on your initial piece: In a longer document, how do I find important warnings when the output is always seemingly 1000 lines long and full of, you know, overfull or underfull badness 66666? Do you experts have a technique? – Richard Birkett Sep 21 '19 at 17:57
• My technique is not really at expert-level: I load the .log-file in a text-editor and use the editor's search function for finding the phrases "warning" and "error". Besides this warnings about missing destinations/destinations with same identifier etc are always close to the end of the .log-file. – Ulrich Diez Sep 21 '19 at 20:24
• TeXBook, Chapter 6: Running TeX says: "...TeX reports all boxes whose badness exceeds a certain threshold called \hbadness..." TeXBook, Chapter 24: Summary of Vertical Mode says: "...\hfuzz (maximum overrun before overfull hbox messages occur)...". In order to not have over-/undefull hbox-warnings intersperse the .log-file you can for one LaTeX run (temporarily) set \hbadness=10000 and \hfuzz=\maxdimen and then examine the .log-file which results from this LaTeX run. (There is also \vbadness and \vfuzz.) You won't have these box-warnings but you still have the other warnings. – Ulrich Diez Sep 21 '19 at 21:01

Ok it's a little embarrassing, apologies to @UlrichDiez who probably spotted this and then took my bad reply.

So I added in some of my old code and slowly got more hints what was wrong. It turned out I had the idea of #1 and ##1 the wrong way around. The counter issue was still a problem, but somehow adding the alias counter (from original code) helped the problem. This is interesting because I had been using it to get around a completely separate problem where it wouldn't write a prime in the autoref. This is nonetheless unsurprising. Also note the autorefname line of code is not necessary unless you want \autoref to work, the rest is the true solution. Finally observe that I changed the \renewcommand of \thethm since it should have been more generally named as \the<#1> e.g. \thetest.

For those that read this, perhaps the following is a nice useful bit of code to give you primed theorem names. :)

\documentclass{amsart}

\usepackage{hyperref, aliascnt}

\author{Richard}
\title{Test}

\newtheorem{thm}{Theorem}[section]

%No more problems
\newcommand{\mynewtheorem}[3]{%token, long name, short name
\expandafter\def\csname#1autorefname\endcsname{#3}
\newaliascnt{#1}{thm}
\newtheorem{#1}[#1]{#2}
\aliascntresetthe{#1}
\newenvironment{#1d}[1]
{\expandafter\def\csname the#1\endcsname{\ref*{##1}$'$}%
\begin{#1}\label{##1'}}
{\end{#1}}
}

%Works
\newenvironment{thmd}[1]
{\renewcommand{\thethm}{\ref*{#1}$'$}%
\begin{thm}\label{#1'}}
{\end{thm}}

\mynewtheorem{test}{Test}{Tst}

\begin{document}

\maketitle

\section{Annoying tests}

\begin{test}\label{testlabel}
A good test.
\end{test}

\begin{testd}{testlabel}
A truly prime test?
\end{testd}

Let's reference \ref{testlabel} and also \autoref{testlabel'}

\begin{thm}\label{mylabel}
A theorem.
\end{thm}

\begin{thmd}{mylabel}
A theorem'. This works nicely!
\end{thmd}

\end{document}


Thanks to all.