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.

I've got an idea and I'm wondering if it is possible to implement in TeX/LaTeX.

The idea is I want to be able to associate arbitrary chunks of text on the page with a quote in the margin, and denote the association with a border around the chunk of text and a line connecting the chunk of text to the quote.

Here is a picture of what I'm talking about:

page

Can this be done using TeX/LaTeX? if so, how?

share|improve this question
1  
Not sure I'll have time soon try to tackle this until later, but how would you want this link to the text displayed if the entire quote fit on one line and so did not cross a line boundary, nor reach the margin of the page? –  Peter Grill Mar 28 '12 at 16:33
    
I was thinking either 1) just direct line from the margin to the text, and then I would make the color of the line very light so as not to interfere with the rest of the text; or 2) straight up until the line in question is reached, and then straight over (although now that I think about it #2 would have issues if there were more than one quote in the margin, so #1 is probably the way I would go) –  Whisker Mar 28 '12 at 16:40
3  
For a temporary solution you could always look into the Todonotes package www.tex.ac.uk/CTAN/macros/latex/contrib/todonotes/todonotes.pdf =) –  N3buchadnezzar Mar 28 '12 at 16:46
    
    
+1 for todonotes, as you don't seem to be looking a publishing-quality design, but rather to put review notes on some paper co-authored with someone else. todonotes will get you there in 30s. –  Xavier Apr 17 '12 at 23:30

2 Answers 2

up vote 28 down vote accepted
+100

Here'a an initial version using the soul, marginnote and tikz packages (and, as it has become customary, the ubiquitous \tikzmark):

\documentclass{article}
\usepackage{soul}
\usepackage{marginnote}
\usepackage{tikz}
\usetikzlibrary{calc}

\definecolor{HLcolor}{RGB}{124,18,18}
\sethlcolor{HLcolor!20}

\makeatletter
\newdimen\SOUL@dimen %new
\def\SOUL@ulunderline#1{{%
    \setbox\z@\hbox{#1}%
    \SOUL@dimen=\wd\z@ %new
    \dimen@i=\SOUL@uloverlap
    \advance\SOUL@dimen2\dimen@i %\dimen@ exchanged too
    \rlap{%
        \null
        \kern-\dimen@i
        \SOUL@ulcolor{\SOUL@ulleaders\hskip\SOUL@dimen}% new
    }%
    \unhcopy\z@
}}
\makeatother

