25

Suppose I have a theorem that is defined a certain way, as follows.

\newtheorem*{mytheorem}{Foo}

And I want to redefine it another way further down in my document, as below.

\newtheorem*{mytheorem}{Bar}

What is the easiest way to go about this?

I would like something like this, but I get undefined control sequence on \renewtheorem.

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsthm}

\begin{document}
\theoremstyle{definition}
\newtheorem*{thmTemp}{Foo}
\begin{thmTemp} First one.  \end{thmTemp}

\renewtheorem*{thmTemp}{Bar}
\begin{thmTemp} Second one.  \end{thmTemp}
\end{document}

When I run pdflatex on this, I get the following:

! Undefined control sequence.
l.10     \renewtheorem
                  *{thmTemp}{Bar}

I am avoiding ntheorem because it does not preserve the theorem styles defined by amstheorem, even when the amsthm option is passed to it, and I would prefer not to redefine those styles.

Anke
  • 400
  • 1
  • 14
merlin2011
  • 3,343
  • 1
    Have you tried \renewtheorem? It would be helpful if you composed a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. That way we would know which packages you are using as some solutions may be package specific. – Peter Grill Mar 18 '13 at 06:13
  • Try replacing amsthm with ntheorem. – Peter Grill Mar 18 '13 at 06:22
  • @PeterGrill, There's actually a specific reason I want to use amsthm and not ntheorem. I prefer not to redefine \theoremstyle{definition} among others. – merlin2011 Mar 18 '13 at 06:23
  • And yes, I have tried the [amsthm] option to ntheorem, but it does not achieve the desired effect. – merlin2011 Mar 18 '13 at 06:23
  • You add this requirement to the question. – Peter Grill Mar 18 '13 at 06:27
  • @PeterGrill, As requested, I have added the requirement. – merlin2011 Mar 18 '13 at 06:47
  • Thanks. Would be good also if you explained exactly what the issue with using ntheorem is. It is not obvious to me, but perhaps to those who are more knowledgeable it would be. Once your objection is understood perhaps there is a way to use ntheorem that overcomes that objection. – Peter Grill Mar 18 '13 at 06:54
  • Errrr... I was about to give a possible helpful answer along the lines of \let\thmTemp\relax\newtheorem*..., but I object to using the \new... and \renew... family as in-document (as opposed to preamble) commands, so I won't. – Ulrich Schwarz Mar 18 '13 at 07:00
  • @UlrichSchwarz, please give your answer. I have tried something similar but could not get it to work. – merlin2011 Mar 18 '13 at 07:02
  • This question is actually closely related to my other question here http://tex.stackexchange.com/questions/103011/how-can-i-define-a-new-environment-which-limits-the-scope-of-newtheorem-command?rq=1 and I think a helpful answer here might resolve that too. – merlin2011 Mar 18 '13 at 07:03
  • 1
    Perhaps it would help if you specified more closely what you mean by 'redefine it another way'... How do you expect the redefined theorem environment to behave? Do you simply want to change the heading and continue numbering with the previous counter? Or do you also want to change the style of the theorem? – bencarabelli Mar 18 '13 at 08:12
  • See also http://tex.stackexchange.com/q/65951 – Philippe Goutet Mar 18 '13 at 11:13

3 Answers3

15

If one systematically needs different theorems definitions in the same documents, egreg approach is convenient. But many of us might have a mymath.tex with common shortcuts and settings, which is a very likely place for the \newtheorem definitions, and occasionally one might want to override these definitions, e.g. to change the theorems' locale.

In these cases, editing mymath.tex or mantaining a second one is inconvenient and prone to errors, much better to plug a new altthm.tex in the main document overriding theorem definitions.
To this end, we need a \renewtheorem command:

