32

Why wont xshift work here?

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  \coordinate (Q) at (2.1cm, -1cm);

  \draw[xshift = 0.5cm] (Q) -- +(0, 3cm); % no shift

  \begin{scope}[xshift = 0.5cm]
    \draw (Q) -- +(0, 3cm); % no shift
  \end{scope}
\end{tikzpicture}
\end{document} 

Both the scoped and non-scoped version produce the same image:

enter image description here

As we can see, the line is drawn from Q and not shifted.

dustin
  • 18,617
  • 23
  • 99
  • 204

3 Answers3

29

As explained in another answer, named nodes are somewhat "absolute" coordinates, and they are not affected by standard transforms (shift, rotate, scale, etc.) They can be transformed via a transform canvas, however, but this is generally discouraged, specially for scale changes, because it affects also to the size and aspect of strokes, fonts, etc.

So, leaving shifts alone, which alternatives do we have?

  1. Use calc to manually add some amount to each coordinate, e.g: ($(Q)+(0.5, 0)$)
  2. Use ++ syntax to set a new "origin" and + syntax to specify coordinates relative to that origin.

Using the second approach for this particular case:

\draw (Q) ++(0.5, 0) -- +(0,3);

which means:

  1. Go to coordinate (Q)
  2. Move (0.5,0) from that coordinate and set this new point as origin for relative coordinates
  3. Draw a line from the last point to the one which is at (0,3) from it.
JLDiaz
  • 55,732
  • How can I move later on (not only at start?) \draw (Q) ++(0.5, 0) -- (P) +(0,3); to say "make line to P + (0,3)" doesn't seem to work. – Johannes Schaub - litb Jan 09 '18 at 11:01
  • @JohannesSchaub-litb For this case the first option is the way to go, i.e. \usetikzlibrary{calc}and the syntax: \draw ($(Q)+(0.5,0)$) -- ($(P)+(0,3)$); – JLDiaz Jan 09 '18 at 11:21
25

AFAIK named coordinates survive a transformation unchanged unless the canvas is transformed:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \coordinate (Q) at (2.1cm, -1cm);
  \draw (Q) -- +(0, 3cm);

  \draw[transform canvas={xshift = 0.5cm}] (Q) -- +(0, 3cm);

  \begin{scope}[transform canvas={xshift = 1cm}]
    \draw (Q) -- +(0, 3cm);
  \end{scope}
\end{tikzpicture}
\end{document}

Result

Heiko Oberdiek
  • 271,626
6

You can shift named coordinates if you apply shift to each coordinate, not to the whole scope:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  \coordinate (Q) at (2.1cm, -1cm);

  \draw[xshift = 0.5cm] (Q) -- +(0, 3cm) node[midway, above,sloped] {no shift};

  \draw[red] ([xshift = 0.5cm]Q) -- +(0, 3cm) node[midway, above,sloped] {shifted}; 

\end{tikzpicture}
\end{document} 

enter image description here

Ignasi
  • 136,588
  • Putting ([xshift=0.5] Q) definetly does not work with Tikz 3.0. It was the first (logical) thing I tried. It is according to the syntax to shift a single coordinate. – Johannes Linkels Jul 06 '18 at 15:29
  • @jlinkels In this case Q is a coordinate and ([xshift=0.5]Q) works perfectly. If Q is a node you need to use ([xshift=0.5]Q.center). – Ignasi Jul 06 '18 at 17:40
  • @jlinkels If you downvoted for this (yours) mistake, I think you could reconsider your vote. In any case, I think it's more polite to claim my attention with a comment and wait some time for an answer before downvote. – Ignasi Jul 06 '18 at 17:44
  • Yes, you are right. Both on syntax and commenting first before downvoting. My apologies. It works on a coordinate like you say. It does NOT work on this: \draw ([xshift=0.5] A1.left) but it does on ([xshift=0.5] A1.center). Which is nice nut useless, because you can't offset anything to the side of a node, which sometimes is needed. I would like to undo the downvote, but I cannot until your answer has been edited. – Johannes Linkels Jul 07 '18 at 14:17
  • And to complete on the application of a shift to a node coordinate, this a way to apply an offset: \draw (R1.east) ++(0,0.1) -- (2,2) – Johannes Linkels Jul 07 '18 at 14:21
  • 1
    @jlinkels \draw([xshift=0.5]A1.left) doesn't work because .left is not an anchor is a relative position. When you say left=... of ... the new position is relative to west anchor. \draw([xshift=0.5]A1.west) will work. – Ignasi Jul 08 '18 at 18:53