\newcommand\tikzmark[1]{\tikz[overlay,remember picture] \node (#1) {};}

\newcommand\MarkText[3][1cm]{%
  \marginnote{\tikzmark{e}\color{HLcolor}\itshape#3}[#1]\tikzmark{s}\hl{#2}
  \begin{tikzpicture}[remember picture, overlay]
  \draw[HLcolor] let \p1 = (s), \p2 = (e) in ($(\x2,\y1)-(\marginparsep,0)$) -- (e);
  \end{tikzpicture}
  }

\begin{document}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
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. 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 eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Cur-
abitur auctor semper nulla.}{I really don't understand what you are talking about} 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. \MarkText[-\baselineskip]{Cum sociis natoque penatibus et
magnis dis parturient montes, nascetur ridiculus mus.}{I still don't understand} Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris.

\end{document}

enter image description here

This approach uses Ulrike Fischer's code of her answer to soul: broken highlighting with xcolor when using \selectcolormodel

Here's now a new version producing the border; this version contains a variation of the great code by Antal S-Z in his answer to Cool Text Highlighting in LaTeX. The main command \MarkText has the following syntax:

\MarkText[<voffset for marginnote>][<tikz options>]{<text>}{<quote>}

It was defined using the xparse package. As can be seen in the image below, some work has still to be done when the highlighted text fits on one line.

\documentclass{article}
\usepackage{xparse}
\usepackage{marginnote}
\usepackage{soul}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathmorphing}

% The following code contains a variation of the great code by Antal S-Z
% in his answer to http://tex.stackexchange.com/a/6029/3954
%in TeX.SX

\newcommand\tikzmark[1]{\tikz[overlay,remember picture] \node (#1) {};}

\newlength\LineWidth
\setlength\LineWidth{1.1pt}

\definecolor{HLcolor}{RGB}{124,18,18}
\sethlcolor{HLcolor!20}

\makeatletter
\newcommand{\defhighlighter}[3][]{%
  \tikzset{every highlighter/.style={color=#2, fill opacity=#3, #1}}%
}

\defhighlighter{HLcolor!20}{.5}

\newcommand{\highlight@DoHighlight}{
  \fill [outer sep = -15pt, inner sep = 0pt, decorate
        , every highlighter, this highlighter,draw=none]
        ($(begin highlight)+(0,8pt)$) rectangle ($(end highlight)+(0,-2pt)$) ;
  \draw[HLcolor,line width=\LineWidth]  ($(begin highlight)+(0,-2pt)$) -- ($(end highlight)+(0,-2pt)$) ;
  \draw[HLcolor,line width=\LineWidth]  ($(begin highlight)+(0,8pt)$) -- ($(end highlight)+(0,8pt)$) ;
}

\newcommand{\highlight@BeginHighlight}{
  \coordinate (begin highlight) at (0,0) ;
}

\newcommand{\highlight@EndHighlight}{
  \coordinate (end highlight) at (0,0) ;
}

\newdimen\highlight@previous
\newdimen\highlight@current

\DeclareRobustCommand*\highlight[1][]{%
  \tikzset{this highlighter/.style={#1}}%
  \SOUL@setup
  %
  \def\SOUL@preamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@BeginHighlight
      \draw[HLcolor,line width=\LineWidth]  ($(begin highlight)+(0,-2pt)+(0,-0.5\pgflinewidth)$) -- ($(begin highlight)+(0,8pt)+(0,0.5\pgflinewidth)$) ;
      \highlight@EndHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@postamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@EndHighlight
      \highlight@DoHighlight
      \draw[HLcolor,line width=\LineWidth]  ($(end highlight)+(0,-2pt)+(0,-0.5\pgflinewidth)$) -- ($(end highlight)+(0,8pt)+(0,0.5\pgflinewidth)$) ;
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@everyhyphen{%
    \discretionary{%
      \SOUL@setkern\SOUL@hyphkern
      \SOUL@sethyphenchar
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everyexhyphen##1{%
    \SOUL@setkern\SOUL@hyphkern
    \hbox{##1}%
    \discretionary{%
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everysyllable{%
    \begin{tikzpicture}[overlay, remember picture]
      \path let \p0 = (begin highlight), \p1 = (0,0) in \pgfextra
        \global\highlight@previous=\y0
        \global\highlight@current =\y1
      \endpgfextra (0,0) ;
      \ifdim\highlight@current < \highlight@previous
        \highlight@DoHighlight
        \highlight@BeginHighlight
      \fi
    \end{tikzpicture}%
    \the\SOUL@syllable
    \tikz[overlay, remember picture] \highlight@EndHighlight ;%
  }%
  \SOUL@
}
\makeatother

\DeclareDocumentCommand\MarkText{O{0.7cm}O{draw}mm}{%
  \marginnote{\tikzmark{endquote}\color{HLcolor}\small\itshape#4}[#1]%
  \tikzmark{beginquote}\highlight[#2]{#3}%
  \begin{tikzpicture}[remember picture, overlay]
  \draw[HLcolor] let \p1 = (beginquote), \p2 = (endquote) 
    in ($(\x2,\y1)+(-\marginparsep,0)+(0,0.5ex)$) -- ($(endquote)+(-1pt,0.5ex)$);
  \end{tikzpicture}
}

\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. \MarkText{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.}{I really don't understand what you are talking about} 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. Praesent eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Cur-
abitur auctor semper nulla. \MarkText[0cm]{Donec varius orci}{Something is wrong} 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. \MarkText[-0.7cm][fill=olive!60]{Cum sociis natoque penatibus et
magnis dis parturient montes, nascetur ridiculus mus.}{I still don't understand} Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris.

\end{document}

enter image description here

P.S.: I'll be improving this answer when I have time.

share|improve this answer
    
+1, Only thing remaining is the border for highlighted text. You are very close. –  Harish Kumar Apr 18 '12 at 0:25
    
absolutely - very impressed with this answer. –  Whisker Apr 18 '12 at 1:15
2  
@HarishKumar: thank you. I've added a new version; only one case remains to solve: as soon as I can I'll provide some code for this case. –  Gonzalo Medina Apr 18 '12 at 1:20
    
@Jordan: thank you. One question; how should the line connecting the highlighted text and the quote be in a case like the second one (the text is small and fits in one line) in my updated version? –  Gonzalo Medina Apr 18 '12 at 1:22
    
@GonzaloMedina: I think all these (including cool highlihting things discussed in the link by you) should be wrapped up in to a package so that we can use it like todonotes (another version). Can you please do that? For the second one in your updated version (in above comment of yours) can you make it like a horizontal line from north west corner of the block to the margin note? –  Harish Kumar Apr 18 '12 at 2:21

Not exactly what you showed in picture. But on the fly this is what I can produce using todonotes. Putting the text inside body within a block is still to be achieved. For the time being I made it differently colored.

\documentclass{article}
\usepackage{todonotes}
%-------------------------------------------------
\begin{document}
This is where \todo{This is highlighted in margin}\textcolor{green}{highlight and show some marginal comments so as to attract the viewer's attention}
%-------------------------------------------------
\end{document}

enter image description here

share|improve this answer

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.