\makeatletter
\def\renewtheorem#1{%
  \expandafter\let\csname#1\endcsname\relax
  \expandafter\let\csname c@#1\endcsname\relax
  \gdef\renewtheorem@envname{#1}
  \renewtheorem@secpar
}
\def\renewtheorem@secpar{\@ifnextchar[{\renewtheorem@numberedlike}{\renewtheorem@nonumberedlike}}
\def\renewtheorem@numberedlike[#1]#2{\newtheorem{\renewtheorem@envname}[#1]{#2}}
\def\renewtheorem@nonumberedlike#1{  
\def\renewtheorem@caption{#1}
\edef\renewtheorem@nowithin{\noexpand\newtheorem{\renewtheorem@envname}{\renewtheorem@caption}}
\renewtheorem@thirdpar
}
\def\renewtheorem@thirdpar{\@ifnextchar[{\renewtheorem@within}{\renewtheorem@nowithin}}
\def\renewtheorem@within[#1]{\renewtheorem@nowithin[#1]}
\makeatother

Use as the \renewtheorem as the standard \newtheorem:

\renewtheorem{env_name}{caption}[within]
\renewtheorem{env_name}[numbered_like]{caption}

It works with amsthm.

The MWE, excluding the \renewtheorem definition, is:

\documentclass{article}
\begin{document}

\newtheorem{thm}{Theorem}[section]
\begin{thm}
My theorem ...
\end{thm}

\renewtheorem{thm}{Proposition}[section]
\begin{thm}
My theorem ...
\end{thm}

\end{document}
antonio
  • 1,446
  • Please, do not tell me you submit papers using some sort of mymacros.tex. If you do, I truly hope I won't meet the papers. – yo' Feb 26 '16 at 22:30
  • 8
    @yo': Hmm... sorry to disappoint you, but LaTeX most relevant feature is macros, which avoid repetitive typing. Putting all of them in the preamble, or even worse in the body of text, creates clutter and defeats the whole purpose of LaTeX being focused on content, rather than presentation. Anyway, unless you are involved publishing a journal, you are not supposed to read the sources producing the papers.

    P.S. in case you might be interested, I fixed a bug in the code.

    – antonio Feb 27 '16 at 20:10
  • Well, if people define 100 macros in their "preamble file" and then use 5 of them in the article, it's anything but convenient. (btw, I am involved in journal typesetting...) – yo' Feb 27 '16 at 20:16
  • 5
    @yo' from your perspective, you are right. From the author perspective, you are absolutely wrong. But I understand that journal typesetting is the key part of Science. – Spherical Triangle Mar 01 '16 at 17:18
  • 1
    @antonio Can your trick be made to work with \newtheorem{thm} ? – Paulo Ney Apr 26 '16 at 06:31
  • 1
    Easier: use \newcommand*{\deftheorem}[3]{\@ifundefined{#1}{\newtheorem{#1}{#2}[#3]}{}} and replace \newtheorem{type}{Name}[within] by \deftheorem{type}{Name}{within} in your mymath.tex, and be sure to issue any occasional \newtheorem before loading your style file. – Hugo Raguet Feb 20 '18 at 02:50
  • 1
    Also, I know from experience that researchers who maintain a personal style file ends up with better (La)TeX code (easier to read, more accurate and elegant typesetting), which should in the end ease the work of publishers. The latter should actually take this into account before imposing class files loading packages known for conflicting with many others. – Hugo Raguet Feb 20 '18 at 03:02
10

Rather than redefining the environment each time, I would define a wrapper:

\documentclass{article}
\usepackage{amsthm}
\theoremstyle{definition}
\newtheorem*{inner}{\innerheader}
\newcommand{\innerheader}{}
\newenvironment{defi}[1]
 {\renewcommand\innerheader{#1}\begin{inner}}
 {\end{inner}}

\begin{document}
\begin{defi}{Foo}
First one.
\end{defi}

\begin{defi}{Bar}
Second one.
\end{defi}
\end{document}

enter image description here

egreg
  • 1,121,712
6

Reducing @antonio's answer: you can get a more robust result with only the first block of code; instead of a \renewtheorem, one can just as easily use a \cleartheorem as below:

\makeatletter
\def\cleartheorem#1{%
    \expandafter\let\csname#1\endcsname\relax
    \expandafter\let\csname c@#1\endcsname\relax
}
\makeatother

In my view, the extra complexity and dependency on \newtheorem (which precludes the use of re-defining a theorem with \declaretheorem from thmtools, and assumes additional things about the inner workings of the theorem package & its interface) is not justified over the extra line of code to get the desired effect:

% suppose you're using \declaretheorem
\usepackage{amsthm, thmtools}

% this could be included from a separate file \declaretheorem[name=Definition,numberwithin=section]{defn}

\cleartheorem{defn} \declaretheorem[name=RevisedDefinition]{defn} % no error here


Edit: for further convenience, one can clear all theorems with one command, so that there is no more than one line of redundant code:

% define this in the preamble in between \makeatletter and \makeatother
\def\clearthms#1{ \@for\tname:=#1\do{\cleartheorem\tname} }

Then in the document:

% clear the previous definitions
\clearthms{defn,example,theorem,coro,prop}

% now execute all of the commands exactly as if the preamble didn't have them... \theoremstyle{plain} \newtheorem{theorem}{Theorem}[section] \newtheorem{coro}{Corollary}[theorem] \newtheorem{prop}[theorem]{Proposition}

\theoremstyle{definition} \newtheorem{defn}{Definition}{section} \declaretheorem[qed=$\square$]{example}

oricha
  • 321