# Theorem name and number in margin, note in text

I'm using thmtools to make ultra-fancy theorems. I would like the Theorem name and number (e.g., "Theorem 1") to jut out into the left margin of the text, and the note (e.g., "The Fundamental Theorem of Calculus" be in the regular text. But I can't get the alignment quite right. Here is a minimal example:

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

\pagestyle{empty}

\declaretheoremstyle[
notefont=\normalfont,
bodyfont=\itshape,
\makebox[0pt][r]{\NAME\ \NUMBER\ }{\NOTE}%
},
]{theorem}

\declaretheorem[style=theorem]{theorem}

% I need to undeclare the proof environment that amsthm provides but I can't use \renewenvironment
\let\proof\relax
\let\endproof\relax

\declaretheoremstyle[
notefont=\itshape
bodyfont=\normalfont,
\makebox[0pt][r]{\NAME\ }\NOTE},
]{proof}

\declaretheorem[
style=proof,
qed=\qedsymbol]{proof}

\begin{document}

\begin{theorem}[The Fundamental Theorem of Calculus]
Let $f$ be an integrable function on $[a,b]$ and define
$g(x) = \int_a^x f(t) \,dt.$
If $f$ is continuous at $x$ in $(a,b)$, then $g$ is differentiable at $x$ and
$g'(x) = f(x).$
\end{theorem}

\begin{proof}
Let $h>0$ be given so that $x+h < b$ (The case $h<0$ is treated similarly).  We have
$\frac{g(x+h) - g(x)}{h} = \frac{1}{h} \int_x^{x+h} f(t)\,dt.$
Let $M_h$ be the maximum value of $f$ on $[x,x+h]$, and let $m_h$ the minimum value of $f$ on $[x,x+h]$.  We have
$m_h \cdot h \leq \int_x^{x+h}f(t)\,dt \leq M_h \cdot h$
So
$m_h \leq \frac{g(x+h) - g(x)}{h} \leq M_h.$
Since $f$ is continuous at $x$, as $h\to 0$, both $m_h$ and $M_h$ tend to $f(x)$.
\end{proof}

\end{document} As you can see, the note is too far to the right. And in the proof environment, there is no note, but the text begins too far to the right as well.

What's putting the extra space in there and how can I get rid of it?

Edit Scroll down to my answer to see how I solved it. I accepted Phil's answer because it's less hacky than Gonzalo's (did not involve @) and got me closest to the result I wanted.

• I had two other problems that I figured out in the process of preparing the minimal example. That is a testament to the usefulness of the minimal example! – Matthew Leingang Mar 16 '11 at 0:31
• @Matthew Leingang: Try \makebox[0pt][r]{\NAME\ \NUMBER\ }\NOTE} to \makebox[0pt][r]{\NAME\ \NUMBER}\NOTE}. Maybe xspace package is helpful. – Rushavski Mar 16 '11 at 0:59
• @Matthew: I think the code + picture in your Resolution should be an answer, no? I think it's best to keep questions for for the actual question (and maybe some discussion of the answers, why not). – Hendrik Vogt Mar 17 '11 at 13:23
• @Hendrik: You're probably right. I didn't want to steal glory (i.e., rep) from the two answerers, but I will make my answer an answer. – Matthew Leingang Mar 17 '11 at 16:47
• In thmtools 58 and higher, the pertinent space is replaced with \thmt@space with a \providecommand defaulting it to a space, so it will be configurable in a less hackish way. (No concrete release date on that yet, though.) – Ulrich Schwarz Mar 21 '11 at 18:37

A quick browse of the code (in the later sections of thmtools.pdf) shows that the macro \NODE itself is inserting a space before the opening parenthesis. If you don't mind being crude, you can do the following:

\newlength{\spacelength}
\settowidth{\spacelength}{\normalfont\ }
\declaretheoremstyle[
notefont=\normalfont,
bodyfont=\itshape,
\makebox[0pt][r]{\NAME\ \NUMBER\ }\hskip-\spacelength{\NOTE}%
},
]{theorem}


