To answer your question of whether ntheorem
is clearly superior to amsthm
(or the other way round), the answer is unfortunately no as both have advantages and shortcomings.
Feature-wise, ntheorem
is clearly ahead of amsthm
, but amsthm
is more robustly designed, and has, as such, less bugs (of course, depending on how you use the ntheorem
package, you may never encounter these bugs, but it's better to be aware of them).
Comparison of features of ntheorem and amsthm
The following table is inspired from the one given in the French FAQ entry about theorem and shows a list of features of each packages, clearly showing that ntheorem
can do more things than amsthm
.
Examples of ntheorem bugs
Here are a few chosen bugs which can occur in ntheorem and of which you should be aware before deciding whether to use ntheorem
or amsthm
. All these bugs are specific to ntheorem
and do not occur in amsthm
.
Break style clash with high material
When using the break style, if you put something a little too high at the start of the theorem, it will overlap with the theorem title:
\documentclass{article}
\usepackage{ntheorem}
\theoremstyle{break}
\newtheorem{theorem}{Theorem}
\begin{document}
\begin{theorem}
$\displaystyle \sum_{n=1}^{+\infty}{\frac{1}{n^2}} = \frac{\pi^2}{6}$
\end{theorem}
\end{document}
Footnote in theorem note
While it may not be a very good pratice to put footnotes in theorem optional arguments, it can be needed some times, but doesn't work with ntheorem
(the footnote text will be lost; you must use the \footnotemark/\footnotetext trick):
\begin{theorem}[Fermat's little theorem\footnote{First stated in a letter dated October 18, 1640.}]
...
\end{theorem}
Long theorem note
If the theorem's optional argument is too long (either because the document is in two column mode or because the note is indeed very long), it will hang out of the margin. Here's an example in two column mode:
\documentclass[twocolumn]{article}
\usepackage{lipsum}
\usepackage{ntheorem}
\newtheorem{theorem}{Theorem}
\begin{document}
\lipsum[1]
\begin{theorem}[A very very very very long optional argument]
\lipsum[2]
\end{theorem}
\lipsum[3-5]
\end{document}
Examples of amsthm bugs
Although it has less bugs, amsthm
is not completely bug-free. Here's a variant of the "too long theorem optional argument" ntheorem
bug, but which only occurs with amsthm
if a list is immediately following the theorem head:
\documentclass[twocolumn]{article}
\usepackage{lipsum}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}
\begin{document}
\lipsum[1]
\begin{theorem}[A very very very very long optional argument]
\begin{enumerate}
\item Bla bla bla.
\item Bla bla bla.
\item Bla bla bla.
\end{enumerate}
\end{theorem}
\lipsum[3-6]
\end{document}
I see at least two solutions:
1) Instead of using algorithmic
, use the algorithmicx
package, which gives you more functionality and flexibility:
\documentclass{article}
\usepackage{algorithmicx,algpseudocode}
\usepackage{program}
\begin{document}
\begin{algorithmic}
\State a
\State b
\end{algorithmic}
\end{document}
2) Use the savesym
package, and the technique I explained in Two conflicting packages in one document — doable? to prevent name clashes:
\documentclass{article}
\usepackage{savesym}
\usepackage{program}
\savesymbol{AND}
\savesymbol{OR}
\savesymbol{NOT}
\savesymbol{TO}
\savesymbol{COMMENT}
\savesymbol{BODY}
\savesymbol{IF}
\savesymbol{ELSE}
\savesymbol{ELSIF}
\savesymbol{FOR}
\savesymbol{WHILE}
\usepackage{algorithmic}
\begin{document}
\begin{algorithmic}
\STATE a
\STATE b
\end{algorithmic}
\end{document}
Best Answer
Preliminaries
In this answer, I'll be discussing how to go about creating cross-references to "objects" such as "Theorem 2", "Lemma 3", "Page 7", and "Section 4". Each object is assumed to be associated with a
\label
command; for instance, some important numbered theorem may be associated with\label{thm:important}
and a numbered\section
directive may be associated with\label{sec:results}
.Classes of cross-references: Static, semi-dynamic, and fully dynamic
Static or hard-coded cross-references. As the name "static" implies, these cross-references are hard-coded strings of the form
Theorem~2
andSection~4
. Other examples arein the next paragraph
,in the final section
, andthe preceding lemma
.\label
commands. Extremely flexible.\hyperlink
andhypertarget
commands of the hyperref package. However, it's much much easier to simply use\label
and various\ref
commands along with thehyperref
package.)Semi-dynamic cross-references. These include the TeX/LaTeX
\ref
and\pageref
commands. I call this method semi-dynamic rather than fully dynamic because it automates the numbering of the objects in question but hard-codes their types (Theorem
,page
,section
, ...).hyperref
package, the cross-references' numbers can be made into hyperlink targets as well.\ref{}
commands quickly becomes very tedious as well as error-prone.Fully dynamic cross-references. Both the object's type and its number are generated by LaTeX.
Which packages provide which kinds of cross-referencing facilities?
amsmath. Provides the
\eqref
command to cross-reference numbered equations.There are two important advantages to typing
\eqref{eq:einstein}
instead of(\ref{eq:einstein})
:\eqref
(including the parentheses) is always set in an upright font shape. This feature is useful because cross-references to equations may occur in some text block that's set in italics (such as in the body of a theorem statement). By (near-universal?) convention, equation numbers and their parentheses should generally be typeset in an upright font shape regardless of the font shape of the surrounding text. It's quite handy not having to keep track of whether a cross-reference to an equation occurs in an italic text block or a "regular" (i.e., upright font shape) text block.prettyref. Provides the command
\prettyref
.An important step towards fully dynamic cross-references. It works by relying on information contained in the object's
\label
to infer the object's type. However, this approach has some serious limitations. Quoting from its manual:In short, if you need to change the object's type from "theorem" to "proposition", you'll also need to change its label. This negates some of the advantages of using this package (in my view). A second problem can arise if the
babel
package is loaded with the optionfrench
, as this makes the:
character "active" (in the TeX-specific sense of the word). For a work-around in case you need to load bothprettyref
andbabel-french
, see the query prettyref bug when used with babel-french and especially Enrico Gregorio's answer.varioref. Provides the commands
\vref
andvpageref
. Its stated aim is to provide "intelligent page references." From the introduction of the package's user guide:Very useful if you need to provide page location information automatically with each cross-reference.
smartref. This package partially extends the functionality of
varioref
, enabling not just the page of a\label
to be recorded, but the value of arbitrary other counters (such as the presentchapter
). This can be immediately used to add "in chaper N" to cross-references or, with some additional logic (example), to produce avarioref
-style "in the next/previous chapter" call-out.fancyref. Another step towards fully dynamic cross-referencing. Provides the commands
\fref
(and, for the beginning of a sentence,\Fref
} for various pre-defined object types. It requires the use of specific label prefixes (e.g.,chap
,sec
,eq
,fig
, etc.) to inform it about the object's type. Quite a few language-specific adaptations are available if it's used together withbabel
. Fully compatible with thevarioref
package.If you wish to add an object type -- say, "Lemma" -- to the list of object types that
fancyref
knows about, you may want to take a look at Enrico Gregorio's brilliant answer to the question "How to define a macro which declares a fancyref prefix? How to prevent it from outputting text?". Enrico provides a macro called\mkfancyprefix
which lets users execute instructions such as\mkfancyprefix{lem}{Lemma}
. This, in turns, lets users cross-reference lemmas (or is that lemmata?!) with an\fref{lem:...}
instruction.theoremref. As its name suggests, this package provides (fully dynamic) cross-references to theorems and related objects (lemmas, corollaries, etc.) Requires you to use the command
\thlabel
instead of\label
to work. Functionality appears to be limited to theorem-like objects.ntheorem. When loaded with the
thref
option, this package provides a very elegant method for creating fully-dynamic cross-references to theorem-like objects. It even works if some of the objects share the same counter variable -- as is often the case with theorems, lemmas, corollaries, and propositions. Main user command:\thref
.hyperref. In addition to providing its well-known capabilities for turning various cross-references -- both within a document and to objects outside the document -- into hyperlink targets, this package also provides the command
\autoref
. This command provides fully dynamic cross-references to a wide range of single instances of objects such as equations, sections, figures, tables, etc. A special aspect of this command is that the object's "name", e.g., "equation", is made a part of the hyperlink target, providing a large visual "target" for the mouse to click on and "jump" to the cross-referenced object.The "names" of the objects being cross-referenced with the
\autoref
command are, by default, in English. However, if thebabel
package is loaded with a suitable language option,hyperref
will provide the objects' names in that language as well. As of mid-February 2020, available language options are (listed alphabetically)afrikaans
,catalan
,dutch
,english
(the default; synonyms:UKenglish
,british
,USenglish
, andamerican
),french
(synonyms:frenchb
,francais
,acadian
, andcanadien
),german
(synonyms:ngerman
,austrian
, andnaustrian
),greek
,italian
,magyar
(synonym:hungarian
),portuges
(synonyms:portuguese
,brazil
, andbrazilan
),russian
,spanish
, andvietnamese
.In order for the
hyperref
package to work correctly, it is generally necessary to load it after all other packages that provide cross-referencing functions. The exception to this rule is thecleveref
package -- see below.The memoir document class: Provides commands
\tref
,\fref
,\pref
, etc to create dynamic cross-references to tables, figures, pages, etc. (Note that thememoir
command\Cref
, which is used to create cross-references to chapters, is incompatible with that of thecleveref
package.) Use of these cross-referencing commands is great for the intelligibility of the LaTeX code. The obvious cost is a proliferation of cross-referencing commands. Wouldn't it be nice to have to remember only a very limited number of cross-referencing commands?!cleveref. The current "king" of all cross-referencing packages. Main user commands:
\cref
and\crefrange
(and the uppercase variants\Cref
and\Crefrange
for use at the start of a sentence).\cref
command can take not just one argument but many arguments. These arguments needn't be sorted by the author -- the package will do that for him/her! -- and they needn't even be of the same type, i.e., you can issue the command\cref{thm:this,lemma:that,prop:here,cor:there}
andcleveref
will automatically create a lexically correct group of cross-references for you.If you have theorems, lemmas, corollaries, propositions, and other environments that share a common counter variable, the
cleveref
manual suggests that you load either thentheorem
or theamsthm
package prior to defining these theorem-like environments (and also prior to loadingcleveref
, obviously). Thentheorem
andamsthm
packages provide some very handy auxiliary, behind-the-scenes information that helpscleveref
figure out which name (theorem, lemma, corollary, etc) to associate with the object being cross-referenced, even if the environments share a common counter.Extensive language adaptations. The document's language, if different from English, has to be specified as an option when the
cleveref
package is loaded. (cleveref
is compatible withbabel
as well.) As of late-2016, the package can adapt the names of cross-referenced objects to the following 14 [!] languages:brazilian
,catalan
,danish
,dutch
,english
(default),esperanto
,french
,german
,italian
,norwegian
,russian
,spanish
,swedish
, andukrainian
. Toby Cubitt (the author and maintainer of thecleveref
package) actively welcomes submissions of object names for additional languages.An interesting tidbit of information regarding the rendering of the names of these objects: In English, there is no declension of nouns (including those used in cross-references), i.e., their form is the same whether the noun is used in the nominative, genitive, dative, accusative, etc form. This simplicity, however, does not apply to many other languages. :-( The
cleveref
package's singular and plural noun forms are all in the nominative form; if you use the package with a language other than English (or, I suppose, French, Spanish, and Italian), you should use the command\creflabel
instead of\cref
, and provide the appropriate case of the noun yourself, if the noun isn't being used in its nominative form. Even though you have to supply the name of the noun yourself when you use the\creflabel
command, you still get to benefit from the sorting and compression facilities of thecleveref
package.Virtually all aspects of the cross-references can be customized fully.
The manual of the
cleveref
package recommends loading thentheorem
package with thethref
option if you're going to create cross-references to theorem-like objects (especially if the objects share the same counter).The
cleveref
package is fully compatible with other packages that provide cross-referencing commands -- includingvarioref
,ntheorem
, andhyperref
-- as long ascleveref
is loaded last.Some caveats when using the
cleveref
package:showonlyrefs
option of themathtools
package. By "seriously incompatible" I mean that ifcleveref
is loaded together with\usepackage[showonlyrefs]{mathtools}
, not only will the\cref
commands containing labels of equation objects be messed up, but many other cross-referencing packages will also experience major problems. Fortunately, this problem appears to be the only incompatibility between thecleveref
andmathtools
packages.footmisc
and thehyperref
packages are loaded, there are some unresolved incompatibilities that affect cross-references to footnotes. This problem also affects a variety of other packages that provide cross-referencing functions, includingcleveref
.babel
package with the optionfrench
, be sure not to use the:
("colon") character in the arguments of any\label
and\cref
-family instructions. This is because (a) thebabel-french
combination makes the:
character "active" and (b)\cref
needs to perform various operations on its argument; these operations will crash if the:
character is active.Addendum: Further cross-referencing possibilities
A major limitation of the cross-referencing mechanisms currently provided by the LaTeX kernel is that the
\label
command only writes two items to the.aux
file: the value of the counter that was last incremented by a\refstepcounter
or similar directive, and the number of the page on which this\label
instruction is recorded. This limitation makes it impossible, for instance, to easily write something like "As was shown by Theorem~xx in Section~yy", as LaTeX doesn't provide a method for associating information about the theorem's number with the number of the section in which the theorem appears. Thehyperref
andcleveref
packages apply some nifty hacks to the LaTeX kernel to overcome this limitation at least partially. However, these hacks aren't all that robust; this lack of robustness explains in part why these two packages should generally be loaded last.The zref bundle of packages created by Heiko Oberdiek aims to overcome this limitation, by providing both an interface for macro programmers and several packages that make use of this interface. The package doesn't modify the basic
\label
,\ref
, and\pageref
commands. Instead, it provides the commands\zlabel
and\zref
, where\zlabel
can be programmed to generate several rather than just two pieces of information to be written to the.aux
file, and where options can be passed to\zref
to specify which piece(s) of information should be cross-referenced.To the best of my knowledge, so far none of the major cross-referencing packages (including
hyperref
) make use of the power afforded by thezref
package. However, there are several smaller packages or "modules", also written by Heiko Oberdiek, that demonstrate some of the potential uses of thezref
approach. For instance, the moduleabspage
makes possible referencing the absolute page number of a document. This is useful for documents (including many books!) that have both roman-style and arabic-style page numbers: The macro\zref[abspage]{LastPage}
references the total number of pages in the document, irrespective of the numbering styles that may be in use.