Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Let me give you some background on my problem.

I am working on application which generates and compiles LaTeX code.

For debugging purposes I need it to add some additional informations next to the standard document content. In the perfect case these informations should not interrupt natural flow of the text, apper near the content (may be at the margin), be printable and require only one compilation.

I need these debug notes for math, inline math, sections, tables, sometimes inside multicols environment.

I have tried marginpar, several packages like todonotes, marginnote, boxes with zero height and overlap or even printing pdf comments but I can't achieve my goal.

So far marginpars seem to be the best, but they do not appear exactly where I would like and sometimes (in example before sections) create additional vertical gap. They also ruin multicols.

Behavior of the package lineno, which adds line number before every line seems very nice but I have no idea how to adapt it.

Do you have any clues?

share|improve this question
    
I've used todonotes a couple of times and I would have thought this achieves your goal. Which of your criteria does it not achieve? –  John Wickerson May 11 '13 at 15:23
    
Welcome to tex.sx. Why the one compilation requirement? It is remarkably difficult to add any kind of annotation without disturbing the flow. If you restrict the places where the annotation will be added to places where characters occur it is easier (this seems to be OK according to your list) but the most robust way these days would probably involve \pdfsavepos (or equivalently \tikzmark ) But that requires two passes. Almost all latex documents require more than one pass anyway so that isn't normally a problem? –  David Carlisle May 11 '13 at 15:23
    
@John Wickerson I've got quite narrow margins in my documents (like 20-25mm) so marginpar is not enough for todonotes. Also the same problem as for marginpar when placed before section - unwanted vertical space. @David Carlisle I just realized that single compilation isn't important at all, as it takes place only in debugging environment. I'll definetely take a look at \pdfsavepos. –  Paul May 11 '13 at 15:39
add comment

1 Answer

up vote 3 down vote accepted

Here's one possible solution using \tikzmark. The code needs three runs to stabilize, but the original constraint about the number of runs has been lifted in the comments to the question, so this is not a problem.

the \MarkText command has two mandatory arguments: the first one, will be used for the material that will be marked; the second argument is the actual contents of the note associated to the marker; an optional argument allows to vertically shift the contents of the annotation, in case it lies too close to another one.

Since the annotation text is placed inside a \node, you can adjust the node attributes according to your needs.

The example below shows the command in action for several of the requested cases: multicolumn text, in-line math, displayed math, regular text and a sectional unit title:

\documentclass{article}
\usepackage{ragged2e}
\usepackage{amsmath}
\usepackage[a5paper,marginparwidth=15mm]{geometry}
\usepackage{multicol}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}

\definecolor{HLcolor}{RGB}{124,18,18}
\newcommand\tikzmark[1]{%
  \tikz[overlay,remember picture,baseline=-0.3ex] \coordinate (#1);}

\newcommand\MarkText[3][0pt]{%
  {\tikzmark{e}#2
  \begin{tikzpicture}[remember picture, overlay]
  \path let \p1 = (e), \p2 = (current page marginpar area.west) 
  in node[yshift=-#1,text width=\marginparwidth,align=left,anchor=north west,font=\normalfont\small\color{HLcolor},inner ysep=0pt] at (\x2,\y1) {\tikzmark{s}\RaggedRight#3};
  \draw[HLcolor] let \p1 = (e), \p2 = (s) in (e) |- ([xshift=-5pt,yshift=0.5ex]s.west);
  \end{tikzpicture}%
  }%
}

\begin{document}

\section{A \MarkText{Test}{This title must be changed} Section}
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. 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. \MarkText{Praesent}{I really don't understand what you are talking about} eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Cur-
abitur 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.
\begin{align}
a+b+c &= d+e+f \\
&\MarkText{=}{Should this really be an equality?} g +h \\
&= i.
\end{align}
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. \MarkText{Cum}{I still don't understand} sociis natoque penatibus et
magnis dis parturient montes, nascetur ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris.
\begin{multicols}{2}
Erat ligula aliquet magna,
vitae ornare odio metus a mi. Morbi ac orci et nisl \MarkText[-20pt]{hendrerit}{Something is wrong here} mollis. Donec aliquet, tortor sed accumsan bibendum. Suspendisse
ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque penatibus et
magnis dis parturient montes, nascetur ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Nulla ullamcorper $f(x)\MarkText{=}{This requires special attention}f(-x)$ vestibulum turpis. Pellentesque cursus luctus mauris.
\end{multicols}

\end{document}

enter image description here

share|improve this answer
    
Would it be possible to auto adjust label vertical position in case there is another annotation in the same line? Or even skip additional notes detected in a line which already has one. Btw - I'm glad that your code works for tabular aswell. –  Paul May 12 '13 at 22:42
    
@Paul I guess it might be possible; however, I don't have the time to do it; that's why I provided the optional argument for manual adjustment. –  Gonzalo Medina May 12 '13 at 22:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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