1

I have a pgfshape declared as

\pgfdeclareshape{Curve1}{
\inheritsavedanchors[from=coordinate]
\inheritanchor[from=coordinate]{center}
\backgroundpath{
\begin{pgfscope}
\pgftransformshift{\centerpoint}
\pgfmathdivide{\pgfkeysvalueof{/pgf/minimum width}}{1pt}
\pgftransformscale{\pgfmathresult}
\pgfpathmoveto{\pgfpoint{                     0}{                    0}}
[...]
\pgfpathlineto{\pgfpoint{-1.637140473757006e-01}{9.752876882003444e-01}}
[...]
\pgfpathlineto{\pgfpoint{                     0}{                    0}}
\pgfpathclose
\end{pgfscope}}}

where the [...] are ellipsis to indicate several points I've omitted for brevity.

Yet, when I plot it with

\node [Curve1, draw, scale = 10] at (0, 0) {};

the final location has nothing to do with the 10 * (-1.637140473757006e-01, 9.752876882003444e-01) that I was expecting:

enter image description here

MWE:

\documentclass[english]{scrbook}

\usepackage{tikz}

\pgfdeclareshape{Curve1}{
\inheritsavedanchors[from=coordinate]
\inheritanchor[from=coordinate]{center}
\backgroundpath{
\begin{pgfscope}
\pgftransformshift{\centerpoint}
\pgfmathdivide{\pgfkeysvalueof{/pgf/minimum width}}{1pt}
\pgftransformscale{\pgfmathresult}
\pgfpathmoveto{\pgfpoint{                     0}{                    0}}
\pgfpathlineto{\pgfpoint{-1.637140473757006e-01}{9.752876882003444e-01}}
\pgfpathlineto{\pgfpoint{                     0}{                    0}}
\pgfpathclose
\end{pgfscope}}}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\draw[step=1cm,gray,very thin] (-2,0) grid (1,10);
\node [Curve1, draw, scale = 10, thick] at (0, 0) {};
\fill (-1.637140473757006, 9.752876882003444) circle[radius=2pt];
\end{tikzpicture}
\end{figure}
\end{document}

What is the relationship between the coordinates given in the shape declaration, the scale, and the coordinates in the final picture? Is there a formula I can use somewhere?

Federico
  • 873
  • 1
  • 7
  • 21

1 Answers1

2

You need to remember that commands like \pgfpoint add the units pt if you do not supply them with units. I added a red circle to show that your approach works in principle.

\documentclass[english]{scrbook}

\usepackage{tikz}

\pgfdeclareshape{Curve1}{
\inheritsavedanchors[from=coordinate]
\inheritanchor[from=coordinate]{center}
\backgroundpath{
\begin{pgfscope}
\pgftransformshift{\centerpoint}
\pgfmathdivide{\pgfkeysvalueof{/pgf/minimum width}}{1pt}
\pgftransformscale{\pgfmathresult}
\pgfpathmoveto{\pgfpoint{                     0}{                    0}}
\pgfpathlineto{\pgfpoint{-1.637140473757006e-01}{9.752876882003444e-01}}
\pgfpathlineto{\pgfpoint{                     0}{                    0}}
\pgfpathclose
\end{pgfscope}}}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\draw[step=1cm,gray,very thin] (-2,0) grid (1,10);
\node [Curve1, draw, scale = 10, thick] at (0, 0) {};
\fill (-1.637140473757006, 9.752876882003444) circle[radius=2pt];
\fill[red] (-1.637140473757006pt, 9.752876882003444pt) circle[radius=1pt];
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

You can use cm in the declaration of the shape.

\documentclass[english]{scrbook}

\usepackage{tikz}

\pgfdeclareshape{Curve1}{
\inheritsavedanchors[from=coordinate]
\inheritanchor[from=coordinate]{center}
\backgroundpath{
\begin{pgfscope}
\pgftransformshift{\centerpoint}
\pgfmathdivide{\pgfkeysvalueof{/pgf/minimum width}}{1pt}
\pgftransformscale{\pgfmathresult}
\pgfpathmoveto{\pgfpoint{                     0}{                    0}}
\pgfpathlineto{\pgfpoint{-1.637140473757006e-01*1cm}{9.752876882003444e-01*1cm}}
\pgfpathlineto{\pgfpoint{                     0}{                    0}}
\pgfpathclose
\end{pgfscope}}}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\draw[step=1cm,gray,very thin] (-2,0) grid (1,10);
\node [Curve1, draw, scale = 10, thick] at (0, 0) {};
\fill (-1.637140473757006, 9.752876882003444) circle[radius=2pt];
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

  • thank you. I do not follow why somewhere units are taken as cm and elsewhere as pt, but I will adapt to that. – Federico Sep 24 '19 at 14:06
  • 1
    @Federico They are actually not really taken cm anywhere. If you say \path (1,2) coordinate(X);, then (1,2) is interpreted as 1 * unit x vector + 2 * unit y vector*. The default unit vectors happen to be(1cm,0)and(0,1cm), respectively, such that you get(1cm,2cm), which you would also have gotten if you multiply 1 and 2 by 1cm. However, if you sayx={(1cm,-0.5cm)}`, the result will change. These things are nicely explained in https://tex.stackexchange.com/a/31606. –  Sep 24 '19 at 14:11