1

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.

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

Matthias
  • 1,956

1 Answers1

1

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%
    \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}

enter image description here


ADDENDUM

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. – Matthias Nov 02 '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. – Steven B. Segletes Nov 02 '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. – Matthias Nov 02 '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? – Matthias Nov 02 '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. – Steven B. Segletes Nov 02 '17 at 13:09
  • @Matthias I like your approach from 2 comments ago. I will try to automate it for all definitions. – Steven B. Segletes Nov 02 '17 at 13:10
  • @Matthias See ADDENDUM for automated fix. – Steven B. Segletes Nov 02 '17 at 13:18
  • @StevenBSegletes: This is just perfect. Thank you so much. Just a small correction: The optional parameter must be added to the call of the original begin enviroment command: {\csname cl@orig@#2\endcsname[##1]\ignorespaces}. – Matthias Nov 02 '17 at 13:23
  • @StevenBSegletes Sorry for bothering you again. There seems to be one problem with \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
  • @Matthias Sure. Fixed. I had to check for the blankness of #1 and 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
  • @StevenBSegletes The error does not even occur when I invoke \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
  • It seems I found a workaround. With \renewenvironment{#2}[1][~] the error is gone. – Matthias Nov 04 '17 at 20:36