2

I am trying to change TikZ: draw only a certain central length of a given path to fit my needs. I have a path that is built with \draw (A) to [bend right=30] (B); I want to now shift this path in a parallel way but only show a segment of it. I am hoping this segment can be arbitrary chosen with two different coordinates. Here is my code:

\documentclass[12pt, thmsa]{article}
\usepackage{tikz,pgf,  pgfplots}
\pgfdeclaredecoration{ignore}{final}
{
\state{final}{}
}

% Declare the actual decoration.
\pgfdeclaremetadecoration{myshortershift}{initial}{
  \state{initial}[
  width=1pt,
        next state=myshortershift
    ]
    {\decoration{moveto}}

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

    \state{final}
    {\decoration{ignore}}
  }
  \tikzset{myshortershift segment/.style={decoration={myshortershift},decorate, segment length=#1}}

\begin{document}
 \begin{tikzpicture}[>=latex]
  \begin{axis}[
    axis x line=bottom,
    axis y line=left,
    xmin=0, xmax=10, 
    ymin=0, ymax=10,
     x label style={at={(axis description cs:1,-.01)},anchor=south},
    ylabel style={at={(axis description cs:0.1,1)},anchor=west, rotate=270}, 
    xlabel={$X$},
    ylabel={$Y$},
    ytick=\empty,
    xtick=\empty,
    ]
    \coordinate (A) at (axis cs:2.2, 9.5);
    \coordinate (B) at (axis cs:8, .5);
    \draw (A) to [bend right=30] (B);
 \draw[blue] (axis cs:2.2+1, 9.5+1) to [bend right=30]  (axis cs:8+1,.5+1);
    \draw [blue,->, dotted, myshortershift segment=1.75cm] (axis cs:2.2+.5,9.5+.5) to [bend right=30]  (axis cs:8+.5,.5+.5);
    \draw [blue,->, dotted, myshortershift segment=1.75cm] (axis cs:8+.5,.5+.5) to [bend left=30]  (axis cs:2.2+.5,9.5+.5);
  \end{axis}
  \end{tikzpicture} 
\end{document}

enter image description here

I am trying to get the dotted arrows to start and stop at any two given coordinate (these will be derived from some intersections commands)

Thank you

2

Welcome to TeX-SE! This defines a shifted curve, and draws the relevant intersection segments with two lines (thin red to guide the eye). It also draws the segment between the two intersections. To this end, one has to first define an auxiliary path (called rest1) which starts from the first intersection and goes to the end of the curve. And then draw the first intersection segment of this path with the second line. Please note also that in recently recent version of pgfplots, which you can access with \pgfplotsset{compat=...}, axis cs: is no longer needed.)

\documentclass[12pt, thmsa]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\pgfdeclaredecoration{ignore}{final}
{
\state{final}{}
}

% Declare the actual decoration.
\pgfdeclaremetadecoration{myshortershift}{initial}{
  \state{initial}[
  width=1pt,
        next state=myshortershift
    ]
    {\decoration{moveto}}

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

    \state{final}
    {\decoration{ignore}}
  }
  \tikzset{myshortershift segment/.style={decoration={myshortershift},decorate, segment length=#1}}

\begin{document}


 \begin{tikzpicture}[>=latex]
  \begin{axis}[
    axis x line=bottom,
    axis y line=left,
    xmin=0, xmax=10, 
    ymin=0, ymax=10,
     x label style={at={(axis description cs:1,-.01)},anchor=south},
    ylabel style={at={(axis description cs:0.1,1)},anchor=west, rotate=270}, 
    xlabel={$X$},
    ylabel={$Y$},
    ytick=\empty,
    xtick=\empty,
    ]
    \coordinate (A) at (2.2, 9.5);
    \coordinate (B) at (8, .5);
    \draw (A) to [bend right=30] (B);
    \draw[blue] (2.2+1, 9.5+1) to [bend right=30]  (8+1,.5+1);
    \path[%draw=blue!30,ultra thin,
     name path=shifted curve] (2.2+2, 9.5+2) to [bend right=30]  (8+2,.5+2);
    \path[draw=red!30,ultra thin,name path=line1] (2.2,8) -- (10,8);
    \path[draw=red!30,ultra thin,name path=line2] (2.2,4) -- (10,4);
    \draw[-latex,blue,very thick,
        intersection segments={of=shifted curve and line1,sequence={A0}}];
    \draw[-latex,blue,very thick,
        intersection segments={of=shifted curve and line2,sequence={A1[reverse]}}];
    \path[name path=rest1,intersection segments={of=shifted curve and line1,sequence={A1}}];    
    \draw[latex-latex,red,very thick,
        intersection segments={of=rest1 and line2,sequence={A0}}];
%     \draw [blue,->, dotted, myshortershift segment=1.75cm] (axis cs:2.2+.5,9.5+.5) to [bend right=30]  (axis cs:8+.5,.5+.5);
%     \draw [blue,->, dotted, myshortershift segment=1.75cm] (axis cs:8+.5,.5+.5) to [bend left=30]  (axis cs:2.2+.5,9.5+.5);
  \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

  • Thank you so much for the answer marmot, but how do I draw an arrow that starts at one of the red lines and finishes on the other. – Jean Eid Jun 14 at 15:20
  • @JeanEid You only need to add \path[name path=rest1,intersection segments={of=shifted curve and line1,sequence={A1}}]; \draw[latex-latex,red,very thick, intersection segments={of=rest1 and line2,sequence={A0}}];. – user121799 Jun 14 at 15:23
  • Thank you! where can I look to understand your code. – Jean Eid Jun 14 at 15:28
  • @JeanEid This is all explained in section 5.7.6 Intersection Segment Recombination of the pgfplots manual. These tricks require the fillbetween library, which is loaded here. – user121799 Jun 14 at 15:30
  • Amazing, thank you! – Jean Eid Jun 14 at 15:33

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.