3

I checked this post in here Drawing complex integration but what I want is draw the one that exclude zero and negative real numbers instead of positives. I tried to play around with the code in the link above and I couldn't figure it out. I'm a beginner with the drawings in Latex

2 Answers2

7

The quickest solution in this case is to do a reflection using xscale=-1 in the answer to the question linked:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.markings}
\begin{document}

\begin{tikzpicture}[xscale=-1]
% Configurable parameters
\def\gap{0.2}
\def\bigradius{3}
\def\littleradius{0.5}

% Axes
\draw (-1.1*\bigradius, 0) -- (1.1*\bigradius,0)
      (0, -1.1*\bigradius) -- (0, 1.1*\bigradius);
% Red path
\draw[red, thick,   decoration={ markings,
      mark=at position 0.17 with {\arrow{latex}}, 
      mark=at position 0.53 with {\arrow{latex}},
      mark=at position 0.755 with {\arrow{latex}},  
      mark=at position 0.955 with {\arrow{latex}}}, 
      postaction={decorate}]  
  let
     \n1 = {asin(\gap/2/\bigradius)},
     \n2 = {asin(\gap/2/\littleradius)}
  in (\n1:\bigradius) arc (\n1:360-\n1:\bigradius)
  -- (-\n2:\littleradius) arc (-\n2:-360+\n2:\littleradius)
  -- cycle;
\end{tikzpicture}
\end{document}

enter image description here

If you don't want to use a reflection, simply redraw the path using the appropriate points for the arcs and the straight line segments:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.markings}
\begin{document}

\begin{tikzpicture}
% Configurable parameters
\def\gap{0.2}
\def\bigradius{3}
\def\littleradius{0.5}

% Axes
\draw (-1.1*\bigradius, 0) -- (1.1*\bigradius,0)
      (0, -1.1*\bigradius) -- (0, 1.1*\bigradius);
% Red path
\draw[red, thick,   decoration={ markings,
      mark=at position 0.17 with {\arrow{latex}}, 
      mark=at position 0.53 with {\arrow{latex}},
      mark=at position 0.755 with {\arrow{latex}},  
      mark=at position 0.955 with {\arrow{latex}}}, 
      postaction={decorate}]  
  let
     \n1 = {asin(\gap/2/\bigradius)},
     \n2 = {asin(\gap/2/\littleradius)}
  in (-180-\n2:\littleradius) arc (180-\n2:-180+\n2:\littleradius) --
  (180+\n1:\bigradius) arc (-180+\n1:180-\n1:\bigradius) -- cycle;
\end{tikzpicture}
\end{document}
Gonzalo Medina
  • 505,128
  • Nitpicking, but drawing on the path doesn't work for with "holes." For example the arrow type open triangle 60. –  May 04 '13 at 06:41
  • @MarcvanDongen you can define a new arrow (for example new open triangle 60) like in my answer here : http://tex.stackexchange.com/questions/104164/color-the-faces-of-a-graph/104169#104169 – Alain Matthes May 04 '13 at 08:57
  • @user30145 You're welcome. Don't forget that you can accept (by clicking the checkmark to the left of the answer) the answer that you consider best solved your problem; in case of doubt, please see How do you accept an answer?. – Gonzalo Medina May 04 '13 at 13:26
2

The problem with TikZ is the difficulty to use arc if you don't know a minimum of trigonometry. You need to know some angles to draw the arcs, so you need to know how to use asin, atan2 etc ... Here a solution to get the last polar coordinates of a point with a new macro pgfgetlastar. This macro is like pgfgetlastxy but the results here are the polar coordinates of the last point. I will try to develop explanations with other methods in the other question about closed contours

The macro is

\def\pgfgetlastar#1#2{%
  \pgfmathparse{veclen(\pgf@x,\pgf@y)/28.45274}
    \edef#1{\pgfmathresult}%
    \pgfmathparse{atan2(\pgf@x,\pgf@y)}
    \edef#2{\pgfmathresult}%
}%

You can use it with a style :

