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.

How can I shift a line the endpoints of which are defined by anchors like this?

\draw (i0) -- (i1);

The following approach does not work:

\draw[xshift=2pt] (i0) -- (i1);
share|improve this question
1  
The question are ambiguous. You write anchors like anchors of node (tikz's notion) or you use another definition. I would like to know if anchors are coordinate \coordinate (i0) at (A.north west); or if anchors are nodes like in Marco's answer because the answer is not the same –  Alain Matthes Jan 2 '12 at 14:39
add comment

2 Answers

up vote 15 down vote accepted

Approach 1

You can work with the library calc:

\draw[green] (i0) -- ($(i1)+(1,2)$);

Approach 2

Another approach based on the let operation. An example is given in the question TikZ: Node at same x-coordinate as another node, but specified y-coordinate?

\draw[blue] let \p1 = (i0) in (2,\y1) -- (i1);

Approach 3

Jake mentioned another approach. You set the shift to the coordinate directly. (this method is documented in the manual section 13 "Specifying Coordinates")

\draw ([xshift=2pt]i0) -- ([xshift=2pt]i1);

Note: By using this method will work fine if you define i0 and i1 with \coordinate. If you define i0 and i1 with \node you must give an anchor

\draw ([xshift=2pt]i0.center) -- ([xshift=2pt]i1.center);

This limitation isn't relevant for the other approaches.

Complete example with result

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\verb+Calc+
\begin{tikzpicture}
\node at (0,0) (i0){};
\node at (2,2) (i1){};
\draw[red] (i0) -- (i1);
\draw[blue] ($(i0)+(2,0)$) -- (i1);
\draw[green] (i0) -- ($(i1)+(1,2)$);
\end{tikzpicture}

\verb+let+
\begin{tikzpicture}
\node at (0,0) (i0){};
\node at (2,2) (i1){};
\draw[red] (i0) -- (i1);
\draw[blue] let \p1 = (i0) in (2,\y1) -- (i1);
\draw[green] let \p1 = (i1) in (i0) -- (\x1,4);

\draw[black] let \p0 = (i0), \p1=(i1) in (\x0,2) -- (\x1,3);
\end{tikzpicture}

\verb+shift+ 1
\begin{tikzpicture}
\node at (0,0) (i0){};
\node at (2,2) (i1){};
\draw[red] (i0) -- (i1);
\draw[blue]([xshift=2cm]i0.center) --  (i1);
\draw[green] (i0) -- ([yshift=2cm,xshift=1cm]i1.center);
\end{tikzpicture}

\verb+shift+ 2
\begin{tikzpicture}
\coordinate (i0) at (0,0) ;
\coordinate (i1) at (2,2) ;
\draw[red] (i0) -- (i1);
\draw[blue]([xshift=2cm]i0) --  (i1);
\draw[green] (i0) -- ([yshift=2cm,xshift=1cm]i1);
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
4  
A different approach would be to add the transformation to the coordinate specification (so \draw ([xshift=2pt]i1) -- ([xshift=2pt]i2);). Maybe you could flesh out your answer to show examples for all three approaches? –  Jake Jan 2 '12 at 11:38
    
I just tried the experiment and found that the syntax ([shift={(1,2)}]A) only works for positioning nodes. Strangely, ([shift={(1,2)}]0,0) works for arbitrary paths. I have no idea why this would be so, it seems rather illogical to me. –  Andrew Stacey Jan 2 '12 at 12:18
1  
Ah, I see that the answer has been edited and that it works if an explicit anchor is given. This could be a bug in how the point-on-the-perimeter is computed for a translated node. Or one could say that translating a node coordinate without a specific anchor is open to different interpretations on which anchor should be used, so translating without an anchor should not be used. –  Andrew Stacey Jan 2 '12 at 12:22
1  
@Altermundus and Marco Daniel: "anchors" are specified points on a shape, like north east, base west, 135, and so on. If you don't supply an anchor to a coordinate specification, but only a node name (like i0), an anchor on the edge the node that lies in the direction of the path is used. Only if i0 happens to be a node of the coordinate shape (created using \coordinate ... or \node [coordinate] ...), this is equivalent to the center anchor. So the original question is indeed ambiguous, as Altermundus noted. I guess it's another example of why MWEs are useful... –  Jake Jan 3 '12 at 9:33
1  
(ctd) until they see the possible solutions, whereupon they will decide which behaviour they want and choose accordingly. –  Andrew Stacey Jan 3 '12 at 9:33
show 9 more comments

For me the shifting line is like the blue one

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc} 
\begin{document}

\begin{tikzpicture} 
\draw [help lines] (0,0) grid (3,2); 
\node[draw](i0) at (0,0) {};
\node[draw](i1) at (2,2) {};
\draw[red] (i0) -- (i1) coordinate[pos=0] (j0) coordinate[pos=1] (j1); 

\draw[thick,blue] ([xshift=1cm]j0) --  ([xshift=1cm]j1);
\draw[thick,purple] let \p0=(i0),\p1=(i1) in
         ([xshift=.5cm]\x0,\y0) --  ([xshift=.5cm]\x1,\y1);        
\end{tikzpicture} 

\end{document}

enter image description here

share|improve this answer
add comment

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.