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%
\the\thm@notefont\thmt@optarg%
}%
}

\declaretheoremstyle%
[%
spaceabove={\parskip},%
spacebelow=0pt,%
notefont=\rmfamily\mdseries\small,%
bodyfont=\normalfont,%
]%
{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

\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.)

A % was needed to close out \begin{theorem}[Satz von Radó]%. I realize it would be preferable to ignore that space via code rather than having to blot it out manually. See ADDENDUM for that possibility:

\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%
\the\thm@notefont\thmt@optarg%
}%
}

\declaretheoremstyle%
[%
spaceabove={\parskip},%
spacebelow=0pt,%
notefont=\rmfamily\mdseries\small,%
bodyfont=\normalfont,%
]%
{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

\end{theorem}

\blindtext

\end{document}


Based on a comment from the OP on how the environment could be redefined, one at a time, to insert an appropriate \ignorespaces, I decided the approach could be automated with the introduction of an ancillary macro (requiring \makeatletter). EDITED to handle blank optional argument.

\newcommand\Declaretheorem[2][]{%
\ifx\relax#1\relax\declaretheorem{#2}\else\declaretheorem[#1]{#2}\fi%
\expandafter\let\csname cl@orig@#2\expandafter\endcsname\csname#2\endcsname%
\expandafter\let\csname cl@orig@end#2\expandafter\endcsname\csname end#2\endcsname%
\renewenvironment{#2}[1][]{\csname cl@orig@#2\endcsname[##1]\ignorespaces}%
{\csname cl@orig@end#2\endcsname}%
}


Then, if I change the declaration from

\declaretheorem[name=Satz,       numberwithin=chapter, style=thmmcaption]{theorem}


to

\Declaretheorem[name=Satz,       numberwithin=chapter, style=thmmcaption]{theorem}


it all works automatically, without manual intervention. This one-letter fix should be applied to all declared theorems.

• Thank you very much. It seems to be the only place I did not look at. Nov 2 '17 at 12:54
• @Matthias a semi-automated approach would be to say, in your preamble, \setbox0=\hbox{~}\xdef\spacewd{\the\wd0}, and then, \declaretheoremstyle[...,headindent=-\spacewd,...]. However, where this would fail is if your theorem name extended more than one line, causing the default spacing to be compressed. Nov 2 '17 at 12:57
• @StevenBSegletes What do you think about this? \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. Nov 2 '17 at 13:02
• It also seems to be possible to add \hspace{-\spacewd} with the above definition of \spacewd at the end of \cl@ThmMarginCaption. Is this better than the headindent option? Nov 2 '17 at 13:07
• @Matthias To your most recent comment, I think both approaches suffer the same risk: \spacewd is 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. Nov 2 '17 at 13:09