multicol – How to Add Margin Notes in a Multicolumn Environment

marginparmulticol

I have been typesetting a two-columned book where I sometimes need to put a mark in the margin to say, that particular line might need special attention. (The book is actually a psalter – for chanting.)

Because it isn't possible to use \marginpar in the multicol environment, I use
\marginnote from the package marginnote instead. The problem is, that all the
margin notes appear in the outer margin of the page. I need each to appear
next to the column, where it occurs – some in the inner margin, some in the outer margin – to match the problematic lines closely.

Is there any way to achieve this? I searched the internet thoroughly and haven't found anything relevant…

\documentclass{article}

\usepackage{multicol}
\usepackage{marginnote}

\begin{document}
\begin{multicols}{2}

Some text, a very long one.

And somewhere in the middle of it I need a (very small) margin note, like this:

Text text text\marginnote{\textbf{!}} and more text...

\end{multicols}
\end{document}

Best Answer

One can use \pdfsavepos to detect if the mark is on the left or right column. This is best done using Heiko Oberdiek's zref package.

Since we can get the actual x-position of the mark (in sp units) we can use it to detect if we are in the left or right columns.

A somewhat simplistic implementation is shown in the minimal below.

\documentclass{article}
\usepackage{multicol,zref-savepos}
\def\putmarginpar#1#2{%
  \zsavepos{#1}%
  \ifnum20432158>\number\zposx{#1}%
     \hbox to 0pt{\hskip\dimexpr-\zposx{#1}sp +0.5cm \relax#2}%
  \else
      \hbox to 0pt{\hskip4cm #2}%
  \fi%
}
\parskip0pt
\begin{document}
\begin{multicols}{2}

Testing \footnotemark\putmarginpar{a:1}{First} of something that is very long and if  it need be can be set somewhere\footnotemark \putmarginpar{a:2}{Second}, where it can be subsequently found or lost\footnotemark \putmarginpar{a:3}{Third.}

\zsavepos{dd}\zposx{dd}
\end{multicols}

\end{document}

You might need to run the example more than once for the position to settle. This works both for pdfLaTeX as XeLaTeX.

enter image description here

The MWE is dependent on the actual geometry used, but one can automate the calculations. Also the boxes have been kept simple for clarity. Substituting:

\hbox to 0pt{\hskip4.2cm {\vtop to 0pt{\parindent0pt\hsize 2cm#2}}}

will enable paragraphs to be inserted.