0

I have three main examples in a paper that I keep coming back to. So, I want to structure things as below. One solution that inverts the numbering/lettering (eg Example A1, A2, B1, B2, etc.) is given in this answer, but I prefer the style below. To clarify, the solution needs to be compatible with hyperref and cross-refs in general.

I'm also open to other creative solutions for this problem.

STRUCTURE

Example 1A. Here is the definition of the first example with some elementary properties.

Example 2A. Here is the definition of the second example with some elementary properties.

Example 3A. Here is the definition of the third example with some elementary properties.

(after introducing Theorem 1, etc)

Example 1B. An example application of Theorem 1 to Example 1.

Example 3B. An example application of Theorem 1 to Example 3.

(after introducing Theorem 2, etc)

Example 1C. An example application of Theorem 2 to Example 1.

(somewhere later in the paper)

Example 2B. An example application of something else to Example 2.

(Edit: Modified example to emphasize that the numbering/lettering is not structured.)

3 Answers3

1

I believe you need to plan in advance how many kinds of examples you have in mind.

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

\newtheorem{theorem}{Theorem}

\newcommand{\newexample}[1]{% \theoremstyle{definition} % upright type for examples \newtheorem{example#1}{Example}% \expandafter\renewcommand\csname theexample#1\endcsname{#1\Alph{example#1}}% }

\newexample{1} \newexample{2} \newexample{3}

\begin{document}

\begin{example1}\label{ex1-first} Here is the definition of the first example with some elementary properties. \end{example1}

\begin{example2} Here is the definition of the second example with some elementary properties. \end{example2}

\begin{example3} Here is the definition of the third example with some elementary properties. \end{example3}

\begin{theorem} This is the first theorem \end{theorem}

\begin{example1} An example application of Theorem 1 to Example 1. \end{example1}

\begin{example2}\label{ex2-second} An example application of Theorem 1 to Example 2. \end{example2}

\begin{example3} An example application of Theorem 1 to Example 3. \end{example3}

\begin{theorem} This is the second theorem \end{theorem}

\begin{example1} An example application of Theorem 2 to Example 1. \end{example1}

\begin{example2} An example application of Theorem 2 to Example 2. \end{example2}

\begin{example3} An example application of Theorem 2 to Example 3. \end{example3}

\ref{ex1-first} and \ref{ex2-second}

\end{document}

enter image description here

If you always have the same structure, that is, the three example varieties always coming together, you might do like the following.

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

\newtheorem{theorem}{Theorem}

\theoremstyle{definition} % upright type for examples \newtheorem{exampleinner}{Example}

\newcounter{examples} \newcounter{example} \newenvironment{examples} {\setcounter{examples}{0}\stepcounter{example}} {} \newenvironment{example} {% \renewcommand{\theexampleinner}{\theexamples\Alph{example}}% \stepcounter{examples}% \exampleinner } {\endexampleinner}

\begin{document}

\begin{examples} \begin{example}\label{ex1-first} Here is the definition of the first example with some elementary properties. \end{example}

\begin{example} Here is the definition of the second example with some elementary properties. \end{example}

\begin{example} Here is the definition of the third example with some elementary properties. \end{example} \end{examples}

\begin{theorem} This is the first theorem \end{theorem}

\begin{examples} \begin{example} An example application of Theorem 1 to Example 1. \end{example}

\begin{example}\label{ex2-second} An example application of Theorem 1 to Example 2. \end{example}

\begin{example} An example application of Theorem 1 to Example 3. \end{example} \end{examples}

\begin{theorem} This is the second theorem \end{theorem}

\begin{examples} \begin{example} An example application of Theorem 2 to Example 1. \end{example}

\begin{example} An example application of Theorem 2 to Example 2. \end{example}

\begin{example} An example application of Theorem 2 to Example 3. \end{example} \end{examples}

\ref{ex1-first} and \ref{ex2-second}

\end{document}

The output is the same as before.

egreg
  • 1,121,712
  • This is similar to the answer I linked. I am looking for a way to avoid hardcoding the number of examples, if possible. – user345345 Nov 09 '21 at 21:45
  • 1
    @user345345 And how can possibly TeX read your mind? Do you always have the same structure? The three example varieties coming together? – egreg Nov 09 '21 at 21:49
  • @user345345 I added a possible solution. – egreg Nov 09 '21 at 22:17
0

Maybe I have misunderstood the requirements but the following does not require hardcoding of the number of examples. The structure is also not fixed, i.e., you can use any amount of items per block, skip numbers or letters etc. This does require putting \refstepcounter (or possibly \setcounter) at relevant positions, either for the number or for the letter.

It works by defining two counters, one for the letters and one for the numbers, and having the number counter reset every time that the letter counter is increased with \counterwithin. This resets to 0, in order to print 1A etc. instead of 0A the counter value+1 is printed.

Then a myexample environment is defined that prints the formatted example number, sets the text for the \label command, and creates an anchor for hyperref with \phantomsection. At the end of the environment the number is increased automatically.

Whenever you want to go to the next 'block' you need to call \refstepcounter{exletter} manually, i.e., that is not linked to any theorem definitions.

\documentclass{article}
\usepackage{hyperref}
\usepackage{chngcntr}
\newcounter{exletter}
\newcounter{exnumber}
\setcounter{exletter}{1}
\setcounter{exnumber}{0}
\counterwithin{exnumber}{exletter}
\makeatletter
\newenvironment{myexample}{%
\textbf{Example \the\numexpr\value{exnumber}+1\relax\Alph{exletter}}%
\def\@currentlabel{\the\numexpr\value{exnumber}+1\relax\Alph{exletter}}%
\phantomsection%
}{\refstepcounter{exnumber}\par}
\makeatother

\begin{document} \begin{myexample} Here is the definition of the first example with some elementary properties. \end{myexample} \begin{myexample} Here is the definition of the second example with some elementary properties. \end{myexample} \begin{myexample} \label{thirdex} Here is the definition of the third example with some elementary properties. \end{myexample}

\refstepcounter{exletter} \begin{myexample} An example application of Theorem 1 to Example 1 \end{myexample} \begin{myexample} An example application of Theorem 1 to Example 2 \end{myexample} \begin{myexample} An example application of Theorem 1 to Example 3 \end{myexample}

\refstepcounter{exletter} \begin{myexample} An example application of Theorem 2 to Example 1 \end{myexample} \refstepcounter{exnumber} \begin{myexample} \label{exappl} An example application of Theorem 2 to Example 3 \end{myexample}

In Example \ref{exappl} a theorem is applied to Example \ref{thirdex}. \end{document}

Result:

enter image description here

Marijn
  • 37,699
  • This seems to work! One minor detail when I tried this out myself: Your current definition of myexample does not maintain the usual formatting for latex environments (indentation, padding, new lines, etc.), in particular, the examples are placed in line with text. Is there a way to correct this? – user345345 Nov 18 '21 at 21:28
  • One more thing: This also doesn't play nice with parentheticals, e.g. \begin{myexample}[here is a comment]. – user345345 Nov 18 '21 at 21:38
0

If your Theorem / Example setup is consistent enough, the following gives your desired output:

\documentclass{article}
\usepackage{amsthm}
\usepackage{hyperref}
\newtheorem{theorem}{Theorem}

\theoremstyle{definition} \newtheorem{example}{Example}[theorem]

% option 1 \renewcommand{\theexample}{% \addtocounter{theorem}{1}% \arabic{example}\Alph{theorem}% \addtocounter{theorem}{-1}% }

% option 2 %\renewcommand{\thetheorem}{\Alph{theorem}} %\renewcommand{\theexample}{\arabic{example}\thetheorem}

\newcommand{\exampleautorefname}{Example}

\begin{document}

\begin{example}\label{egone} Here is the definition of the first example with some elementary properties. \end{example} \begin{example}\label{egtwo} Here is the definition of the second example with some elementary properties. \end{example} \begin{example}\label{egthree} Here is the definition of the third example with some elementary properties. \end{example}

\begin{theorem}\label{thmone} This is the first theorem \end{theorem}

\begin{example} An example application of \autoref{thmone} to \autoref{egone}. \end{example} \begin{example}\label{thmoneEgtwo} An example application of \autoref{thmone} to \autoref{egtwo}. \end{example} \begin{example} An example application of \autoref{thmone} to \autoref{egthree}. \end{example}

\begin{theorem}\label{thmtwo} This is the second theorem \end{theorem}

\begin{example} An example application of \autoref{thmtwo} to \autoref{egone}. \end{example} \begin{example} An example application of \autoref{thmtwo} to \autoref{egtwo}. \end{example} \begin{example} An example application of \autoref{thmtwo} to \autoref{egthree}. \end{example}

\autoref{egone} and \autoref{thmoneEgtwo}

\end{document}

But I find it confusing that Example #C refers to Theorem 2. Using option 2, which yields the following output:

option 2 output

Teepeemm
  • 6,708