1

I have a small number of examples (3, 4, or 5 maybe) which are repeated throughout my document to help the reader understand new concepts as they are introduced using the same set of examples.

I would like to label these with letters but then incremented with numbers as they are introduced multiple times. Let’s say I have two examples, A and B. Then in section 1 I would have Example A.1 and Example B.1. In a later section (not necessarily Section 2) I would examine them again and have Example A.2 and Example B.2.

When I reference these I would like them referenced in the same way, so \ref{some_label_A1} would show A.1.

I can get this working without the .1 and .2s by redefining a theorem and passing the letter as a parameter, I.e. \begin{example}{A}. However, as an extra complication, I would rather do it using \newenvironment because I want the the formatting in the environment to be identical to the rest of the document (same font etc) and I can’t find a nice way to do that without digging all of the information from the cls file. This will need to be submitted in multiple formats using different cls files and I don’t want to copy the fonts etc from the cls file each time.

I would be happy to define two different new environments (one for A and one for B). It’s just the referencing I cannot get right when using newenvironment. I can’t seem to get the reference to actually give me A.1.

Can somebody give such an example showing a newenvironment with this sort of labelling and successful referencing?

Edit: Really I could achieve this just by custom numbering some subsections, but seemed to me like there should also be a good way to do this using environments and counters.

3
  • Wouldn't it make more sense to number them 1.A, B.1 and 2.A, 2.B as usual? And can you please show, what you already have?
    – cabohah
    Sep 20, 2023 at 15:59
  • I will add some code when I can, but I definitely want A.1, A.2 etc. as it’s much better presented that way.
    – SEP101
    Sep 20, 2023 at 16:05
  • What, precisely, isn't working with my solution? I added an image of the output with some context.
    – egreg
    Sep 21, 2023 at 9:23

1 Answer 1

0

In this solution I define two environments, example and example*, the latter for continuations.

Both environments take a mandatory argument which is an arbitrary label for referring to the current example or the original one in case of continuation. In the continuations you can use a standard \label command for setting a reference. The numbering is completely independent on sections.

\documentclass{article}
\usepackage{amsthm}
\usepackage{refcount}
\usepackage{hyperref}

\usepackage{lipsum}

\theoremstyle{definition}

\newtheorem{exampleinner}{Example}
\newcounter{example}
\renewcommand{\theexample}{\Alph{example}}

\makeatletter
\newenvironment{example}[1]
 {%
  \stepcounter{example}%
  \newcounter{example@#1}%
  \stepcounter{example@#1}%
  \renewcommand{\theexampleinner}{\theexample.\arabic{example@#1}}
  \exampleinner
  \label{example@full@#1}%
  \def\@currentlabel{\theexample}\label{example@short@#1}%
 }
 {\endexampleinner}

\newenvironment{example*}[1]
 {%
  \stepcounter{example@#1}%
  \renewcommand{\theexampleinner}{\getrefnumber{example@short@#1}.\arabic{example@#1}}%
  \exampleinner
 }
 {\endexampleinner}

\newcommand{\exaref}[1]{\ref{example@full@#1}}
\makeatother

\begin{document}

\lipsum[1][1-4]

\begin{example}{first}
This is the first example.
\lipsum[2][1-3]
\end{example}

\lipsum[1][1-4]

\begin{example}{second}
This is the second example.
\lipsum[2][1-3]
\end{example}

\lipsum[1][1-4]

\begin{example*}{first}
This is the continuation of \exaref{first}.
\lipsum[2][1-3]
\end{example*}

\lipsum[1][1-4]

\begin{example*}{second}\label{xyz}
This is the continuation of \exaref{second}.
\lipsum[2][1-3]
\end{example*}

\lipsum[1][1-4]

\begin{example}{third}
This is an altogether new series of examples.
\lipsum[2][1-3]
\end{example}

\lipsum[1][1-4]

\begin{example*}{first}
This is the continuation of \exaref{first}.
\lipsum[2][1-3]
\end{example*}

\lipsum[1][1-4]

\begin{example*}{second}
This is the continuation of \exaref{second}.
\lipsum[2][1-3]
\end{example*}

\lipsum[1][1-4]

\begin{example*}{third}
This is the continuation of \exaref{third},
but we refer to example~\ref{xyz}
\lipsum[2][1-3]
\end{example*}

\lipsum[1][1-4]

\end{document}

Note that hyperref is used only to show the code is compatible with it. Also I load geometry in order to fit the picture in one page.

enter image description here

4
  • Defining the continuation environment is interesting, thank you. I need to read more carefully to fully understand the referencing here, but I assume that some of the work is being done by the use of the new theorem. I was hoping to avoid that as I don’t want to have theorem formatting, I wanted the text in my environment to look exactly the same as any other body text. Any thoughts on that?
    – SEP101
    Sep 20, 2023 at 16:57
  • @SEP101 Doesn't it look like normal text?
    – egreg
    Sep 20, 2023 at 17:13
  • Yes it does look like normal text, apologies! There are two tweaks I would like to make in my case. One (easier?) one would be a way to reference the continuation examples. The second (harder?) is to not have the numbering following the sections, I may go many sections without an example but want it to follow on numerically when I do.
    – SEP101
    Sep 22, 2023 at 13:16
  • @SEP101 Now the code should do what you want, sorry for having misunderstood the specifications.
    – egreg
    Sep 22, 2023 at 14:26

You must log in to answer this question.

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