[Tex/LaTex] Shift line connecting anchors

tikz-pgf

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);

Best Answer

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

Related Question