2

I have several examples (using the theorem environent in amsmath) that must continue along the text and I would like to referece them by "example number" + "name given the fist time". For instance:

Example 1 (Example name)

Some more discussion

Continuance of Example 1 (Example name) ...

I found here how to implement the fist part (code below), which reference by "example number" but I don't know how to recover the example name.

\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}
2
  • What do you mean by "example name?" The thing that is often supplied as an optional argument? As in, \begin{example}[title]...\end{example}.
    – Werner
    May 2, 2023 at 16:20
  • If you know you're going to use the name again, you could just save it as \newcommand{\exmaneA}{<repeatable text>}. May 2, 2023 at 16:27

1 Answer 1

3

You can use \getnamereftext and, of course, nameref (automatically loaded by 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}

enter image description here

With no \expandafter and friends:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .