Theorems & proofs (amsthm)

The hollow square that closes a proof is not a character in any font. LaTeX’s amsthm draws it on the spot out of two vertical rules and two horizontal ones (\openbox in amsthm.sty; measured at 7.77786pt wide by 6.75003pt tall, which is 0.77778em × 0.675em in a 10pt document). Theorem environments work the same way. LaTeX does not hand you a finished “theorem”; it hands you \newtheorem, a declaration mechanism, and leaves it to you to decide what counts as a theorem and how it gets numbered. This page follows what \newtheorem actually generates, why its two optional arguments cannot be combined, what \theoremstyle really changes, and the \qedhere problem that lands the QED symbol on the wrong line — with the results of real compilations at each step.

What \newtheorem generates: an environment and a counter

\newtheorem{theorem}{Theorem} creates one environment and one counter to number it with. The log shows a line such as \c@theorem=\count196, which is the counter actually being allocated. The first argument is the environment name you write in \begin{…}; the second is the word printed in bold in the heading. The point is that these are two different things: you can keep the environment name as plain theorem and change only the printed word to Satz, Théorème, or anything else.

latex
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}   % declares the environment AND allocates a counter

After that declaration, every use of the theorem environment gets a bold heading with a running number — Theorem 1, Theorem 2 — and its text is set in italic under the default style. The number lives in a LaTeX counter, so inserting a theorem in the middle renumbers everything after it automatically. That is the property people are really after when they search for theorem numbering: as long as you never type a number by hand, the numbers cannot go out of step.

latex
\begin{theorem}
  There are infinitely many primes.
\end{theorem}

\begin{theorem}[Pythagoras]
  In a right triangle, $a^2 + b^2 = c^2$.
\end{theorem}

As in the second example, a name passed in brackets right after \begin{theorem} is appended in parentheses after the number: “Theorem 2 (Pythagoras).” This is independent of whether the environment is numbered, and works just as well with the unnumbered environments made by \newtheorem* below. Note also that \newtheorem itself is part of standard LaTeX2e. Plain \newtheorem can make numbered environments on its own, but \theoremstyle, \newtheorem*, the proof environment, and \qedhere — everything that follows — are amsthm extensions that appear only once you write \usepackage{amsthm}.

The two brackets: sharing and subordinating cannot be combined

\newtheorem takes exactly one bracket, and its position decides what it means. Placed right after the environment name, it says “share an existing counter”; placed after the second argument, it says “subordinate the counter to this parent and reset with it”. Numbering theorems and lemmas separately scatters the same numbers about — “Theorem 1, Lemma 1, Theorem 2, Lemma 2…” — so most mathematical writing takes the first option and runs a single sequence: “Theorem 1, Lemma 2, Theorem 3…”.

latex
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}[theorem]{Lemma}        % bracket BEFORE: share theorem's counter
\newtheorem{definition}{Definition}[section]  % bracket AFTER: reset per section, "2.1"

The trailing bracket can also take an existing theorem environment instead of section. With \newtheorem{corollary}{Corollary}[theorem], the corollary number resets every time a theorem appears, so “Corollary 3.1” reads as “the first corollary attached to Theorem 3”.

DeclarationEffect
\newtheorem{theorem}{Theorem}Numbers 1, 2, 3, … on a counter of its own
\newtheorem{lemma}[theorem]{Lemma}Shares theorem’s counter, so lemmas and theorems run in one sequence
\newtheorem{theorem}{Theorem}[section]Restarts at every \section and prints with the section number, as “2.1”
\newtheorem{corollary}{Corollary}[theorem]Resets at every theorem and inherits its number, printing as “3.1”
\newtheorem*{remark}{Remark}Allocates no counter and prints the heading word only, unnumbered (needs amsthm)

So what happens if you write both at once? Feeding \newtheorem{lemma}[theorem]{Lemma}[section] to TeX Live 2024 does produce an error — but it says nothing about \newtheorem. Since \newtheorem never reads a second bracket, the [section] is left behind in the preamble as ordinary text, and LaTeX concludes that the document body has begun. What you get is this.

