70

This one should be easy for the gurus…

Suppose I want to define a theorem environment which has a custom numbering scheme. That is, instead of being numbered according to some counter, it is numbered according to a passed parameter. So writing something like

\begin{customtheorem}{8}
   Text.
\end{custom theorem}

would produce

Theorem 8. Text.

I realize I could define a new environment on my own, but then the header will be bold, or small caps, or whatever I defined it to be, and so may differ from the rest of the document if I switch documentclass. So I would like something that also matches the regular theorem headers.

Thanks in advance for any ideas.

Skeptic
  • 3,025
  • 5
  • 26
  • 23

4 Answers4

84

An easy way is

\documentclass{article}
%\usepackage{amsthm} %% uncomment to see the difference
\newtheorem{innercustomthm}{Theorem}
\newenvironment{customthm}[1]
  {\renewcommand\theinnercustomthm{#1}\innercustomthm}
  {\endinnercustomthm}

\begin{document}

\begin{customthm}{8}\label{eight} Every theorem must be numbered by hand. \end{customthm}

Here is a reference to theorem~\ref{eight}. \end{document}

You can also use the optional argument for attribution:

\begin{customthm}{99}[Somebody]\label{ninetynine}
Statement.
\end{customthm}

A more generic interface for defining several of these environments; this doesn't respect theorem styles; it could be adapted, though.

\documentclass{article}
\usepackage{amsthm}

\newtheorem{innercustomgeneric}{\customgenericname} \providecommand{\customgenericname}{} \newcommand{\newcustomtheorem}[2]{% \newenvironment{#1}[1] {% \renewcommand\customgenericname{#2}% \renewcommand\theinnercustomgeneric{##1}% \innercustomgeneric } {\endinnercustomgeneric} }

\newcustomtheorem{customthm}{Theorem} \newcustomtheorem{customlemma}{Lemma}

\begin{document}

\begin{customthm}{8}\label{eight} Every theorem must be numbered by hand. \end{customthm}

Here is a reference to theorem~\ref{eight} and one to the important lemma~\ref{life-universe-everything}

\begin{customlemma}{42}\label{life-universe-everything} This lemma explains everything. \end{customlemma}

\end{document}

enter image description here

ADDITION

Support for cleveref was asked in comments.

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

\newtheorem{innercustomgeneric}{\customgenericname} \providecommand{\customgenericname}{} \newcommand{\newcustomtheorem}[2]{% \newenvironment{#1}[1] {% \ifdefined\crefalias\crefalias{innercustomgeneric}{#2}\fi \renewcommand\customgenericname{#2}% \renewcommand\theinnercustomgeneric{##1}% \innercustomgeneric } {\endinnercustomgeneric}% \ifdefined\crefname\crefname{#2}{#2}{#2s}\fi }

\newcustomtheorem{customthm}{Theorem} \newcustomtheorem{customlemma}{Lemma}

\begin{document}

\begin{customthm}{8}\label{eight} Every theorem must be numbered by hand. \end{customthm}

Here is a reference to \cref{eight} and one to the important \cref{life-universe-everything}

\begin{customlemma}{42}\label{life-universe-everything} This lemma explains everything. \end{customlemma}

\end{document}

The \ifdefined bits allow for the solution to be independent on cleveref.

enter image description here

egreg
  • 1,121,712
  • 1
    I guess that's simpler than mine (and allows non numeric values too) – David Carlisle May 01 '12 at 16:48
  • @DavidCarlisle Yes, it uses the fact that innercustomthm is stepped, so the reference is established, but \theinnercustomthm is used, which can contain anything (well, almost). – egreg May 01 '12 at 16:50
  • This is splendid. Thanks so much.

    @DavidCarlisle: I went with egreg's, because it's simpler, but thanks also!

    – Skeptic May 01 '12 at 17:06
  • I am interested in an extension of this behavior. Let's say I want to create a theorem with the same label as a previous theorem. I would like to display this as \begin{customthm}{\ref{oldtheorem}}\end{customthm}, but this gives me errors. How would I modify this code to achieve the desired result? – Jonathan Gleason Aug 10 '12 at 15:20
  • The optional argument form did not work for me (I'm using amsthm). I fixed this with \newenvironment{customthm}[2][]{\renewcommand\theinnercustomthm{#2}\innercustomthm[#1]}{\endinnercustomthm}. – equaeghe Mar 07 '14 at 15:42
  • @egreg may I ask what is the merits of "somebody"? – athos Jul 03 '14 at 06:56
  • 1
    @egreg and is it possible to extend this to definition, lemma? – athos Jul 03 '14 at 06:59
  • 1
    @athos \newtheorem{innercustomlemma}{Lemma} and \newenvironment{customlemma}[1]{\renewcommand\theinnercustomlemma{#1}\innercustomlemma}{\endinnercustomlemma} – egreg Jul 03 '14 at 08:00
  • @egreg the error says "undefined control sequence".... – athos Jul 03 '14 at 11:28
  • What undefined control sequence? – egreg Jul 03 '14 at 11:51
  • @egreg I added "\newtheorem{innercustomlemma}{Lemma} \newenvironment{customlemma}[1] {\renewcommand\theinnercustomlemma{#1}\innercust‌​omlemma} {\endinnercustomlemma}" at the beginning then "\begin{customlemma}{abc}[abcd]\label{abcde} abcdef \end{customthm}" at the body. The error says: "! Undefined control sequence. \customlemma ...heinnercustomlemma {#1}\innercust XXXXX ‌​omlemma l.126 \begin{customlemma}{abc} XXXXX [abcd]\label{abcde} ? " here XXXXX is the line break. – athos Jul 04 '14 at 01:35
  • @egreg one more question -- is it possible to "tune off" the italic font used for theorems? – athos Jul 04 '14 at 01:41
  • @athos Can you please open a new question linking to this one? – egreg Jul 04 '14 at 08:31
  • @egreg i've posted it in http://tex.stackexchange.com/questions/187971/custom-theorem-numbering-and-extend-to-lemma-etc – athos Jul 04 '14 at 09:36
  • @egreg I am trying to set counter for both of my lemmas and theorems. So I copy and paste your code "\newtheorem{innercustomthm}{Theorem} \newenvironment{customthm}[1] {\renewcommand\theinnercustomthm{#1}\innercustomthm} {\endinnercustomthm}" again, and change the "Theorem" to "Lemma". However, I found that all of my lemmas become theorems. I couldn't figure out what's wrong with that.... – KevinKim Dec 30 '16 at 04:02
  • @KevinKim You have to define two environments for each type, or use two arguments. I'll add it later. – egreg Dec 30 '16 at 10:40
  • @egreg: Thanks, this answer is great! I just have a follow-up question: Is it possible to make the number of the theorem differ from the number that is printed in references to it? For example, if I do \begin{customthm}{8}{-5}\label{abc} blabla \end{customthm}, I would like the theorem number to be 8, but if I do \ref{abc}, I would like -5 to be printed. – PhoemueX Jan 17 '18 at 10:22
  • 2
    @PhoemueX I'm not sure why you'd like to do that. You can do \makeatletter\newcommand\manuallabel[1]{\def\@currentlabel{#1}}\makeatletter and use \begin{customthm}{8}\manuallabel{-5}\label{abc} – egreg Jan 17 '18 at 11:41
  • @egreg: Thank you very much, that worked perfectly! If you really want to know why I want to do this, read on: For a certain reason, I want to have a Theorem with the number 16.9\frac{1}{2}. But since (at least in amsart) the theorem number is set in bold, I use \frac{\boldsymbol{1}}{\boldsymbol{2}}. However, in references to the theorem, I would like the usual \frac{1}{2} instead of the bold fraction :) – PhoemueX Jan 18 '18 at 10:44
  • @PhoemueX Naughty boy! – egreg Jan 18 '18 at 10:46
  • Hi @egreg I am trying to adapt this code to acknowledge theorem styles, but I'm not able to make sufficient sense of the underlying logic do pull this off. Do you know of where I can go to understand how the \the[innercustomgeneric] and \end[innercustomgeneric] are working here? I found this https://tex.stackexchange.com/questions/38674/the-the-command but I'm having trouble figuring out how that articulates with what you are doing here. – benblumsmith Jun 20 '20 at 21:54
  • @benblumsmith You need to define an “inner” environment for each style you want to use. The command \the has nothing to do with this. – egreg Jun 21 '20 at 08:12
  • @egreg I was making an attempt to parse the logic of your code in order to be able to see what I was doing when adapting it. I think the \the is my primary obstruction to parsing the logic, which is why I asked about it. I have a temporary workaround so I guess I'll just let this go for now. – benblumsmith Jun 22 '20 at 15:14
  • This works fine to restate theorems. I know there are packages specifically for that. But, I prefer this for practical reasons (eg: less chance of package conflicts). – Cyriac Antony Jan 13 '21 at 10:16
  • Does anyone know how to make this play nice with the restatable environment from thmtools package? That is, I want to make a custom-numbered theorem restatable. I can't figure out how to get the restatable environment to understand why I'm giving it an extra argument (namely, the number that I want to assign to the theorem) – Joseph Granata Feb 04 '21 at 22:20
  • A disadvantage of this solution is that is doesn’t appear to work with cleveref. – Hermetically Sealed Halibut Feb 02 '24 at 14:33
  • 1
    @HermeticallySealedHalibut Added support for cleveref – egreg Feb 02 '24 at 15:31
14

You can just temporarily reset your theorem counter:

\documentclass{article}
\usepackage{amsthm}


\newtheorem{Theorem}{Theorem}



\makeatletter

\newenvironment{customTheorem}[1]
  {\count@\c@Theorem
   \global\c@Theorem#1 %
    \global\advance\c@Theorem\m@ne
   \Theorem}
  {\endTheorem
   \global\c@Theorem\count@}

\makeatother


\begin{document}


\begin{Theorem}
Dummy text
\end{Theorem}

\begin{customTheorem}{99}
Dummy text
\end{customTheorem}

\begin{Theorem}
Dummy text
\end{Theorem}


\end{document}
David Carlisle
  • 757,742
3

An easy way if it's just for a few theorems is

\newtheorem*{theorem8}{Theorem 8}
  • This is not what OP asked for, since it's not using a parameter to the environment, but it solved a problem of mine. – snaut May 02 '22 at 16:12
0

Another easy way suitable for a few theorems, but technically not addressing the exact question, as it doesn't define a new environment.

Immediately before declaring the theorem, reset the counter to the number preceding the one you want.

% For Theorem 9, with the default counter named "theorem"
\setcounter{theorem}{8}

% Now declare the theorem \begin{Theorem} Dummy text \end{Theorem}