[Tex/LaTex] How to place todonotes in left and right margin

marginstodonotes

I want to place todonotes not only on the left or the right margin. I want to use both at the same time, and that the notes should decide by themself where to place. E.g. when the note points to a place near the left margin it should go to the left margin. Or when the left margin is full, it should use right margin then.

There are already similar questions, but I couldn't find some focusing also on the todonotes package.

Best Answer

There is a way of achieving this by using the zref package and marginnotes. This answer is based on this answer about placing nodes on both sides and hints taken from this answer about zrefusage.

The idea is to use zref to get the horizontal position of the "anchor" of a todo note and let ifdim check, if that is on the left or right side of the page. Since all the x-coordinates are written to the aux file at least two compilations are necessary. Additionally unique labels are needed for all those coordinates so in the MWE below the count \todocount is provided giving each note a label of the kind todo<number>. The aux file contains all those todo<number> labels associated with their posx coordinate (in sp).

The command \mytodo takes an optional argument and the mandatory argument which are passed to \todo, it additionally calls \zsafeposx, \checkxpos and advances the counter.

The number (20000000) in the definition of \checkxpos is the estimated center of the page, really, its just roughly the middle and there are some warnings about taking the zref positions as absolute values on a page.

I am not sure how stable those macros are in conection with twopage documents and other stuff.

If the connecting lines and everything looks like a mess another run with pdflatexor what ever you use might fix it.

The complete MWE is:

\documentclass{scrartcl}

\usepackage{lipsum}
\usepackage[colorinlistoftodos,textsize=scriptsize]{todonotes}
\usepackage{marginnote}
\usepackage{zref-savepos}

\newcount\todocount

\newcommand{\checkxpos}[3][]{%
  \ifdim \zposx{#2}sp < 20000000sp%
    \mynote[#1]{#3}%
  \else%
    \note[#1]{#3}%
  \fi%
}

\newcommand{\mytodo}[2][]{%
  \zsaveposx{todo\the\todocount}%
  \checkxpos[#1]{todo\the\todocount}{#2}%
  \global\advance\todocount1\relax
}

\newcommand{\mynote}[2][]{{%
  \let\marginpar\marginnote
  \reversemarginpar
  \renewcommand{\baselinestretch}{0.8}%
  \todo[#1]{#2}}}

\newcommand{\note}[2][]{\renewcommand{\baselinestretch}{0.8}\todo[#1]{#2}}

\begin{document}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,\mytodo{the first note} vestibulum ut,
placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero,
nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis
egestas. Mauris ut leo.\mytodo{this is another note} Cras viverra metus rhoncus sem. Nulla et lectus vestibulum
urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer
sapien est, iaculis in, pretium quis, viverra ac, nunc. Praesent eget sem vel leo ultrices
bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac,
nulla. Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim rutrum.
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi auctor lorem
non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et, tellus. Donec
aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna, vitae ornare odio
metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse ut massa. Cras nec ante.
Pellentesque a nulla. Cum sociis natoque penatibus et magnis dis parturient montes,
nascetur ridiculus mus.\mytodo{now there would be this} Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis.
Pellentesque cursus luctus mauris.
Nulla malesuada porttitor diam.\mytodo{now this} Donec felis erat, congue non, volutpat at, tincidunt
tristique, libero.\mytodo{and dont forget that} Vivamus viverra fermentum felis. Donec nonummy pellentesque ante.
Phasellus adipiscing semper elit. Proin fermentum massa ac quam. Sed diam turpis,
molestie vitae, placerat a, molestie nec, leo. Maecenas lacinia. Nam ipsum ligula, eleifend
at, accumsan nec, suscipit a, ipsum. Morbi blandit ligula feugiat magna. Nunc eleifend
consequat lorem. Sed lacinia nulla vitae enim. Pellentesque tincidunt purus vel magna.
Integer non enim. Praesent euismod nunc eu purus. Donec bibendum quam in tellus.
Nullam cursus pulvinar lectus. Donec et mi. Nam vulputate metus eu enim. Vestibulum
pellentesque felis eu massa.

\end{document}