用于编写由参数命名的新环境的宏

用于编写由参数命名的新环境的宏

我正在编写一个 sty,它会产生许多定理。我制作了一个(到目前为止有效的)宏来加速每个新定理的进程。为了方便起见,这是我的问题的一个最小示例,没有 sty 或我定义的任何其他垃圾。

发生的事情是我收到错误

\endtestd 定义中的参数非法

如您所见,我试图定义一个宏,它不仅定义一个定理,还定义一个类似的环境,该环境通过附加“d”和带破折号的函数来命名。请注意,当我使用类型化的环境名称时,这种代码通常会起作用;thmd 的另一个定义表明这通常有效(您可以注释掉 mynewtheorem 定义、\mynewtheorem{test}{Test} 和 begin/end{test} 以查看它是否有效)。

\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}$'$}%
  \addtocounter{##1}{-1}%
  \begin{##1}\label{#1'}
  }
 {\end{##1}}
}

%Works
\newenvironment{thmd}[1]
 {\renewcommand{\thethm}{\ref*{#1}$'$}%
  \addtocounter{thm}{-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}

这让我想起了 \csname 问题的宏名称,但我不知道该怎么办。

答案1

你可以更直接地做到这一点:

\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}

在此处输入图片描述

经过一些修改,但基本思想是一样的,我们可以使得\begin{thmd}{<label>}使用相同标签的连续调用首先产生一个素数,然后产生一个双素数等等。

对于每个被调用的标签,都会在属性列表中创建一个条目,其中包含所需素数的数量;每次调用时此数字都会增加。

\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}

在此处输入图片描述

答案2

即使来自您自己的答案的示例我仍然在 .log 文件中收到一些警告:

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

在我看来,这些“警告”应该是错误消息,因为它们表明自己的代码干扰了 hyperref 自动为超链接的目标/锚点创建名称的机制,存在严重问题。

关键点是:

hyperref-package 中提供的用于自动创建超链接目标/锚点名称的机制通常从相应计数器的名称和当前值中派生出这些名称。 hyperref-package在执行时通过这些不错的 -macros来实现这一点。 (分段命令(例如或或编号定理的环境等)在内部使用。)\theH⟨counter⟩\refstepcounter\section\caption\refstepcounter

因此,如果你以无知的心态处理这个问题,并让 LaTeX 以这样的方式减少计数器,那么你就会陷入 hyperref 为开朗性格的用户准备的这些陷阱之一:

在计数器减少并再次增加之后\refstepcounter,hyperref 将再次创建相同的目标名称/锚点名称。这反过来会导致类似上述警告的 pdfTeX 警告,因为超链接的目标/锚点名称应该是唯一的。这里的唯一性意味着当以任何方式单击/激活指向目标的超链接时,对于要前往哪个目标的问题的明确性。


我还是不清楚你到底想的是哪种行为。

您希望重新使用公式编号并附加′′(等等)。(据我所知,该符号的 Unicode 名称'PRIME' (U+2032)。)

一种方法可能是:

定义另一个在 thm 环境内“编号”的 thm-like-environment,并重新定义其所属的-macro 以扩展为。\the⟨counter⟩\thethm+⟨an amount of which corresponds to the value of that counter⟩

然后你可以使用该环境athm环境以获得具有相同编号的方程式,并附加/ ′′/ ′′′/等。

采用这种方法,你必然会在所属的“非素数变体/非派生变体”之后出现定理的“素数变体/派生变体”。

这种方法的实现可能如下所示:

\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}

在此处输入图片描述

在此处输入图片描述

如果您不喜欢被束缚于定理的“主要变体/派生变体”紧随其后的“非主要变体”,那么您需要提供创建“主要变体/派生变体”的确切规则以及序列出现的顺序

假设你已经从定理 1.4 推导出“主要变体”定理 1.4′ 和定理 1.4′′。这可能看起来像

定理 1.4′. 该定理由定理 1.4 推导而来。

定理 1.4. 这是定理 1.4。

定理1.4′′。该定理也是由定理1.4推导出来的。

问题是:如何处理存在不是由定理 1.4 而是由定理 1.4′ 推导出的“原变量”的情况?

您已经有了从定理 1.4 推导出的定理 1.4′′。那么新的变体应该是 1.4′′′ 吗?(如果是这样:如何处理已经从定理 1.4/1.4′/1.4′′ 之一推导出定理 1.4′′′ 的情况?)

或者应该是定理 1.4′′,而原来的定理 1.4′′ 现在应该是定理 1.4′′′,等等?

可能需要澄清在从定理的数字/标签中得出这些数字/标签时将素数符号附加到定理的数字/标签上的确切规则。

不幸的是,从您的工作示例中我们无法看出您打算在这种情况下如何让事情出现结果。


基于提供参考标签的方法的起点可以是这样的:

\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}

在此处输入图片描述

在此处输入图片描述

埃格尔在他令人印象深刻的回答中已经提供了通过 expl3 以类似方式执行操作的更短代码。

但我对这种方法持怀疑态度,因为它留下了歧义:

例如,您可以(使用至少五次编译)生成两个方程式 Foo 1.1'' 和四个方程式 Foo 1.1′′',如下所示:

\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}

在此处输入图片描述

答案3

好吧,这有点尴尬,向@UlrichDiez 道歉,他可能发现了这一点,然后接受了我的错误回复。

所以我添加了一些旧代码,并慢慢地得到了更多关于哪里出错的提示。结果发现我的想法是错误的#1##1计数器问题仍然是一个问题,但不知何故添加别名计数器(来自原始代码)有助于解决问题。这很有趣,因为我一直在用它来解决一个完全独立的问题,即它不会在 autoref 中写入素数。不过这并不奇怪。还要注意,autorefname除非你想工作,否则这行代码是不必要的\autoref,剩下的才是真正的解决方案。最后观察一下,我改变了\renewcommandof ,\thethm因为它应该更普遍地命名为\the<#1>eg \thetest

对于读过这篇文章的人来说,也许下面的一段代码很有用,可以给你提供引数定理的名称。:)

\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}$'$}%
  \addtocounter{#1}{-1}%
  \begin{#1}\label{##1'}}
 {\end{#1}}
}

%Works
\newenvironment{thmd}[1]
 {\renewcommand{\thethm}{\ref*{#1}$'$}%
  \addtocounter{thm}{-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}

谢谢大家。

相关内容