33

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.

5
  • 3
    \draw (Q) ++(0.5cm,0) -- +(0,3cm);
    – JLDiaz
    Commented May 27, 2014 at 16:06
  • @JLDiaz so xshift wont or can't work in this case?
    – dustin
    Commented May 27, 2014 at 16:06
  • Apparently named nodes cannot be shifted. Their names make them somewhat "absolute"
    – JLDiaz
    Commented May 27, 2014 at 16:07
  • @JLDiaz do you want to make an answer? I used your comment to solve my problem.
    – dustin
    Commented May 27, 2014 at 16:17
  • Ok, I elaborated it a bit
    – JLDiaz
    Commented May 27, 2014 at 16:51

3 Answers 3

30

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.
2
  • 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. Commented Jan 9, 2018 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
    Commented Jan 9, 2018 at 11:21
26

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

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

6
  • 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. Commented Jul 6, 2018 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
    Commented Jul 6, 2018 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
    Commented Jul 6, 2018 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. Commented Jul 7, 2018 at 14:17
  • 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
    Commented Jul 8, 2018 at 18:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .