74

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.

4 Answers 4

88

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

27
  • 1
    I guess that's simpler than mine (and allows non numeric values too) Commented May 1, 2012 at 16:48
  • 1
    @egreg and is it possible to extend this to definition, lemma?
    – athos
    Commented Jul 3, 2014 at 6:59
  • 1
    @athos \newtheorem{innercustomlemma}{Lemma} and \newenvironment{customlemma}[1]{\renewcommand\theinnercustomlemma{#1}\innercustomlemma}{\endinnercustomlemma}
    – egreg
    Commented Jul 3, 2014 at 8:00
  • 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
    Commented Jan 17, 2018 at 11:41
  • 1
    @HermeticallySealedHalibut Added support for cleveref
    – egreg
    Commented Feb 2 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}
0
4

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

\newtheorem*{theorem8}{Theorem 8}
2
  • 1
    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
    Commented May 2, 2022 at 16:12
  • hours before a conference deadline, this is the answer we want to see :) Commented May 22 at 1:49
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}

You must log in to answer this question.

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