2

I have a tikz pic that I wish to rotate and move around. If I define the pic with number coordinates, this works fine. If I define the pic with named coordinates (handy reference points I use for many of my pics), then the transforms do not apply.

enter image description here

(err sorry, the picture is rather small. I guess because the default units are in centimetres?).

In the first example, I've defined a pic named line that goes from points (A) to (B). In the example below it, I've defined a pic unnamed line that goes from points (0,0) to (1, 0) (which are A and B respectively).

I then apply a transform to the pic (xscale=-1). The named line is not transformed, while the unnamed one is.

I guess the coordinate definition of (A) and (B) is invariant to transforms? Is there a way to make them subject to the transforms? (I have quite a number of coordinates that I reuse a few times, and it would be nice to just to refer to them by labels instead of their numbers. I guess I can just \def\coordA{(0,0)} but I wanted to know if there was a Tikz solution).

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{scopes}
\tikzset{
  named line/.pic = {
    \draw (A) -- (B);
  },
  unnamed line/.pic = {
    \draw (0,0) -- (1, 0);
  }
}
\begin{document}
\begin{tikzpicture}
  \coordinate (A) at (0, 0);
  \coordinate (B) at (1, 0);

  \draw pic [red] {named line};
  \draw pic [blue,xscale=-1] {named line};

  \begin{scope}[yshift=-1cm] 
    \draw pic [red] {unnamed line};
    \draw pic [blue,xscale=-1] {unnamed line};
  \end{scope}
\end{tikzpicture}
\end{document}
3

One thing to note is that you can redefine \coordinates at will, so one thing you might do is define the following command:

\newcommand\setcoordinates{
  \coordinate (A) at (0, 0);
  \coordinate (B) at (1, 0);
}

Then you can say \setcoordinates every time you need (A) and (B) to be transformed. Tikz coordinates are not supposed to be transformed. If you really want to transform them anyways, though, the following code defines a key transform coordinates that changes the way tikz looks up the coordinates of nodes in order to accomplish this:

\documentclass{article}
\usepackage{tikz}
\makeatletter
\def\tikz@calc@anchor@modified#1.#2\tikz@stop{%
  \bgroup
  \pgftransformreset
  \pgfpointanchor{\tikz@pp@name{#1}}{#2}%
  \edef\tikz@point@smuggle{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}%
  \expandafter\egroup
  \tikz@point@smuggle
}

\tikzset{transform coordinates/.code={\let\tikz@calc@anchor\tikz@calc@anchor@modified}}
\makeatother

\begin{document}
\begin{tikzpicture}
  \coordinate (A) at (0, 0);

  \draw (A) circle [radius=10pt];

\draw[xshift=1cm,red] (A) circle [radius=10pt];
\end{tikzpicture}


\begin{tikzpicture}[transform coordinates]
  \coordinate (A) at (0, 0);

  \draw (A) circle [radius=10pt];

\draw[xshift=1cm,red] (A) circle [radius=10pt];
\end{tikzpicture}

\begin{tikzpicture}
\coordinate (A) at (0, 0);

\draw (A) circle [radius=10pt];

\draw[xshift=1cm,red,transform coordinates] (A) circle [radius=10pt];
\end{tikzpicture}
\end{document}

Output:

enter image description here

  • Ahh, I had thought it was specific to the use of pic somehow. Thanks. – mathematical.coffee Dec 9 '16 at 1:48
  • 1
    Oh right, so if you want your pic to make use of this, you should say \tikzset{named line/.pic = {\draw[transform coordinate] (A) -- (B);}. This should do exactly what you want. – Hood Chatham Dec 9 '16 at 1:54

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.