\tikzset{
    last polar/.code 2 args=
     {\pgfgetlastar{#1}{#2} }
    }

it's the easiest way but possible is something like \pgfextra{\pgfgetlastar{\r}{\a}}

Now the bigger part of the code is the decoration part to get the arrow :

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.markings,arrows}
\begin{document}

\makeatletter

\def\pgfgetlastar#1#2{%
  \pgfmathparse{veclen(\pgf@x,\pgf@y)/28.45274}
    \edef#1{\pgfmathresult}%
    \pgfmathparse{atan2(\pgf@x,\pgf@y)}
    \edef#2{\pgfmathresult}%
}%
\makeatother

\tikzset{
    last polar/.code 2 args=
     {\pgfgetlastar{#1}{#2} }
    }

\begin{tikzpicture}
\draw[red,postaction=decorate,decoration={markings,
      mark=at position .17 with {\arrow[scale=2]{>}},
      mark=at position .51 with {\arrow[scale=2]{>}},
      mark=at position .72 with {\arrow[scale=2]{>}},
      mark=at position .95 with {\arrow[scale=2]{>}}}]  
      (176:4 cm) arc (176:-176:4 cm) -- +(3,0) [last polar={\r}{\a}] arc (\a:-\a:\r) --cycle ;
\end{tikzpicture}

\end{document}

enter image description here

Now for the problem described by Mark with open triangle 60, it's possible to declare a new arrow

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.markings,arrows}
\begin{document}

\makeatletter

\def\pgfgetlastar#1#2{%
  \pgfmathparse{veclen(\pgf@x,\pgf@y)/28.45274}
    \edef#1{\pgfmathresult}%
    \pgfmathparse{atan2(\pgf@x,\pgf@y)}
    \edef#2{\pgfmathresult}%
}%

\pgfarrowsdeclare{new open triangle 60}{new open triangle 60}
{
  \pgfutil@tempdima=0.5pt%
  \advance\pgfutil@tempdima by.25\pgflinewidth%
  \pgfutil@tempdimb=7.29\pgfutil@tempdima\advance\pgfutil@tempdimb by.5\pgflinewidth%
  \pgfarrowsleftextend{+-\pgfutil@tempdimb}
  \pgfutil@tempdimb=.5\pgfutil@tempdima\advance\pgfutil@tempdimb by\pgflinewidth%
  \pgfarrowsrightextend{+\pgfutil@tempdimb}
}
{
  \pgfutil@tempdima=0.5pt%
  \advance\pgfutil@tempdima by.25\pgflinewidth%
  \pgfsetdash{}{+0pt}
  \pgfsetmiterjoin
    \pgfsetfillcolor{white}
  \pgfpathmoveto{\pgfpointadd{\pgfqpoint{0.5\pgfutil@tempdima}{0pt}}{\pgfqpointpolar{150}{9\pgfutil@tempdima}}}
  \pgfpathlineto{\pgfqpoint{0.5\pgfutil@tempdima}{0\pgfutil@tempdima}}
  \pgfpathlineto{\pgfpointadd{\pgfqpoint{0.5\pgfutil@tempdima}{0pt}}{\pgfqpointpolar{-150}{9\pgfutil@tempdima}}}
  \pgfpathclose
  \pgfusepathqfillstroke
}
\makeatother

\tikzset{
    last polar/.code 2 args=
     {\pgfgetlastar{#1}{#2} }
    }

\begin{tikzpicture}
\draw[red,postaction=decorate,decoration={markings,
      mark=at position .17 with {\arrow[scale=2]{new open triangle 60}},
      mark=at position .51 with {\arrow[scale=2]{new open triangle 60}},
      mark=at position .72 with {\arrow[scale=2]{new open triangle 60}},
      mark=at position .95 with {\arrow[scale=2]{new open triangle 60}}}]  
      (176:4 cm) arc (176:-176:4 cm) -- +(3,0) [last polar={\r}{\a}] arc (\a:-\a:\r) --cycle ;
\end{tikzpicture}

\end{document}

enter image description here

Alain Matthes
  • 95,075