5

In a maths paper I am writing, I use what I've written below for my theorems. The trouble is, when I write a theorem in section 2, its numbering is something like 'Theorem 2.1.', but when I try to reproduce the same theorem in the Introduction it comes out as 'Theorem 1.1.'. How can I make the numbering how I want it (ie the same as it is in Section 2)?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath,amsthm}
\usepackage[runin]{abstract}
\usepackage{titlesec}
\usepackage{titlecaps}
\usepackage{fancyhdr}

\newtheorem{theorem}{Theorem}[section]

\begin{document}

\section{Introduction}

I want the theorem in Section 2 to be here and numbered correctly, eg Theorem 2.1.

\section{Section 2}
\begin{theorem}
Hi!
\end{theorem}

\end{document}

6
  • Take a look at the documentation of thmtools, §1.4, pp. 6-7. It defines a restatable theorem environment.
    – Bernard
    Mar 20, 2021 at 21:52
  • @Bernard thank you very much. Perhaps you'd like to post that as an answer to make it easier to see quickly? I will accept it if you post it. Mar 20, 2021 at 21:55
  • 2
    Thank you, but I don't think it's necessary. I'm pretty sure there are already detailed answers on this site, albeit I don't know exactly where.
    – Bernard
    Mar 20, 2021 at 21:59
  • Off-topic: Although amsmath cooperates with amsthm, it is irrelevant here. I've therefore removed the amsmath tag. Mar 21, 2021 at 15:44

4 Answers 4

4

