8

It is apparently possible to put a TikZ drawing inside a pdfcomment (but using mbox).

Is is possible to associate only part of a drawing (e.g. a line) to a pdfcomment? Below it is the suggested code.

I am open to use other interactive packages (similar to pdfcomment) if that can solve the problem. The final goal is to associate individual curves in a pgfplot to a tooltip when dragging the mouse.

\documentclass[]{article}
\usepackage{tikz}
\usepackage[]{pdfcomment}
\begin{document}

%ok
\pdfmarkupcomment[]{
    text
}{
    a paragraph
}

% ok but needs mbox
\pdfmarkupcomment[]{
    \mbox{
    \begin{tikzpicture}
    \draw(0,0)--(1,1);
    \draw(0,1)--(1,0);
    \end{tikzpicture}}
}{
    a drawing
}

%not ok (Error: Missing } inserted.)
\begin{tikzpicture}
    \pdfmarkupcomment[]{
        \draw(0,0)--(1,1);
    }{
        a line
    }
    \draw(0,1)--(1,0);
\end{tikzpicture}

\end{document}
3

This question can be solved in several parts:

In what extend can pdf comments be part of TikZ pictures?

As long as the comment is put inside a node{}, there is nothing impossible. In fact, you can put almost everything inside a node{}. For example:

\tikz[remember picture,id=right triangle]{
    \draw(0,0)node[below]{\pdfmarkupcomment[color=yellow]{$A$}{This is (0, 0)}}
       --(6,0)node[right]{\pdfmarkupcomment[color=yellow]{$B$}{This is (6, 0)}}
       --(0,3)node[above]{\pdfmarkupcomment[color=yellow]{$C$}{This is (0, 3)}};
       --cycle
}

How to make a comment associated to a line/polyline/polygon?

The provide \pdflinecomment so that you can, according to the manual, typeset a comment in form of a line, polyline or polygon at the wished spot of the page.

For example:

\pdflinecomment[type=polyline,opacity=1,color=green,line={
    200 500 260 500 200 530
}]{This is a triangle comment!}

How to position this comment

From the previous example we see that a line comment is defined by x- and y- coordinates, in bp, relative to the lower left corner of this page. But how do we generate these numbers automatically so that the comment coincides with the triangle in TikZ pucture?

A possibility is that TikZ has an option called remember picture and it will store the position of the TikZ picture. By using this imformation we have a chance to calculate the coordinates we need.

But instead of doing boring maths, we can fire \pgfsys@markposition multiple times so that every position we need are stored. And again, there is nothing impossible if you put them in node{}'s:

\def\markposition#1{
    \pgfsys@markposition{#1}
}
\tikz{
    \draw(0,0)node{\markposition{point A}}
       --(6,0)node{\markposition{point B}}
       --(0,3)node{\markposition{point C}}
       --cycle;
}

And then we can retrieve those coordinate by \pgfsys@getposition:

\def\getposition#1#2#3{
    \pgfsys@getposition{#1}\pointposition\pointposition
    \pgf@sys@bp@correct\pgf@x\xdef#2{\pgf@sys@tonumber\pgf@x}
    \pgf@sys@bp@correct\pgf@y\xdef#3{\pgf@sys@tonumber\pgf@y}
}
\getposition{point A}\Ax\Ay
\getposition{point B}\Bx\By
\getposition{point C}\Cx\Cy

Now \Ax is the x-coordinate of A, and so on. So afterwards we can these coordinates in \pdflinecomment as follows:

\let~\space
\pdflinecomment[type=polyline,opacity=1,color=blue,line={
    \Ax~\Ay~\Bx~\By~\Cx~\Cy
}]{This is a triangle comment!}

Can I get rid of the ugly blue drawing?

Change the opacity:

Full code

\documentclass{article}
\usepackage{tikz,pdfcomment,lipsum}
\begin{document}

\lipsum[1]

\tikz[remember picture,id=right triangle]{
    \draw(0,0)node[below]{\pdfmarkupcomment[color=yellow]{$A$}{This is (0, 0)}}
       --(6,0)node[right]{\pdfmarkupcomment[color=yellow]{$B$}{This is (6, 0)}}
       --(0,3)node[above]{\pdfmarkupcomment[color=yellow]{$C$}{This is (0, 3)}}
       --cycle;
}

\lipsum[2]

\clearpage

\lipsum[3]

\pdflinecomment[type=polyline,opacity=1,color=green,line={
    200 500 260 500 200 530
}]{This is a triangle comment!}

\lipsum[4]

\clearpage

\lipsum[5]

\makeatletter

\def\markposition#1{
    \pgfsys@markposition{#1}
}
\tikz{
    \draw(0,0)node{\markposition{point A}}
       --(6,0)node{\markposition{point B}}
       --(0,3)node{\markposition{point C}}
       --cycle;
}
\def\getposition#1#2#3{
    \pgfsys@getposition{#1}\pointposition\pointposition
    \pgf@sys@bp@correct\pgf@x\xdef#2{\pgf@sys@tonumber\pgf@x}
    \pgf@sys@bp@correct\pgf@y\xdef#3{\pgf@sys@tonumber\pgf@y}
}
\getposition{point A}\Ax\Ay
\getposition{point B}\Bx\By
\getposition{point C}\Cx\Cy
\let~\space
\pdflinecomment[type=polyline,opacity=.5,color=blue,line={
    \Ax~\Ay~\Bx~\By~\Cx~\Cy
}]{This is a triangle comment!}

\lipsum[6]

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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