11

Consider the code below:

\documentclass[convert = false]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}
  \draw[-latex] (0, 0) ellipse [x radius = 3cm, y radius = 2cm, start angle = 30,
  end angle = 150];
\end{tikzpicture}
\end{document}

enter image description here

This code produces no compiling errors but it doesn't acknowledge the start and angle or the arrow option. I could use the command arc, but then to have the arc centered at the origin, I would need to define the arc starting coordinate as

\coordinate (P) at ($(0, 0) + (30:3cm and 2cm)$);

And then draw the arc:

\documentclass[convert = false]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
  \draw[-latex] (0, 0) ellipse [x radius = 3cm, y radius = 2cm,
  start angle = 30, end angle = 150];

  \coordinate (P) at ($(0, 0) + (30:3cm and 2cm)$);

  \draw[thick, red, -latex] ($(0, 0) + (30:3cm and 2cm)$(P) arc
  (30:150:3cm and 2cm);
\end{tikzpicture}
\end{document}

enter image description here

This isn't terribly difficult but is there a way to use the ellipse command to achieve the desired result?

  • 4
    The ellipse command always draws a full ellipse, so unless you're willing to clip it, that's not the way to go. You don't need all that \coordinate and calc stuff for the arc, though: Just use \draw[thick, red, -latex] (30:3cm and 2cm) arc (30:150:3cm and 2cm);. The definition of your coordinate (($(0, 0) + (30:3cm and 2cm)$)) doesn't really make sense: It's perfectly equivalent to just saying (30:3cm and 2cm) (you're simply adding 0 to the coordinates). – Jake Jul 8 '13 at 19:54
  • @Jake I don’t think the problem here is the use of calc but that to draw an arc around a coordinate you need to evaluate/input start angle and radii twice. – Qrrbrbirlbel Jul 8 '13 at 22:26
  • @Qrrbrbirlbel: Possibly. I wasn't sure from the question (hence the comment), but I am sure that the ($(0, 0) + (30:3cm and 2cm)$) syntax is unnecessary. – Jake Jul 8 '13 at 22:28
31

You can define a new style that automates setting the start coordinate and drawing the arc:

\tikzset{
    partial ellipse/.style args={#1:#2:#3}{
        insert path={+ (#1:#3) arc (#1:#2:#3)}
    }
}

Then you can simply say

\draw[thick, red, -latex] (0,0) [partial ellipse=30:150:3cm and 2cm];

to draw the arc:

7

Apart from defining a custom to path or an insert path solution, one could re-define the way TikZ draws its arc. For that, I add two keys:

  • move to start,
  • line to start.

This implementation does only affect the superior [ ] syntax, though (but that could be changed with a small fix to \tikz@do@arc).

The advantage of settings this up as a key is that it can be applied to a scope or to a path and can be used inside other styles.

Code

\documentclass[tikz]{standalone}
\makeatletter
\def\tikz@arc@opt[#1]{% over-write!
  {%
    \tikzset{every arc/.try,#1}%
    \pgfkeysgetvalue{/tikz/start angle}\tikz@s
    \pgfkeysgetvalue{/tikz/end angle}\tikz@e
    \pgfkeysgetvalue{/tikz/delta angle}\tikz@d
    \ifx\tikz@s\pgfutil@empty%
      \pgfmathsetmacro\tikz@s{\tikz@e-\tikz@d}
    \else
      \ifx\tikz@e\pgfutil@empty%
        \pgfmathsetmacro\tikz@e{\tikz@s+\tikz@d}
      \fi%
    \fi
    \tikz@arc@moveto
    \xdef\pgf@marshal{\noexpand%
    \tikz@do@arc{\tikz@s}{\tikz@e}
      {\pgfkeysvalueof{/tikz/x radius}}
      {\pgfkeysvalueof{/tikz/y radius}}}%
  }%
  \pgf@marshal%
  \tikz@arcfinal%
}
\let\tikz@arc@moveto\relax
\def\tikz@arc@movetolineto#1{%
  \def\tikz@arc@moveto{\tikz@@@parse@polar{\tikz@arc@@movetolineto#1}(\tikz@s:\pgfkeysvalueof{/tikz/x radius} and \pgfkeysvalueof{/tikz/y radius})}}
\def\tikz@arc@@movetolineto#1#2{#1{\pgfpointadd{#2}{\tikz@last@position@saved}}}
\tikzset{%
  move to start/.code=\tikz@arc@movetolineto\pgfpathmoveto,%
  line to start/.code=\tikz@arc@movetolineto\pgfpathlineto}
\makeatother
\begin{document}
\begin{tikzpicture}[x radius=1, y radius=.6]
  \coordinate (C) at (rand,rand);
  \draw[ultra thick] (C) ellipse [];
  \draw[green]   ([shift=(30:1 and .6)] C) arc [start angle=30,   end angle=150];
  \draw[red]     (C) -- ++ (180:1 and .6)  arc [start angle=180, delta angle=70] -- cycle;
  \draw[blue!50] (C)    ++ (270:1 and .6)  arc [start angle=270, delta angle=80] -- cycle;
\end{tikzpicture}

\begin{tikzpicture}[x radius=1cm, y radius=.6cm, move to start]
  \coordinate (C) at (rand,rand);
  \draw[ultra thick] (C) ellipse [];
  \draw[green]   (C) arc [start angle=30,   end angle=150];
  \draw[red]     (C) arc [line to start, start angle=180, delta angle=70] -- cycle;
  \draw[blue!50] (C) arc [start angle=270, delta angle=80] -- cycle;
\end{tikzpicture}
\end{document}

Output

enter image description here

  • @Jake Come to think of it, the **s were not such a good idea. I think using keys is a better approach and suits the system of TikZ better. – Qrrbrbirlbel Jul 9 '13 at 2:19

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.