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.

In the following example I'm using the library plotmarks to set a text on the given path. Now I am interested in showing the current coordinates of the marker instead of p.

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{plotmarks}
\begin{document}
\begin{tikzpicture}

\draw[thick,mark=text,
%      text mark=(CURRENT COORDINATE)
      ] plot [smooth,] coordinates{
           (-0.5,0)(-0.7,-0.75) (1.3,1)
           (-1.6,0.8) (-1,0) 
            };
\end{tikzpicture}
\end{document}

Now I'm using the provided solution expanded by a precision:

\documentclass[12pt]{article}
\usepackage{tikz}

\usetikzlibrary{plotmarks}
\begin{document}
\begin{tikzpicture}
\draw[thick,mark=text,
     text mark={%
    \pgfgetlastxy{\x}{\y}%
    (\pgfmathparse{\x/28.45274}\pgfmathprintnumber[fixed,precision=0]{\pgfmathresult},% Convert to cm.
     \pgfmathparse{\y/28.45274}\pgfmathprintnumber[fixed,precision=0]{\pgfmathresult})% Convert to cm.
    }%
] plot [smooth,] coordinates{
           (-0.5,0)(-0.7,-0.75) (1.3,1)
           (-1.6,0.8) (-1,0) 
            };
\end{tikzpicture}
\end{document}

share|improve this question

3 Answers 3

up vote 4 down vote accepted

A possible "quite lucky" solution is to check if the last used coordinate is available in the pgfgetlastxy command (it is.)

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{plotmarks}
\begin{document}
\begin{tikzpicture}

\draw[thick,mark=text,
     text mark={%
    \pgfgetlastxy{\x}{\y}%
    (\pgfmathparse{\x/28.45274}\pgfmathresult cm,% Convert to cm.
     \pgfmathparse{\y/28.45274}\pgfmathresult cm)% Convert to cm.
    }%
] plot [smooth,] coordinates{
           (-0.5,0)(-0.7,-0.75) (1.3,1)
           (-1.6,0.8) (-1,0) 
            };
\end{tikzpicture}
\end{document}

enter image description here

But suffers from numerical errors for the display which can further be trimmed. But I don't have any elegant solution for that yet.

share|improve this answer

You can get the coordinates by using \pgfgetlastxy. This will give you the coordinates in points, so if you want to use them for display purposes you will need to perform some further calculations to get values that will display nicely.

You can simply use it like this:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{plotmarks}
\begin{document}
\begin{tikzpicture}

\draw[thick,mark=text,
      text mark={\pgfgetlastxy{\myx}{\myy}(\myx,\myy)}
      ] plot [smooth,] coordinates{
           (-0.5,0)(-0.7,-0.75) (1.3,1)
           (-1.6,0.8) (-1,0) 
            };
\end{tikzpicture}
\end{document}

It looks like this (very bad due to the dimensions being in points and thus very long).

Plot with displayed coordinates

share|improve this answer
    
+1 I accept the answer of percusse because he has an additional converting ;-) –  Marco Daniel Dec 30 '11 at 12:30

Here is a different approach by saving the actual input in a macro and accessing it later. As the plotsmarks are placed after the actual path and not when the point is used in the path. We need to define a few macros (for each coordinate one macro).

Still, this doesn’t show the real coordinates (e.g. not (1.5cm, 0cm) but (2-0.5,0)) but if you know the type of input you can also process it again (splitting at the ,, appending cm, …).

Code

\documentclass[tikz]{standalone}
\usepackage{etoolbox}
\usetikzlibrary{plotmarks}
\makeatletter
\newcount\tikz@plot@coordinate
\tikzset{text mark/.append code=\tikz@plot@coordinate\z@}
\pretocmd{\tikz@@scan@@no@calculator}{%
    \expandafter\def\csname tikz@plot@coordinate@\the\tikz@plot@coordinate\endcsname{#2}%
    \advance\tikz@plot@coordinate\@ne
    }{}{\errmessage{!}}
\preto\tikz@@@plot{\tikz@plot@coordinate\z@}
\pgfdeclareplotmark{text*}
{
    \pgfkeysgetvalue{/pgf/text mark style}\pgfmarktext@style
    \pgfkeysgetvalue{/pgf/text mark}\pgfmarktext@
    \expandafter\pgfutil@in@\expandafter*\expandafter{\pgfmarktext@}
    \ifpgfutil@in@
        \expandafter\def\expandafter\pgfmarktext@\expandafter{\expandafter\qrr@tikz@plot@replace\pgfmarktext@\@qrr@tikz@plot@replace
            {\csname tikz@plot@coordinate@\the\tikz@plot@coordinate\endcsname}}
    \fi
    \ifpgfmarktext@usetikznode
    \expandafter\node\expandafter[\pgfmarktext@style]{\pgfmarktext@};
    \else
    \expandafter\pgftext\expandafter[\pgfmarktext@style]{\pgfmarktext@}
    \fi
    \global\advance\tikz@plot@coordinate\@ne
}
\def\qrr@tikz@plot@replace#1*#2\@qrr@tikz@plot@replace#3{#1#3#2}
\makeatother

\begin{document}
\begin{tikzpicture}

\draw[
    thick,
    mark=text*,
    /pgf/text mark=$(*)$,
    text mark as node,
    text mark style={fill=white,fill opacity=.5,text opacity=1,inner sep=+0pt}
    ] plot [smooth] coordinates{
           (2-0.5,0) (-0.7,-0.75) (1.3,1in)
           (-1.6,0.8) (-1,0) 
            };
\end{tikzpicture}
\end{document}

Output

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.