log
! LaTeX Error: Missing \begin{document}.

l.4 \newtheorem{lemma}[theorem]{Lemma}[
                                       section]

So sharing and subordinating are mutually exclusive. When you want both — “lemmas share the theorem numbering, and that numbering restarts each section” — put the [section] on the parent only. Declare \newtheorem{theorem}{Theorem}[section], and a lemma sharing it via \newtheorem{lemma}[theorem]{Lemma} inherits the section numbering automatically. Since there is only one counter, that satisfies both requirements without contradiction.

When you hit Command \theorem already defined.

This error means you declared the same environment name twice. \newtheorem creates a new environment, so it refuses to overwrite a name that already exists. Here is what two \newtheorem{theorem}{Theorem} lines in one preamble produce.

log
! LaTeX Error: Command \theorem already defined.
               Or name \end... illegal, see p.192 of the manual.

In practice you rarely hit this by typing the line twice. Usually the document class or a loaded package has already declared theorem for you. Conference templates, amsart, and elsarticle are among those that may set up theorem environments in advance. There are three ways out: delete your line, rename yours to something like mytheorem, or keep the existing declaration and adjust only \theoremstyle. If all you want is a different heading word for an environment that already exists, look for the class’s own hook rather than reaching for \newtheorem.

What \theoremstyle changes: the head font and the body font

\theoremstyle in amsthm switches the combination of fonts used for the heading (head) and the text (body). Three styles are built in: plain, definition, and remark. The decisive detail is that \theoremstyle{…} affects only the \newtheorem lines written after it. Setting a style below a declaration does not apply retroactively, so group your declarations by style in the preamble. Write nothing and you get plain.

  • plain: bold heading, italic body. For assertions you state emphatically — theorems, lemmas, propositions, corollaries. The default when nothing is set.
  • definition: bold heading, upright (roman) body. For things meant to read as ordinary prose — definitions, examples, problems, conditions. Upright is easier to read when the text carries formulas or runs long.
  • remark: italic heading, upright body. For asides — remarks, notes, claims. Because even the heading lightens, it does not interrupt the flow of the surrounding text.
preamble
\usepackage{amsmath, amsthm}

\theoremstyle{plain}            % italic body
\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{corollary}[theorem]{Corollary}

\theoremstyle{definition}       % upright body
\newtheorem{definition}[theorem]{Definition}
\newtheorem{example}[theorem]{Example}

\theoremstyle{remark}           % italic head
\newtheorem*{remark}{Remark}

With this setup, theorem, lemma, corollary, definition, and example all share a single per-section sequence (Theorem 2.1, Definition 2.2, Lemma 2.3…), the first three with italic bodies and the last two upright. Only remark has no number, and its “Remark” heading is italic. Sharing the numbers while splitting the fonts works because the counter and the style are two independent axes.

The proof environment, and the QED symbol landing on the wrong line

The proof environment of amsthm opens with an italic “Proof.” and automatically closes with \qedsymbol, which defaults to □. But proof tries to set the symbol at the end of the last paragraph line, so when a proof ends in a displayed equation there is no line to attach it to, and the □ drops onto a line of its own. This is not an impression; it is measurable. Putting the same proof in a \vbox and measuring the height gives:

log
% \vbox{\hsize=8cm \begin{proof}Compute:\[a^2+b^2=c^2.\]\end{proof}}
without \qedhere   h = 66.94444pt
with    \qedhere   h = 47.77777pt      % 19.16667pt shorter: one whole line saved

The difference is 19.16667pt — exactly one line. \qedhere is an instruction saying “put the symbol here”: written at the end of a display, it tucks the □ against the right edge of that equation’s line and the extra line disappears. \qedhere works inside equation, align, and gather*, and at the last item of an enumerate (all confirmed to compile without error on TeX Live 2024). Where it cannot place the symbol cleanly, amsthm reports Package amsthm Warning: The \qedhere command may not work correctly here, so the log will tell you.

latex
\begin{proof}
  Rearranging both sides gives
  \[
    a^2 + b^2 = c^2. \qedhere
  \]
\end{proof}

