继续示例,重复第一次给出的标题

继续示例,重复第一次给出的标题

我有几个例子(使用 amsmath 中的定理环境)必须沿着文本继续,我想通过“示例编号”+“第一次给出的名称”来引用它们。例如:

示例1(示例名称)

更多讨论

示例 1 的延续 (示例名称) ...

我发现这里如何实现第一部分(下面的代码),通过“示例编号”引用,但我不知道如何恢复示例名称。

\documentclass{book}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}[chapter]
\newtheorem{example}[theorem]{Example}
\newtheorem*{continuancex}{Continuance of Example \continuanceref}
\newenvironment{continuance}[1]
{\newcommand\continuanceref{\ref{#1}}\continuancex}{\endcontinuancex}

\begin{document}
\chapter{Title}
\begin{example} \label{ex1}
     This is an example.
\end{example}

Some text stands here. And then there is some space between this and the continued example.
Further should the continued example not be intended but start at the beginning of the line.

\begin{continuance}{ex1}
    This is the continued example.
\end{continuance}

Some more text, which should follow after a little vertical space.
\end{document}

答案1

您可以使用\getnamereftext当然,还有nameref(由 自动加载hyperref)。

\documentclass{book}
\usepackage{amsthm}
\usepackage{nameref}% or hyperref

\newtheorem{theorem}{Theorem}[chapter]
\newtheorem{example}[theorem]{Example}
\newtheorem*{continuancex}{Continuance of Example \continuanceref}

\makeatletter
\newcommand{\getnamereftext}[1]{%
  \@ifundefined{r@#1}{}{%
    \unexpanded\expandafter\expandafter\expandafter{%
      \expandafter\expandafter\expandafter\@thirdoffive\csname r@#1\endcsname
    }%
  }%
}
\makeatletter

\newenvironment{continuance}[1]{%
  \newcommand\continuanceref{\ref{#1}}%
  \if\relax\getnamereftext{#1}\relax
    \continuancex
  \else
    \continuancex[\nameref{#1}]%
  \fi
}{\endcontinuancex}

\begin{document}

\chapter{Title}

\begin{example}\label{ex0}
This example has no title
\end{example}

Some text in between

\begin{continuance}{ex0}
Something else
\end{continuance}


\begin{example}[Description] \label{ex1}
     This is an example.
\end{example}

Some text stands here. And then there is some space between this and the continued example.
Further should the continued example not be intended but start at the beginning of the line.

\begin{continuance}{ex1}
    This is the continued example.
\end{continuance}

Some more text, which should follow after a little vertical space.

\end{document}

在此处输入图片描述

没有\expandafter和朋友:

\documentclass{book}
\usepackage{amsthm}
\usepackage{nameref}% or hyperref

\newtheorem{theorem}{Theorem}[chapter]
\newtheorem{example}[theorem]{Example}
\newtheorem*{continuancex}{Continuance of Example \continuanceref}
\newcommand{\continuanceref}{}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\getnamereftext}{m}
 {
  \tl_if_exist:cT { r@#1 }% check whether \ref is known
   {% in this case extract the third item
    \tl_item:cn { r@#1 } { 3 }
   }
 }

\NewDocumentEnvironment{continuance}{m}
 {
  \renewcommand\continuanceref{\ref{#1}}%
  \tl_if_blank:eTF { \getnamereftext{#1} }
    { \continuancex }
    { \continuancex[\nameref{#1}] }
 }
 {\endcontinuancex}

\ExplSyntaxOff

\begin{document}

\chapter{Title}

\begin{example}\label{ex0}
This example has no title
\end{example}

Some text in between

\begin{continuance}{ex0}
Something else
\end{continuance}


\begin{example}[Description] \label{ex1}
     This is an example.
\end{example}

Some text stands here. And then there is some space between this and the continued example.
Further should the continued example not be intended but start at the beginning of the line.

\begin{continuance}{ex1}
    This is the continued example.
\end{continuance}

Some more text, which should follow after a little vertical space.
\end{document}

相关内容