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}
\end{##1}
as##1
is not available at the end of an environment. You can use theenviron
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}
.\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.\mynewtheorem{test}{Test}
beneath other things tries to define an environmenttestd
wherein the macro\thethm
is defined to refer to a referencing-label whose name istest
. 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.%Works
example for what I intend and how it usually works.\thethm
is being set to the input of the environment, NOTtest
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 exceptthm
andthmd
are replaced by##1' and
##1d`.##1
and##1d
. Gosh I did find one problem with the counter. That should have been##1
not a#1
for the counter!