4

To help me learn Tikz, I thought it will help, if I can draw each Tikz "object" (the thing that ends with a ";") on its own, then at the end, send them all to tikzpicture to see the whole objects put together. I do this all the time in Mathematica, but I can't get the syntax right with Latex/Tikz.

Let me explain little more, then give a MWE. Suppose I want to draw this picture, which is made of 3 separate objects (the code is from this answer)

\begin{tikzpicture}
    \draw (-1,0) arc (180:360:1cm and 0.5cm) -- (0,3) -- cycle;
    \draw[dashed] (-1,0) arc (180:0:1cm and 0.5cm);
    \shade[left color=blue!5!white,right color=blue!40!white,opacity=0.3]
            (-1,0) arc (180:360:1cm and 0.5cm) -- (0,3) -- cycle;
\end{tikzpicture}

Mathematica graphics

I'd like to draw each object on its own picture, so I can see how they look like. So I need to assign each object to a variable, to avoid copy/pasting same code. i.e. I'd like to do something like this (which does not work, since syntax is not right) but just to give an idea what I want to do

\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}
\newbool{debug} \setbool{debug}{true}  %flag for debugging
\begin{document}

%syntax error., Argument of \a has an extra }. }{}
\newcommand{\a}{\draw (-1,0) arc (180:360:1cm and 0.5cm) -- (0,3) -- cycle}
\newcommand{\b}{\draw[dashed] (-1,0) arc (180:0:1cm and 0.5cm)}
\newcommand{\z}{\shade[left color=blue!5!white,right 
    color=blue!40!white,opacity=0.3](-1,0) 
        arc (180:360:1cm and 0.5cm) -- (0,3) -- cycle}


\ifbool{debug}
{
  \tikz{\a}
  \tikz{\b}
  \tikz{\z}
 }{}

\begin{tikzpicture}  %now put things all together, once get the syntax right
    \a;
    \b;
    \z;
\end{tikzpicture}
\end{document}

The above is what I'd like to do. Now, I do this (copy/paste same code)

\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}
\newbool{debug} \setbool{debug}{true}  %flag for debugging

\begin{document}

\ifbool{debug}
{
\tikz{\draw (-1,0) arc (180:360:1cm and 0.5cm) -- (0,3) -- cycle}
\tikz{\draw[dashed] (-1,0) arc (180:0:1cm and 0.5cm)}
\tikz{\shade[left color=blue!5!white,right color=blue!40!white,opacity=0.3]
            (-1,0) arc (180:360:1cm and 0.5cm) -- (0,3) -- cycle}
}{}

\begin{tikzpicture}
    \draw (-1,0) arc (180:360:1cm and 0.5cm) -- (0,3) -- cycle;
    \draw[dashed] (-1,0) arc (180:0:1cm and 0.5cm);
    \shade[left color=blue!5!white,right color=blue!40!white,opacity=0.3]
            (-1,0) arc (180:360:1cm and 0.5cm) -- (0,3) -- cycle;
\end{tikzpicture}

\end{document}

Mathematica graphics

I tried using \def also, but many syntax errors as well. I am newbie.

Nasser
  • 20,220
  • 1
    Nothing to do with tikz you can not use numbers in command names in TeX this defines \g to be 1 : \let\g1\{ – David Carlisle Feb 09 '15 at 14:05
  • @DavidCarlisle you are right, I forgot this, I read it 3 days ago in the manual. I'll fix it now. thanks – Nasser Feb 09 '15 at 14:07
  • and of course the syntax for \let is just two tokens \let\a\b you can not use \let with a brace group, use \newcommand – David Carlisle Feb 09 '15 at 14:08
  • \let\a\{ defines \a to be \{ You may be new to tikz, but all the usual tex commands have the meaning they had before:-) – David Carlisle Feb 09 '15 at 14:10
  • @DavidCarlisle ok, ok, I am all confused by all these syntaxes, that is why I asked. Will change it to use \newcommand, but still get an syntax error :) thanks – Nasser Feb 09 '15 at 14:16
  • The error message is pretty self explanatory LaTeX Error: Command \a already defined \a is already defined, call your commands \za \zb \zz – David Carlisle Feb 09 '15 at 15:01

1 Answers1

3

The error message is pretty self explanatory:

 LaTeX Error: Command \a already defined 

call your commands \za \zb \zz then it works as you intended.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}
\newbool{debug} \setbool{debug}{true}  %flag for debugging
\begin{document}

%syntax error., Argument of \a has an extra }. }{}
\newcommand{\za}{\draw (-1,0) arc (180:360:1cm and 0.5cm) -- (0,3) -- cycle}
\newcommand{\zb}{\draw[dashed] (-1,0) arc (180:0:1cm and 0.5cm);}
\newcommand{\zz}{\shade[left color=blue!5!white,right color=blue!40!white,opacity=0.3]
            (-1,0) arc (180:360:1cm and 0.5cm) -- (0,3) -- cycle;}


\ifbool{debug}
{
  \tikz{\za}
  \tikz{\zb}
  \tikz{\zz}
 }{}

\begin{tikzpicture}  %now put things all together, once get the syntax right
    \za;
    \zb;
    \zz;
\end{tikzpicture}
\end{document}
David Carlisle
  • 757,742