4

Is there an elegant way to vertical align the margin of a paragraph exactly to the half of the length thereof? I know the command to manually shift the vertical position with \marginnote{text}[voffset], but this seems to be a bit exhausting when you have numerous paragraphs.

MWE: (with manual offset)

\documentclass{scrreprt}
\usepackage{blindtext}
\usepackage{marginnote} %Margin note
\renewcommand*{\marginfont}{\sffamily\footnotesize} %Format of margin

\begin{document}
\marginnote{Lorem ipsum}[2.0cm]
\blindtext

\end{document}

Thanks in advance!

  • Margin of a section? What is a section in this context? Here are no sections in this MWE – daleif May 29 '18 at 11:46
  • I mean a text section/paragraph. Not sure about the correct term. I did NOT meant the \section or \paragraph command. – D5E May 29 '18 at 13:02
  • 1
    Just out of curiosity: Where shall the margin note be placed when there is a page break within the paragraph? – Lupino May 31 '18 at 8:32
  • Good question. Still in the mid of the paragraph, if possible. If the page break is exactly in the middle, I would prefer the margin note on the first page. – D5E May 31 '18 at 8:35
5
+50

You can try something like this, saving the paragraph and measuring its size and the outputting the text with parameters restored.

Sample output

\documentclass{scrreprt}

\usepackage{lipsum,blindtext} % Dummy text
\usepackage{marginnote} % Margin note
\renewcommand*{\marginfont}{\sffamily\footnotesize} % Format of margin

\newsavebox\mytempbox
\newlength{\currparindent}
\newcommand{\parwithmarginnote}[2]{\setlength{\currparindent}{\parindent}%
\sbox{\mytempbox}{%
  \parbox[t]{\textwidth}{\setlength{\parindent}{\currparindent}
  #2\par
  \xdef\myprevdepth{\the\prevdepth}%
  }%
}%
\marginnote{#1}[.5\dp\mytempbox]\par\noindent
\usebox{\mytempbox}\par
\prevdepth\myprevdepth}

\begin{document}

\lipsum[1]

\parwithmarginnote{Lorem ipsum}{\blindtext}

\lipsum[2]

\end{document}
  • Great solution! However, mentioned already by @Lupino: Page breaks are complicated. – D5E May 31 '18 at 11:14
  • 2
    Indeed page breaks require some extra code depending on the desired behaviour. A simple approach would be to include \needspace in the macro... – Andrew Swann May 31 '18 at 11:30
4

The asked for feature would need a package of its own, and in fact is the kind of things which would need a more structured document layout than LaTeX offers (which is but a slim layer on top of TeX) to its users. Nowadays probably LuaTeX offers the needed hooks into paragraph building.

Anyway here we go with poor man approach (an approach which does not even look into the way marginnote package is implemented). Of course it has some rather severe limitations: one which comes to mind is the case of a paragraph split across two pages...

The \myparnote must be used at end of paragraph in source.

As package marginnote uses \pdfsavepos etc... at least 2 compilations are needed.

\documentclass{article}

\usepackage{marginnote}
\usepackage{ragged2e}

\newcommand\myparnote[1]{%
  \par
  \xdef\zzz{\the\prevgraf}%
  \marginnote{\smash{\parbox{\marginparwidth}{\RaggedRight#1}}}%
             [-0.5\dimexpr\numexpr1+\zzz\relax\baselineskip\relax]%
}

\begin{document}
The command
\verb|\vadjust(filler){vertical mode material}|
is only allowed in horizontal and math modes (but it is not a (horizontal
command). Vertical mode material specified by \verb|\vadjust| is moved from the
horizontal list in which the command is given to the surrounding vertical
list, directly after the box in which it occurred.
\myparnote{Hello}

Any vertical material in a \verb|\vadjust| item is processed in internal
vertical mode, even though it will wind up on the main vertical list. For
instance, the \verb|\ifinner| test is true in a \verb|\vadjust|, and at the
start of the vertical material \verb|\prevdepth|=-1000pt.
\myparnote{I am here!}

The command
\verb|\vadjust(filler){vertical mode material}|
is only allowed in horizontal and math modes (but it is not a (horizontal
command). Vertical mode material specified by \verb|\vadjust| is moved from the
horizontal list in which the command is given to the surrounding vertical
list, directly after the box in which it occurred.
Any vertical material in a \verb|\vadjust| item is processed in internal
vertical mode, even though it will wind up on the main vertical list. For
instance, the \verb|\ifinner| test is true in a \verb|\vadjust|, and at the
start of the vertical material \verb|\prevdepth|=-1000pt.
\myparnote{Hi again,
  it's me, and look how nice vertically centered I am.}

\end{document}

As the above illustrates, the OP left open some rather crucial questions about what was meant exactly by "vertically aligned", so we made a guess.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.