# How to correctly change the side that marginpar appears per case?

I'm trying to use marginpar and todonotes to make comments in a latex file as to ease the collaboration. The margins are just quick todo notes and fixes and I don't want to hardcode the position of every margin note since they'll be temporary. So, I'm trying to create small scripts in the preamble and use them as I go.

My problem is with long margin texts that multiples of them in one paragraph/page will either overlap or get too far away from the actual text. Naturally, I I thought I should be able to change the margin to appear on the left or right side of page per case that I have. That is, I want to be able to define to commands, let's say \lmargin and rmargin so that all the margin notes with \lmargin appear on one side and all the ones with \rmargin to appear on the other side. I don't really care about which one is on the left and which one is on the right, as long as they are on opposite sides. Or ideally, it would be great if latex could keep track of the margin notes and alternate between left and right automatically.

Searching through forums, the only solutions change the side of the margins for all the document. The closest solution I could find is to use \reversemarginpar. It works as long as the margin notes are in different paragraphs, but I don't want to break the paragraphs just for each note that I add to text.

Here is a MnonWE for the marginpar, the todonotes package is similar:

\documentclass[]{article}

\usepackage{pgf}
\usepackage[ %
%top=Bcm,
%bottom=Hcm,
%outer=Ccm,
%inner=Acm,
%heightrounded,
marginparwidth=2.5cm,
%marginparsep=Ecm
]{geometry}
\usepackage{lipsum}
\usepackage{soul} % for highlighting the text: \hl
\usepackage{setspace} % for setting the linespace for a portion of the text: \setstretch

\definecolor{dkblue}{rgb}{0.0, 0.0, 0.6}
\definecolor{ltblue}{rgb}{0.6, 0.6, 1.0}

\usepackage{color}

\newcommand{\lmargin}[2]{\sethlcolor{ltblue}\hl{#1}\marginpar{\setstretch{0.5} {\color{dkblue} \tiny note: #2}}}

\begin{document}
And even there \lmargin{are}{there's something here that I need to pay attention to, and I need to be very clear about it, so I am going to write everything that comes to my mind here in the margin so that it doesn't take anyone 400 years to come up with the same thing!}  more things that can be said, but I am \reversemarginpar\lmargin{not sure}{Same thing here: there's something here that I need to pay attention to, and I need to be very clear about it, so I am going to write everything that comes to my mind here in the margin so that it doesn't take anyone 400 years to come up with the same thing!}  exactly what. \lipsum[7]

Do you see the problem \reversemarginpar\lmargin{here}{And this is what I need}? If I don't go to a new paragraph, everything changes altogether! and overlaps even when I do!

\end{document}


And this is how it looks like:

While I was looking at my old paracol solution I started thinking I could do it better. In particular, I should be able to handle more than one note per paragraph.

The advantage of this solution is that it will never overlap and will break across pages. Because the notes are being written to the AUX file, I put most of the formatting into \marginparformat. Also, it takes two runs.

The main problem in implementing this is that you can only switch columns between paragraphs, which can be solved using \everypar. Even then, you need to know at the beginning of the paragraph what will be coming up later. The solution is to write the needed information to the AUX file. When you switch back to the original column, it immediately kicks off another \everypar which you have to ignore or get trapped in a loop.

It should be noted that \everypar tends not to be preserved. I rewrote \@afterheading, but there are bound to be others.

\documentclass{article}
\usepackage[ %
%top=Bcm,
%bottom=Hcm,
%outer=Ccm,
%inner=Acm,
%heightrounded,
textwidth={\dimexpr 5in+5cm+2\columnsep},
marginparwidth=0pt,
marginparsep=0pt
]{geometry}
\usepackage[nopar]{lipsum}
\usepackage{soul} % for highlighting the text: \hl
\usepackage{setspace} % for setting the linespace for a portion of the text: \setstretch

\usepackage{color}
\definecolor{dkblue}{rgb}{0.0, 0.0, 0.6}
\definecolor{ltblue}{rgb}{0.6, 0.6, 1.0}

\usepackage{paracol}
\setcolumnwidth{2.5cm, 5in, 2.5cm}

\newcommand{\marginparformat}% only change between paragraphs
{\tiny% affects em and ex
\parindent=2em
\parskip=1ex
\sloppy
\setstretch{0.5}%
\color{dkblue}%
\noindent note: }

\newcounter{absparagraph}
\newcounter{leftmarginpar}
\newcounter{rightmarginpar}
\globalcounter{absparagraph}
\globalcounter{leftmarginpar}
\globalcounter{rightmarginpar}

\makeatletter
\newcommand{\leftmarginpar}[1]% #1=text
{\stepcounter{leftmarginpar}%
\pdfsavepos
\protected@write\@auxout{}{\string\newleftmarginpar{\theleftmarginpar}{\theabsparagraph}%
{\thepage}{\noexpand\number\pdflastypos}{#1}}}%

\newcommand{\rightmarginpar}[1]% #1=text
{\stepcounter{rightmarginpar}%
\pdfsavepos
\protected@write\@auxout{}{\string\newrightmarginpar{\therightmarginpar}{\theabsparagraph}%
{\thepage}{\noexpand\number\pdflastypos}{#1}}}%

%begin gory details
\newcommand{\current@page}{}% reserve global name

\newif\ifrepeatpar

\newlength{\leftmarginpar@y}
\newlength{\rightmarginpar@y}
\setlength{\leftmarginpar@y}{\pagetop@y}
\setlength{\rightmarginpar@y}{\pagetop@y}

\coordinate (sw1) at (current page.south west);
\coordinate (nw1) at (current page.north west);
\coordinate (leftmargin) at (intersection of lm--leftbottom and sw1--nw1);
\node[right,align=flush left ,text width=2cm, fill=green!20] at (leftmargin)  {\color{gray!70}\footnotesize\bfseries{#2}};
\end{tikzpicture}
}
\begin{document}
\pagestyle{empty}
\tikzstyle{every picture}+=[remember picture]

\tikzrmargin{Remark in the right margin!}{This is the Remark.} bla bla bla bla bla gjqp bla bla bla bla blabla bla bla
bla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla bla bla bla bla bla blabla bla bla bla bla blabla bla bla \tikzlmargin{Remark in the left margin!}{This is the Remark.}
bla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla bla bla blabla bla bla bla bla blabla bla bla
bla bla blabla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla bla bla blabla bla bla bla bla blabla bla bla
bla bla blabla bla blabla bla \tikzrmargin{ 2nd remark in the right margin!}{This is the 2\textsuperscript{nd} remark}blabla bla blabla bla blabla bla blabla bla bla bla bla blabla bla bla bla bla blabla bla bla
bla bla blabla bla blabla bla blabla bla blabla bla blabla bla bla

This should be put inside a suitable macro! \tikzlmargin{On the left}{again!}

\end{document}

• This looks very cool. It seems like all the answers come down to tikz at some point. I might adopt this. Thanks for the workaround. Just one question, do you know if it's possible to make the numbering automatic? – Keivan Feb 5 '18 at 4:50
• And, about the first link. I've been looking at the todonotes, but as far as I figured out it behaves the same as marginpar regarding which side it appears. – Keivan Feb 5 '18 at 4:52
• @Keivan: Automatic numbering is obsolete now with the new version. Is this right? – Jens Schwaiger Feb 5 '18 at 10:56
• Yup. That's right – Keivan Feb 6 '18 at 20:01