You can create a custom theorem mythm that's manually numbered the way you want.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath,amsthm}
\usepackage[runin]{abstract}
\usepackage{titlesec}
\usepackage{titlecaps}
\usepackage{fancyhdr}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{innercustomthm}{Theorem}
\newenvironment{mythm}[1]
  {\renewcommand\theinnercustomthm{#1}\innercustomthm}
  {\endinnercustomthm}

\begin{document}

\section{Introduction}

I want the theorem in Section 2 to be here and numbered correctly, eg Theorem 2.1.

\begin{mythm}{2.1}
Hi! from Section \thesection
\end{mythm}

\section{Section 2}
\begin{theorem}
Hi! from Section \thesection
\end{theorem}

\end{document}

enter image description here

1
  • That's amazing, thank you for your help! Mar 20, 2021 at 22:03
5

With this setup, you will state the theorems you want to be repeated in the introduction, without worrying about their number. The theorem body needs to be typed in just once.

Of course, you don't need to have all theorems in the introduction. If you have one that you don't need to restate, just type it in the document body normally.

\begin{futuretheorem}{<label>}[optional argument]
<theorem body>
\end{futuretheorem}

and in the document body you will use

\pasttheorem{<label>}

The body of the theorem is saved to be also typeset at the time when the theorem number will be known.

Full code:

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

\newtheorem{theorem}{Theorem}[section]
\newtheorem*{futuretheoreminner}{Theorem~\thefuturetheoreminner}
\newcommand{\thefuturetheoreminner}{} % initialize

\ExplSyntaxOn

\prop_new:N \g_alevel_future_prop

\NewDocumentEnvironment{futuretheorem}{ m o +b }
 {% #1 is the label to be also used in the body for restating
  % #2 is the standard optional argument for a theorem
  % #3 is the body
  \renewcommand{\thefuturetheoreminner}{\ref{#1}}
  \IfNoValueTF{#2}
   {\futuretheoreminner}
   {\futuretheoreminner[#2]}
  #3
 }
 {
  \endfuturetheoreminner
  \prop_gput:Nnn \g_alevel_future_prop { #1 } { #3 }
  \IfValueT{#2}{ \prop_gput:Nnn \g_alevel_future_prop { #1-attr } { #2 } }
 }

\NewDocumentCommand{\pasttheorem}{m}
 {
  \prop_if_in:NnTF \g_alevel_future_prop { #1-attr }
   {
    \begin{theorem}[\prop_item:Nn \g_alevel_future_prop { #1-attr }]
   }
   {
    \begin{theorem}
   }
  \label{#1}
  \prop_item:Nn \g_alevel_future_prop { #1 }
  \end{theorem}
 }

\ExplSyntaxOff

\begin{document}

\section{Introduction}

\begin{futuretheorem}{test1}
This is a theorem
\end{futuretheorem}

\begin{futuretheorem}{test2}[Important]
This is an important theorem
\end{futuretheorem}

\section{Test}

\pasttheorem{test1}

\section{Another}

\pasttheorem{test2}

\end{document}

With hyperref the numbers in the introduction will be hyperlinks to the main part where the theorem is restated in context.

enter image description here

Doing the other way around, that is, typing in the theorem body only in the main part of the document, would require writing out it in an auxiliary file. But I believe that having all main theorems in a controlled place at the beginning is better.


What does the code do? First I define an auxiliary theorem-like environment naming it futuretheoreminner. It is not numbered, because we'll supply the number automatically. Its label is Theorem~\thefuturetheoreminner where the command with a long name a default definition of nothing: we'll redefine it later to contain the desired number.

Next we enter the expl3 programming layer and allocate a prop variable (property list). This kind of variable contains items that we can think to have the form key=value. How do we populate it?

The idea is that a call \begin{theorem}\label{foo} defines a cross-reference for the theorem. So we'll use this system in order to retrieve the correct number, but in an indirect way.

A new environment futuretheorem is defined that takes a mandatory argument (designed with m, in the code #1) and an optional argument (designed with o, in the code #2) and also the entire contents up to \end{futuretheorem} (available in the code as #3).

The job of this environment is to first see whether an optional argument (for the theorem note/attribution) is present. After this check the futureinnertheorem environment is started, with the optional argument if present. But first \thefuturetheoreminner command is redefined to contain \ref{#1}, which will eventually produce the desired theorem number as it results from the main document body.

The contents of the environment is delivered and the environment is finished, but there's still something to do: store in the property list the environment's content with the key #1 and possibly the optional argument with the key #1-attr.

The storing is global, because we're working inside an environment.

Now the \pasttheorem command takes a mandatory argument, which is the key for the corresponding theorem stated in the introduction. If the optional argument is present, we call

\begin{theorem}[\prop_item:Nn \g_alevel_future_prop {#1-attr}]\label{#1}

so retrieving the optional argument added at the beginning, or just

\begin{theorem}\label{#1}

which will assign the desired label for the cross reference. Next we retrieve the body of the theorem we had stored and issue \end{theorem}.

At the first run, \ref{test1} will produce just ??, but that's normal. Once the run is finished, \label{test1} will attach the correct number for the theorem in the introduction to be typeset.

9
  • 1
    Thank you for your answer! Unfortunately it's way too complicated for me to understand as I really only know the rudiments of LaTeX, but I really appreciate it. Mar 21, 2021 at 14:47
  • 2
    @A-LevelStudent I agree that the code in the preamble is not for beginners. On the other hand, the user interface is very friendly, in my opinion. Many packages do very complicated things under the hood, but as a user you are not supposed to understand how they work.
    – egreg
    Mar 21, 2021 at 16:01
  • 1
    @A-LevelStudent Think to that part as to \usepackage{something}. What is after \begin{document} is what you should look at to confirm ease of use.
    – egreg
    Mar 21, 2021 at 16:06
  • 1
    Ok, I like that suggestion. Many thanks. Mar 21, 2021 at 16:07
  • 1
    @A-LevelStudent I added an explanation of how the code works.
    – egreg
    Mar 21, 2021 at 17:08
3

The following is similar to the other answer but provides a more natural way of use - an optional argument to theorem* (the `*being common for using unnumbered document elements) that checks for an empty argument:

enter image description here

\documentclass{article}

\usepackage{amsthm}

\newtheorem{theorem}{Theorem}[section]

\newtheorem*{theorem**}{Theorem\theoremnum}
\newenvironment{theorem*}[1][]{%
  % https://tex.stackexchange.com/a/53091/5764
  \edef\theoremnum{\if\relax\detokenize{#1}\relax\else~#1\fi}% Store theorem number
  \begin{theorem**}
}{%
  \end{theorem**}
}  

\begin{document}

\section{Introduction}

I want the theorem in Section 2 to be here and numbered correctly, eg Theorem~2.1.

\begin{theorem*}[\ref{thm:second}]
This is a theorem.
\end{theorem*}

\section{Section 2}
\begin{theorem}\label{thm:second}
This is a theorem.
\end{theorem}

\end{document}

theorem** is just an inner definition to use an unnumbered theorem. Then we define theorem* to update \theoremnum if something is passed to the theorem* environment.

1

Maybe an easy solution is to use thmtools package. I'll give an example from the manual, which by the way, is easy to read because it consist of examples.

\documentclass{article}

\usepackage{amsthm}
\usepackage{thmtools, thm-restate}
\declaretheorem{theorem}

\begin{document}
\section{First section}
\begin{restatable}[Euclid]{theorem}{firsteuclid} \label{thm:euclid}%
For every prime $p$, there is a prime $p’>p$. In particular, the list of primes, \begin{equation}\label{eq:1}
    2,3,45,7,\dots
\end{equation}
  is infinite.
\end{restatable}

\section{Second section}
\firsteuclid*

\end{document}

And the output enter image description here

The manual explains the reference system with the starred and non-starred version os \firsteuclid in this case.

1

You must log in to answer this question.

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