ragged sidenotes or numbered marginnotes

I have a two-sided document with fairly large margins and figures spreading into the margins. I'd like to set marginnotes/sidenotes with the following requirements:

1. superscript number from sidenotes package
2. raggedoutside like marginnote package, i.e. depending on where the note appears, it's raggedleft (even) or raggedright (odd)
3. notes should not spread further than the figure. This is currently defined through the marginparwidth settings of geometry and should not break with a new package.

Here's my MWE using both sidenotes and marginnoted to show the difference:

\documentclass[a4paper,11pt,twoside,openright,parskip=half]{scrbook}

\usepackage[ngerman, english]{babel}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{calc}

\makeatletter
\newlength \figwidth
\setlength \figwidth {-3cm}
\makeatother

\usepackage{geometry}
\geometry{a4paper, top=30mm, left=30mm, right=55mm, bottom=35mm, marginparwidth=-\figwidth-1em, marginparsep=1em}

\usepackage{marginnote}
\usepackage{sidenotes}
\renewcommand*{\marginfont}{\footnotesize}
\newcommand{\smallside}[1]{\sidenote{\footnotesize{#1}}}

\usepackage{caption}
\usepackage{graphicx}
\usepackage[strict]{changepage}
\newenvironment{widefigure}[1][tbp]

\usepackage{blindtext}

\begin{document}
\begin{widefigure}[tbp]
\includegraphics[width=\linewidth, height=9cm]{example-image}
\caption{Some figure caption in here that does not really matter but should be long enough to fill the line and also break}
\end{widefigure}
\blindtext
\marginnote{some long text for the margin}
\blindtext
\smallside{some long text for the margin}
\blindtext
%\newpage
\begin{widefigure}[tbp]
\includegraphics[width=\linewidth, height=9cm]{example-image}
\caption{Some figure caption in here that does not really matter but should be long enough to fill the line and also break}
\end{widefigure}
\marginnote{some extremely long text for the margin going on many lines}
\blindtext
\smallside{some extremely long text for the margin going on many lines}
\blindtext
\end{document}


and a screenshot of the document:

\sidenote uses \marginnote when an offset is given and \marginpar otherwise. \marginnote does the \raggedleft/\raggedright stuff but \marginpar doesn't. On the other hand \marginpar tries to float the notes so that they don't bump into each other.

So there are basically two solutions:

(1) Give an offset in the definition of \smallside:

\newcommand{\smallside}[1]{\sidenote[][0pt]{\footnotesize{#1}}}


But then your sidenote will no longer float, so they can bump into each other. But you do get the proper ragged stuff.

(2) Change the definition of the sidenote macro part where the choice is made between \marginpar and \marginnote and supply the \ragged... stuff to \marginpar.

\makeatletter
\RenewDocumentCommand \@sidenotes@placemarginal { m m }
{%
\IfNoValueOrEmptyTF{#1}
{\marginpar[\raggedleftmarginnote #2]{\raggedrightmarginnote #2}}
{\marginnote{#2}[#1]}%
}
\makeatother


By the way, I think there are some spurious spaces in the sidenotes package. I had to get rid of some in the above macro definition.

• Of course I want them to float, interestingly no overlapping notes occurred in my test documents. Thank you for even anticipating what I did not ask for! – Wiebke Jan 10 '17 at 18:04

Piet van Oostrum's answer did not compile for me (I am not reputed enough to comment on it). But surrounding the first (optional) argument to \marginpar with braces does the job. I also replaced the \ragged...marginnote commands of the marginnote package by just plain \ragged... commands, but that is not necessary:

\makeatletter
\RenewDocumentCommand \@sidenotes@placemarginal { m m }
{
\IfNoValueOrEmptyTF{#1}
{\marginpar[{\raggedleft #2}]{\raggedright #2}}
{\marginnote{#2}[#1]}%
}