7

I want to shift a line by some amount in some direction. Here the direction is fixed, orthogonal to the line. One difficulty is that the line should be drawn like it would be at the origin position. The solution I have almost does what I want, except that the shifted line is longer, because it is not constrained by the nodes.

The examples I found e.g. Shifting a line joining nodes in TikZ affect how the line is drawn.

Something like (A) edge[shift left=2cm] (B); would be nice.

\documentclass[tikz, border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}
\node (A) at (3,0) {$A$};
\node (B) at (5,1) {$B$};
\path[-]
    (A) edge (B)
    ($(A)!5mm!90:(B)$) edge node{shifted} ($(B)!5mm!-90:(A)$);
\end{tikzpicture}
\end{document}

2 Answers2

11

(Note: the shifted lines do not care about the border of the nodes.)

You may use the simple decoration provided by JLDiaz in this answer:

enter image description here

\documentclass[tikz]{standalone}

\usetikzlibrary{decorations}
\pgfdeclaredecoration{simple line}{initial}{
  \state{initial}[width=\pgfdecoratedpathlength-1sp]{\pgfmoveto{\pgfpointorigin}}
  \state{final}{\pgflineto{\pgfpointorigin}}
}
\tikzset{
   shift left/.style={decorate,decoration={simple line,raise=#1}},
   shift right/.style={decorate,decoration={simple line,raise=-1*#1}},
}

\begin{document}
\begin{tikzpicture}
  \node (A) at (0,0) {A};
  \node (B) at (2,1) {B};

  \path[->,red] (A) edge (B);
  \path[->,blue] (A) edge[shift left=2mm] (B);
  \path[->,orange] (A) edge[shift right=2mm] (B);
\end{tikzpicture}
\end{document}
Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
2

Another solution is to use transform canvas, e.g.:

\documentclass[tikz, border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}
\node (A) at (3,0) {$A$};
\node (B) at (5,1) {$B$};
\path[-]
    (A) edge (B)
\path[-,transform canvas={yshift=5mm,xshift=-2.5mm}]
    (A) edge (B);
\end{tikzpicture}
\end{document}

Naturally, xshift and yshift depends on the line angle and distance from the original line you want to achieve.

pisoir
  • 882