This problem is related to my previous question on how to put name, number and note of a theorem in the margin column.
After hours of trial and error I have come up with the following solution:
\documentclass[fontsize=11pt, paper=a4, DIV=classic]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{ragged2e}
\usepackage{changepage}
\usepackage{blindtext}
\KOMAoption{mpinclude}{true}
\recalctypearea
\setlength{\marginparsep}{2em}
\setlength{\marginparwidth}{10em}
\makeatletter
\newcommand{\RaggedOuter}%
{%
\if@twoside%
\checkoddpage%
\ifoddpage%
\RaggedRight%
\else%
\RaggedLeft%
\fi%
\else%
\RaggedRight%
\fi%
}
\newcommand{\cl@ThmMarginCaption}%
{
\leavevmode%
\marginpar%
{%
\RaggedOuter%
\hbadness=10000%
\the\thm@headfont\thmt@thmname~\thechapter.\arabic{\thmt@envname}\\%
\the\thm@notefont\thmt@optarg%
}%
}
\declaretheoremstyle%
[%
spaceabove={\parskip},%
spacebelow=0pt,%
headindent=0pt,%
postheadspace=0pt,%
headformat={},%
headpunct={},%
headfont=\sffamily\bfseries\small,%
notefont=\rmfamily\mdseries\small,%
bodyfont=\normalfont,%
preheadhook={},%
postheadhook={\cl@ThmMarginCaption},%
]%
{thmmcaption}
\makeatother
\declaretheorem[name=Definition, numberwithin=chapter, style=thmmcaption]{definition}
\declaretheorem[name=Satz, numberwithin=chapter, style=thmmcaption]{theorem}
\begin{document}
\chapter{Theorems with margin captions}
\blindtext
\begin{definition}[Kellerautomat (KA)\\ pushdown automaton (PDA)]
Eine ganz wichtige Definition, die natürlich länger als eine Zeile sein sollte, um sehen zu können, ob alles funktioniert wie gewünscht.
\end{definition}
\blindtext
\clearpage
\blindtext
\begin{theorem}[Satz von Radó]
Die Radó-Funktion ist nicht berechenbar.
\end{theorem}
\blindtext
\end{document}
This is close to the desired result, however, there is unwanted space at the beginning of the theorem body.
This space disappears when removing the command \leavevmode, but then the margin note is attached to the last line of the previous paragraph.
The behaviour is not too surprsing because the manual states in the explanation of postheadhook: "Note that all backends seem to delay typesetting the actual head, so code here should probably enter horizontal mode to be sure it is after the head, but this will change the spacing/wrapping behaviour if your body starts with another list."
However, what is the cause for this space of about 0.3 em, and how can I remove it?
(I will keep my previous question open, since there might be a completely different approach for placing theorem name, number and note in the margin column.)


\setbox0=\hbox{~}\xdef\spacewd{\the\wd0}, and then,\declaretheoremstyle[...,headindent=-\spacewd,...]. However, where this would fail is if yourtheoremname extended more than one line, causing the default spacing to be compressed. – Steven B. Segletes Nov 02 '17 at 12:57\let\cl@orig@definition\definition \let\cl@orig@enddefinition\enddefinition \renewenvironment{definition}[1][]{\cl@orig@definition[#1]\ignorespaces}{\cl@orig@enddefinition}The disadavantage is that it needs to be done for each theorem separately. – Matthias Nov 02 '17 at 13:02\hspace{-\spacewd}with the above definition of\spacewdat the end of\cl@ThmMarginCaption. Is this better than theheadindentoption? – Matthias Nov 02 '17 at 13:07\spacewdis a fixed dimension, whereas the gap we are trying to eliminate is given by a\space, which can change size depending on how many characters are on the line, to meet alignment requirements. – Steven B. Segletes Nov 02 '17 at 13:09{\csname cl@orig@#2\endcsname[##1]\ignorespaces}. – Matthias Nov 02 '17 at 13:23\Declaretheorem. It produces an error if a theorem environemt is used without optional parameter for assigning a note. This error does not occur with the original\declaretheorem. Do you have any idea what do look at? – Matthias Nov 04 '17 at 14:13#1and invoke with no optional argument in that case. See change to 1st line of code in\Declaretheorem. – Steven B. Segletes Nov 04 '17 at 17:22\Declaretheorem{MyTheorem}without your fix. What I meant is that using a theorem without the optional parameter does not work, i.e.,\begin{MyTheorem}[note]is fine, whereas\begin{MyTheorem}or\begin{MyTheorem}[]result in an error. When using\declaretheorem{MyTheorem}in the preamble, all three versions work well. – Matthias Nov 04 '17 at 17:51\renewenvironment{#2}[1][~]the error is gone. – Matthias Nov 04 '17 at 20:36