10

I have to start by saying that I am a newbie to LaTeX, let alone TikZ.

In order to solve another problem, I found a solution in another question in a comment by Tom Bombadil. He defines the new command:

    \def\centerarc[#1](#2)(#3:#4:#5)% 
[draw options] (center) (initial angle:final angle:radius) { \draw[#1] ($(#2)+({#5*cos(#3)},{#5*sin(#3)})$) arc (#3:#4:#5); }

This basically draws an arc with a given circle centre, initial and final angle. Now I am not sure about where I should define the command. I tried at the beginning of the Tikz picture, but it does not work.

\begin{tikzpicture}[thick,>=latex]
\def\centerarc[#1](#2)(#3:#4:#5)% 
[draw options] (center) (initial angle:final angle:radius) { \draw[#1] ($(#2)+({#5*cos(#3)},{#5*sin(#3)})$) arc (#3:#4:#5); };
\centerarc[red, thick](0,0)(5:85:1);
\end{tikzpicture}

What am I doing wrong?

3 Answers 3

13

Code in comments is badly formatted: the correct input should be

\def\centerarc[#1](#2)(#3:#4:#5)% [draw options] (center) (initial angle:final angle:radius)
  { \draw[#1] ($(#2)+({#5*cos(#3)},{#5*sin(#3)})$) arc (#3:#4:#5); }

because the bits after the % are just comments showing the syntax.

You should have this in the preamble (before \begin{document}) and probably you should also add a \newcommand just to be sure \def doesn't overwrite an existing command.

\newcommand\centerarc{} % just for safety
\def\centerarc[#1](#2)(#3:#4:#5){%
  % Synopsis
  % \centerarc[draw options](center)(initial angle:final angle:radius)
  \draw[#1] ($(#2)+({#5*cos(#3)},{#5*sin(#3)})$) arc (#3:#4:#5);
}
1
  • 2
    Wow, I always was wondering if there isn't a way to combine the best of both (\def and \newcommand). Now seeing this, it looks so easy, yet it never crossed my mind to do it this way. Thanks a lot, I learned something new today! Oct 30, 2015 at 21:06
4

You could use extra #n to add flexibility. [#1]#2(#3)#4(#5:#6:#7) instead of [#1](#2)(#3:#4:#5).

\def\centerarc[#1]#2(#3)#4(#5:#6:#7)% [draw options] (center) (initial angle:final angle:radius)
  {\draw[#1]($(#3)+({#7*cos(#5)},{#7*sin(#5)})$)arc(#5:#6:#7);}

and then you can use

\centerarc[thick] (1,2) (10:20:1); % which has spaces for readability

You could probably use the power of xparse too, but I'm not sure about spaces since at this moment it doesn't accept spaces before certain arguments.

4
  • Wouldn't that also accept (unwanted) usage like \centerarc[thick] Foo (1,2) Bar (10:20:1)?
    – quinmars
    Oct 31, 2015 at 12:22
  • @quinmars Yes, but that's a side effect. Why would yo use that?
    – Manuel
    Oct 31, 2015 at 21:36
  • Shouldn't you replace \centerarc[thick] (1,2) (10:20:1); by \centerarc[thick] (1,2) (10:20:1) (no final semi column) for proper compilation?
    – pluton
    Oct 16, 2018 at 14:15
  • I think in TikZ extra ; do no harm. But may be I'm wrong.
    – Manuel
    Oct 16, 2018 at 18:36
1

You should define your new command outside, i.e. before the tikzpicture environment where you want to use it.

2
  • Using \newcommand is impossible for that case.
    – egreg
    Oct 30, 2015 at 10:51
  • @egreg, OK, I realized that later and edited consequently my answer
    – DRi
    Oct 30, 2015 at 14:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .