Marginpar – How to Put Real Notes in the Margin

marginpar

I'm looking to write a fictional description/discussion by a 'scholar' of an alien race for an RPG, but I can see this being handy elsewhere.

I'd like to put his 'notes' in the margins of some text he's found/is writing.

I've seen what \marginpar{margin text} can do:

enter image description here

but I'm more after this kind of margin note:

enter image description here

How could I do that with LaTex?

Best Answer

Here is my very unsatisfactory attempt at this, but perhaps someone can use this to come up with a real solution. This just requires way too much tweaking of the paramaters to control the location of the comment text.

enter image description here

References:

Further Enhancements:

  • Replace font of the notes
  • More automatic placement of the comments

Code:

\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{calc,fit, decorations.pathmorphing}

\makeatletter
% https://tex.stackexchange.com/questions/39296/simulating-hand-drawn-lines
\pgfdeclaredecoration{penciline}{initial}{
    \state{initial}[width=+\pgfdecoratedinputsegmentremainingdistance,auto corner on length=1mm,]{
        \pgfpathcurveto%
        {% From
            \pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}
                            {\pgfdecorationsegmentamplitude}
        }
        {%  Control 1
        \pgfmathrand
        \pgfpointadd{\pgfqpoint{\pgfdecoratedinputsegmentremainingdistance}{0pt}}
                        {\pgfqpoint{-\pgfdecorationsegmentaspect\pgfdecoratedinputsegmentremainingdistance}%
                                        {\pgfmathresult\pgfdecorationsegmentamplitude}
                        }
        }
        {%TO 
        \pgfpointadd{\pgfpointdecoratedinputsegmentlast}{\pgfpoint{1pt}{1pt}}
        }
    }
    \state{final}{}
}
\makeatother

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

\newcommand{\CommentText}[3]{\tikzmark{#1}#3\tikzmark{#2}}

\NewDocumentCommand{\CommentPar}{%
    O{}% #1 = draw options for the referenced word
    O{}% #2 = draw options for the comment
    O{}% #3 = draw options for the connecting line
    m  % #4 = left \tikzmark name
    m  % #5 = left \tikzmark name
    m  % #6 = comment
    }{%
    \begin{tikzpicture}[overlay,remember picture,decoration=penciline, thick]
        \node [shape=rectangle,inner sep=0, draw=blue, ,rounded corners=2pt, fit={(#4.south) ($(#5.north)+(0,0.75ex)$)}, decorate, #1] (Source) {};
        \node at ($(#4)!0.5!(#5)$) [blue, font=\itshape, rounded corners=5pt, decorate, #2] (Label) {#6};
        \draw [draw=red, decorate, #3] (Label) to (Source);
    \end{tikzpicture}
}


\begin{document}
Lorem ipsum dolor sit amet, \CommentText{ConsecA}{ConsecB}{consectetur} adipiscing elit. Fusce mollis ullamcorper augue. Cras ut bibendum est. Nullam mauris lectus, imperdiet eget interdum quis, vestibulum quis orci. 
\CommentPar[draw=brown][draw=red, shift={(1.0cm,2.0cm)}, rotate=40, text width=2cm, align=center][out=-110, in=10, distance=0.1cm,->]{ConsecA}{ConsecB}{~~~Did you  \\ mean~~ \\ consecutive??}

\begin{quote}
Ut tellus odio, rhoncus at blandit nec, tempor malesuada nunc. In id euismod odio. Ut lobortis varius pellentesque. Proin molestie \CommentText{estA}{estB}{est} dolor, eu tristique enim. Nulla facilisi. Curabitur pharetra sem eget sem volutpat sed sollicitudin mauris interdum. Nullam iaculis leo vitae tellus mattis aliquam. \CommentText{MexicanA}{MaxicanB}{Maecenas} metus odio, mollis ac posuere vitae, pulvinar ut orci. Nullam porta, dolor vel ornare gravida, leo mauris tempor metus, eu dignissim eros neque quis justo.
\end{quote}

\CommentPar[brown][color=blue, shift={(0.75cm,0.1cm)}, rotate=-50][draw=none]{estA}{estB}{Yes!!}

\CommentPar[brown][text width=3cm, color=violet, shift={(6.7cm,-1.5cm)}, rotate=-65][brown, out=225, in=-80, distance=0.7cm, ->]{MexicanA}{MaxicanB}{Seriously that is how you spell Mexican??}

Nulla massa nisl, tempus sit amet scelerisque ac, vulputate quis ligula. Sed a neque vel enim ultrices ullamcorper. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. In hac habitasse platea dictumst. Quisque tincidunt pretium est, vitae ornare urna varius et.  
\end{document}
Related Question