2

Im trying to draw a path in tikz, using Paul Gaborit's solution here TikZ: How to draw an arrow in the middle of the line? I managed to get this, where the path would be the half of the annulus.

enter image description here

But I want the bottom left arrow and inner half-circle arrow to go to the right (so all arrows have the same direction when traversing the path and having the inside of the annulus to the left).

My poor edit in paint

enter image description here

And the code I'm using

 \usepackage{tikz}
 \usetikzlibrary{positioning, calc, arrows, decorations.markings, decorations.pathreplacing}

\tikzset{
  % style to apply some styles to each segment of a path
  on each segment/.style={
    decorate,
    decoration={
      show path construction,
      moveto code={},
      lineto code={
        \path [#1]
        (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
      },
      curveto code={
        \path [#1] (\tikzinputsegmentfirst)
        .. controls
        (\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)
        ..
        (\tikzinputsegmentlast);
      },
      closepath code={
        \path [#1]
        (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
      },
    },
  },
  % style to add an arrow in the middle of a path
  mid arrow/.style={postaction={decorate,decoration={
        markings,
        mark=at position .5 with {\arrow[#1]{stealth}}
      }}},
}


\begin{center}
\begin{tikzpicture}[domain=0:4]
\draw [<->, very thick] (0,4) node (yaxis) [above] {$y$}
    |- (-4,0) node (zaxis) [left] {}
    |- (4,0) node (xaxis) [right] {$x$}
    ;
   \path [draw=black, ultra thick, postaction={on each segment={mid arrow=black}}] (3,0) arc (0:180:3cm)
    (1,0) -> (3,0)
    (1,0) arc (0:180:1cm)
    (-1,0) -> (-3,0) ;
\end{tikzpicture}
\end{center}

Any help would be appreciated :)

2

In the future, please consider posting a complete minimal working example (MWE). It makes it easier for others to get started helping you. :-)

Paul Gaborit's code applies the arrows in the direction the path is traversed. So in this case, all that's needed is to change the direction of the offending path segments.

I only had to change two lines of code inside the tikzpicture environment; both of these changes are detailed in comment lines in the code below.

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning, calc, arrows, decorations.markings, decorations.pathreplacing}

\tikzset{
  % style to apply some styles to each segment of a path
  on each segment/.style={
    decorate,
    decoration={
      show path construction,
      moveto code={},
      lineto code={
        \path [#1]
        (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
      },
      curveto code={
        \path [#1] (\tikzinputsegmentfirst)
        .. controls
        (\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)
        ..
        (\tikzinputsegmentlast);
      },
      closepath code={
        \path [#1]
        (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
      },
    },
  },
  % style to add an arrow in the middle of a path
  mid arrow/.style={postaction={decorate,decoration={
        markings,
        mark=at position .5 with {\arrow[#1]{stealth}}
      }}},
}

\begin{document}
\begin{tikzpicture}[domain=0:4]
\draw [<->, very thick] (0,4) node (yaxis) [above] {$y$}
    |- (-4,0) node (zaxis) [left] {}
    |- (4,0) node (xaxis) [right] {$x$}
    ;
   \path [draw=black, ultra thick, postaction={on each segment={mid arrow=black}}] (3,0) arc (0:180:3cm)
    (1,0) -> (3,0)
    (-1,0) arc (180:0:1cm) % changed starting point and swapped arc bounding angles
    (-3,0) -> (-1,0) ; % swapped coordinates here
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |

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.