I want to define a theorem environment where I can issue the numbering manually instead of its following some inner counter. Looking around I found this answer. There they propose the following code as solution:


The code works as I want, however I was trying to understand everything in the code, in case I want to tweak it a little bit (and also to learn what I am putting in my document) and am a little bit stuck.

First of all, the \newenvironment has the following structure:


I understand the third argument is left empty; no problem with that. Then comes the \renewcommand, which has the structure:


Here I have trouble following what's going on, since no braces are being used; I thought maybe the command allows you to omit them, but I didn't find any info about this. And after that it gets even more confusing, since the commands \theinnercustomthm, \innercustomthm and \endinnercustomthm appear custom made for this particular solution, and I don't get how they work.

One thing I noticed, for example, is that changing the name of the environment (say mytheo instead of customthm) breaks the code down.

If someone can explain what is going on or point me to some manual or webpage were I can understand what is going on I would deeply appreciate it.

2 Answers 2


The first thing to note is that


internally does something similar to


where the <begin> and <end> codes are not of a concern here. Note that the environment looks for an optional argument (a theorem note or attribution). It also sets up a counter with the same name as the environment.

The chosen name is long and unappealing on purpose: it's very unlikely that such a name will conflict with existing or imagined environments.

For the application we have in mind we don't really need the counter, but rather the fact that any call of innercustomthm will step the counter and set what's needed in order that the next \label commands will use the counter's value. However, LaTeX doesn't look at the value itself, but uses the current meaning of \the<counter>, in this case \theinnercustomthm. The meaning of \theinnercustomthm is also used for numbering the environment.

Our idea is to manually number theorems, because we need to quote theorems with the same number they have on another publication, so automatic numbering is out of question. Well, we make up a wrapper to innercustomthm that will take as argument the number we want the statement to have.

So the idea is to start an environment taking an argument which will be used to (locally) redefine \theinnercustomthm and call the “inner” environment that will do its job of typesetting the statement.

The definition of customthm might be


but this has a small drawback: if you forget \end{customthm}, you'll end up with the error of “Missing \end” mentioning innercustomthm rather than customthm. So, macho programmers use in this case the internal version of the \begin and \end routines for the “inner” environment: the bookkeeping has already been done by \begin{customthm} and we need not repeat it. So we get


Well, the actual code you quote is


Why the missing braces? Another (bad) habit of macho programmers who know that those braces are unnecessary and will be stripped out anyway.

In case of doubt, use them.


First, we should ask what \newvironment{myenv}{..start code...}{...end code...} actually does. Simplifying it slightly, what happens is that \newenvironment defines two commands \myenv and \endmyenv, where \myenv expands to the ...start code... and \endmyenv expands to ...end code.... When you write \begin{myenv}...\end{myenv} LaTeX replaces this environment with code that is (almost) equivalent to {\myenv ... \endmyend}. (A little more happens behind the scenes.)

Next, the line \newtheorem{innercustomthm}{Theorem} defines the innercustomthm environment be a "Theorem" environment that uses the innercustomthm counter. In particular, the "theorem number" for this environment is printed using \theinnercustomthm.

Now let's consider:


This defines the customthm environment, which takes one argument #1. By the first paragraph above, \begin{customthm}[X]...\end{customthm} expands to:

{\renewcommand\theinnercustomthm{X}\innercustomthm ... \endinnercustomthm}

That is, \theinnercustomthm is redefined to be equal to X after which the innercustomthm environment is applied. So, the customthm environment is essentially the same as the innercustomthm environment except that \theinnercustomthm is first set equal to X, where X is the argument to the customthm environment. So the net effect is that this environment prints: "Theorem X" ...

Finally, you said changing the name of the environment (say mytheo instead of customthm) breaks the code. If what I have said is correct then this can't be true! Indeed, as the following code shows, the code works in exactly the same way if we change the name of the environment:







You must log in to answer this question.

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