Both the heading word and the end symbol can be replaced. An individual proof takes a bracket, as in \begin{proof}[Proof of Theorem 1], and the document-wide default comes from \proofname, which you redefine (\renewcommand{\proofname}{Demonstration} was verified to produce a “Demonstration.” heading). The end symbol is a redefinition of \qedsymbol. As noted at the top, the default \qedsymbol is \openbox, the hollow square drawn from rules — so swapping in $\blacksquare$ gives a filled square, and redefining it as empty removes the marker entirely. Note that \blacksquare is a symbol from amssymb (line 48 of amssymb.sty), so with amsthm alone you get ! Undefined control sequence. — do not forget \usepackage{amssymb}.

preamble
\usepackage{amssymb}                         % \blacksquare lives here, not in amsthm
\renewcommand{\proofname}{Demonstration}     % heading word for every proof
\renewcommand{\qedsymbol}{$\blacksquare$}    % filled square instead of the hollow one
% \renewcommand{\qedsymbol}{}                % no end-of-proof marker at all

thmtools: replacing bracket positions with key=value

By the time you have a dozen declarations, \newtheorem’s convention — meaning determined by bracket position — has become hard to read. thmtools (2023/05/04 v0.76 in TeX Live 2024) is a higher-level interface on top of amsthm (or ntheorem) that lets you write the same settings as key=value through \declaretheorem. It owns no numbering machinery of its own and simply calls amsthm’s \newtheorem underneath, so you load it alongside amsthm.

preamble
\usepackage{amsmath, amsthm, thmtools, thm-restate}

\declaretheorem[numberwithin=section]{theorem}      % same as [section] after arg 2
\declaretheorem[sibling=theorem]{lemma}             % same as [theorem] after arg 1
\declaretheorem[style=definition, sibling=theorem]{definition}
\declaretheorem[numbered=no, name=Remark]{remark}

The keys map straightforwardly. Subordination to a section is numberwithin= (synonyms parent=, within=), sharing a counter is sibling= (synonyms numberlike=, sharecounter=), unnumbered is numbered=no, the heading word is name= (synonyms title=, heading=), and the look is style=. Typeset the preamble above and Theorem 1.1 is followed by Lemma 1.2 — the output confirms that sibling= really does build one shared sequence.

The real reason to add thmtools is two features that are awkward with amsthm alone. The first is restating (restatable). Load the bundled thm-restate, write a theorem inside a restatable environment, and its content is stored as a macro that can be set again later with the same number. That is the standard way to state a result with its number in the main text while sending the proof to an appendix. Compiled for real, the Theorem 1.1 of the body reappeared in the appendix as Theorem 1.1.

latex
\begin{restatable}[Euclid]{theorem}{firsteuclid}
  \label{thm:euclid}
  There are infinitely many primes.
\end{restatable}

% later, e.g. in an appendix — same number, references still point at the original
\firsteuclid*

restatable takes an optional name, then the theorem environment to use, then the macro name that remembers the content. Expanding \firsteuclid later sets the theorem again with its number pinned to the original occurrence. The starred \firsteuclid* restates it while keeping \label/\ref pointed at the original declaration rather than the copy. The second feature is the list of theorems: \listoftheorems generates a contents-like list, and ignoreall together with show={…} narrows it to chosen environments. There is also onlynamed, which picks up only theorems given a name via \begin{…}[name], and with hyperref loaded each entry becomes a link to the theorem itself.

latex
\listoftheorems                                     % everything
\listoftheorems[ignoreall, show={theorem, lemma}]   % only these two
\listoftheorems[ignoreall, show={theorem}, onlynamed]  % only the ones you named

To build your own look, define a style with \declaretheoremstyle and call it from \declaretheorem[style=…]{…}. Keys let you set the heading font headfont, the body font bodyfont, the space above and below (spaceabove, spacebelow), the braces around the name (notebraces), the gap after the head (postheadspace), the end symbol (qed), and more. As a judgement call: if a handful of declarations is all you need, amsthm alone is enough; add thmtools once counting \newtheorem’s brackets starts to grate, or once you want restatable theorems and a list of theorems.