2

I am studying the answer of @Caramdir to TikZ: draw only a certain central length of a given path

I would like the segment to be shifted orthogonally relative to the path. I am trying to insert raise somewhere in

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations}

% A simple empty decoration, that is used to ignore the last bit of the path                                                                  
\pgfdeclaredecoration{ignore}{final}
                     {
                       \state{final}{}
                     }

                     % Declare the actual decoration.                                                                                         
                     \pgfdeclaremetadecoration{middle}{initial}{
               \state{initial}[
                         width={(\pgfmetadecoratedpathlength - \the\pgfdecorationsegmentlength)/2},
                         next state=middle
                       ]
                             {\decoration{moveto}}

                     \state{middle}[
                               width={\the\pgfdecorationsegmentlength},
                               next state=final
                             ]
                                   {\decoration{curveto}}

                                   \state{final}
                                         {\decoration{ignore}}
                     }

% Create a key for easy access to the decoration (as suggested by Jake).                                                 
\tikzset{middle segment/.style={decoration={middle},decorate, segment length=#1}}
\begin{document}
\begin{tikzpicture}[radius=2pt]
     \fill (0,0) circle;
     \fill (4,2) circle;
     \draw[middle segment=2cm, double, red, ->] (0,0) to[bend right =20] (4,1);
     \end{tikzpicture}
\end{document}
1

You could simply use the calc library to shift the path.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations,calc}

% A simple empty decoration, that is used to ignore the last bit of the path                                                                  
\pgfdeclaredecoration{ignore}{final}
                     {
                       \state{final}{}
                     }

                     % Declare the actual decoration.                                                                                         
                     \pgfdeclaremetadecoration{middle}{initial}{
               \state{initial}[
                         width={(\pgfmetadecoratedpathlength - \the\pgfdecorationsegmentlength)/2},
                         next state=middle
                       ]
                             {\decoration{moveto}}

                     \state{middle}[
                               width={\the\pgfdecorationsegmentlength},
                               next state=final
                             ]
                                   {\decoration{curveto}}

                                   \state{final}
                                         {\decoration{ignore}}
                     }

% Create a key for easy access to the decoration (as suggested by Jake).                                                 
\tikzset{middle segment/.style={decoration={middle},decorate, segment length=#1}}
\begin{document}
\begin{tikzpicture}[radius=2pt]
     \fill (0,0) circle;
     \fill (4,2) circle;
     \draw[middle segment=2cm, red, ->] (0,0) to[bend right =20] (4,1);
     \draw[middle segment=2cm, red, ->] ($(0,0)!1cm!90:(4,1)$) to[bend right =20] 
     ($(4,1)!1cm!-90:(0,0)$);
     \end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.