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}

4 Answers4

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.

egreg
  • 1,121,712
  • 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. – A-Level Student Mar 21 '21 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 '21 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 '21 at 16:06
  • 1
    Ok, I like that suggestion. Many thanks. – A-Level Student Mar 21 '21 at 16:07
  • 1
    @A-LevelStudent I added an explanation of how the code works. – egreg Mar 21 '21 at 17:08
  • @egreg Is it possible to number the theorems according to the first occurence, i.e., when stating futuretheorem, instead of waiting for the pasttheorem environment and copying its numbering? Of course, I'd like pasttheorem to copy futuretheorem's numbering. – J. Schmidt May 06 '22 at 09:50
  • @J.Schmidt I'm not sure what you mean, but if you want that, you can use restatable from thmtools – egreg May 06 '22 at 12:26
  • @egreg In your example, the numbering of the theorems is 2.1 and 3.1, I'd like for them to be 1.1 and 1.2, and when calling them later on, for them to stay 1.1 and 1.2. (I made a separate post about it https://tex.stackexchange.com/questions/643221/how-to-fix-the-order-of-my-theorems) – J. Schmidt May 06 '22 at 12:57
  • I had some trouble getting this to work, and eventually found the following strange interaction: This snippet does not work if the package "thmtools" is loaded before the snippet is copy pasted into the preamble! (It seems to work fine if the package is loaded afterwards). – s.harp May 31 '23 at 13:33
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

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
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.

Werner
  • 603,163
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.

Luis Turcio
  • 2,757