Giving a quick look to the package implementation, I found that there's a hardcoded space before the note; a possible solution would be to remove that space (this will, of course, affect all the theorems). You could add the following to the preamble of your document (the space has already been suppressed):

\makeatletter
\def\NUMBER{\bgroup\@upn{##2}\egroup}%
\def\NOTE{\if=##3=\else\bgroup\the\thm@notefont(##3)\egroup\fi}%
}
\def\thmt@tmp{#1}%
\@onelevel@sanitize\thmt@tmp
%\tracingall
}%
\else
#1
}%
\fi
}
\makeatother


The original is almost the same; there's only a difference in the line starting with \def\NOTE :

\def\NOTE{\if=##3=\else\bgroup\ \the\thm@notefont(##3)\egroup\fi}%


you can see the extra space right before \the\thm@notefont(##3).

The only solution I found for the proof environment situation was to manually suppress the extra space, adding something like this

postheadspace=-4pt,


to the corresponding \declaretheoremstyle command

• Yep, so the "real" solution would be to add a new key controlling that space. – Matthew Leingang Mar 16 '11 at 2:30
• Good point, I've opened a ticket for that. Let's see if it does me any good. ;) – Ulrich Schwarz Mar 16 '11 at 5:32

Thanks to Phil and Gonzalo for their answers. I took aspects from both, so don't vote my answer up without voting theirs up too.

I used both Gonzalo's suggestion to adjust postheadspace (to 0pt) in the proof environment, and Phil's suggestion to “backspace.” Rather than use a global variable as Phil did, I used a temporary box so that the negative interword space would be the same width as the interword space in the theorem's head font. If there is another way or existing macro which does the same thing, let me know.

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

\pagestyle{empty}

\declaretheoremstyle[
notefont=\normalfont,
bodyfont=\itshape,
\makebox[0pt][r]{\NAME\ \NUMBER\ }\setbox0\hbox{\ }\hspace{-\the\wd0}\NOTE%
},
]{theorem}

\declaretheorem[style=theorem]{theorem}

% I need to undeclare the proof environment that amsthm provides but I can't use \renewenvironment
\let\proof\relax
\let\endproof\relax

\declaretheoremstyle[
notefont=\itshape,
bodyfont=\normalfont,
\makebox[0pt][r]{\NAME\ }\setbox0\hbox{\ }\hspace{-\the\wd0}\NOTE},
]{proof}

\declaretheorem[
style=proof,
qed=\qedsymbol
]{proof}

\begin{document}

\begin{theorem}[The Fundamental Theorem of Calculus]
Let $f$ be an integrable function on $[a,b]$ and define
$g(x) = \int_a^x f(t) \,dt.$
If $f$ is continuous at $x$ in $(a,b)$, then $g$ is differentiable at $x$ and
$g'(x) = f(x).$
\end{theorem}

\begin{proof}
Let $h>0$ be given so that $x+h < b$ (The case $h<0$ is treated similarly).  We have
$\frac{g(x+h) - g(x)}{h} = \frac{1}{h} \int_x^{x+h} f(t)\,dt.$
Let $M_h$ be the maximum value of $f$ on $[x,x+h]$, and let $m_h$ the minimum value of $f$ on $[x,x+h]$.  We have
$m_h \cdot h \leq \int_x^{x+h}f(t)\,dt \leq M_h \cdot h$
So
$m_h \leq \frac{g(x+h) - g(x)}{h} \leq M_h.$
Since $f$ is continuous at $x$, as $h\to 0$, both $m_h$ and $M_h$ tend to $f(x)$.
\end{proof}
\end{document} It still doesn't look great with notes in the proof environment. I think there's another hard-coded space.

I'm looking forward to Ulrich's update. :-)