In the following code, would it be possible to automatically generate the desired output (2nd paragraph) form the given input (1st paragraph)?
\documentclass[parskip]{scrbook}
\usepackage{xparse}
\NewDocumentCommand{\mref}{ m o }{
\IfValueT{#2}{%
\textbf{#2}
}%
\marginpar{$\to$ \ref{#1}}%
}
\usepackage{lipsum}
\begin{document}
Input:\\
Text \mref{foo}[Foo] with two Refs \mref{bar}[Bar-Text] in a line.
\lipsum[2]
Desired Output:\\
Text \textbf{Foo} with two Refs \textbf{Bar-Text}
\marginpar{$\to$ \ref{foo} \enskip $\to$ \ref{bar}}
in a line.
\lipsum[2]
\chapter{Foo}\label{foo}
\chapter{Bar}\label{bar}
\end{document}
My idea would be to test and save the y position on the page and collect all margin pars until the shipout. Then combine this with the same y value and add them to the page as an overlay. I didn’t implement that yet as I want to wait if there is an easier more elegant way …
reledmac'sledsidenote-mechanism, which does this. I didn't look closely at it but perhaps the code can be extracted and used separately. – Florian May 23 '17 at 08:20