3

I want to use some constants, height and width, when creating an ellipse but it won't work with \newcommand. Here is a minimal example:

\documentclass[12pt, a4paper]{article}

\usepackage{amsthm, tikz}
\usetikzlibrary{arrows, shapes, trees, positioning}
\begin{document}

\begin{figure}
\centering
\begin{tikzpicture} 

    \newcommand*{\width}{2}%
    \newcommand*{\height}{0.5*\width}%

    \draw[fill=gray!40] (0, 0) ellipse (\width and \height); \node (Y) at (0,0) {\(Y\)};    

\end{tikzpicture}
\end{figure}

\end{document} 

I get the following error:

error: line 14:

Package PGF Math Error: Unknown operator `a' or `an' (in '2and 0.5*2'). ...ay!40] (0,0) ellipse (\width and \height)
  • The correct usage for the ellipse and the circle path operator (they're the same in that regard) is: \draw[fill=gray!40] (0, 0) ellipse[x radius=\width, y radius=.5*\height]; This also makes it possible to use with styles and make reference (e.g. …[y radius=.5*\pgfkeysvalueof{/tikz/x radius}] … ellipse[x radius=2] … ellipse[x radius=4] …). Also, check out the ellipse shape from the shapes.geometric library. (Your node usages indicates you could use that.) – Qrrbrbirlbel Apr 30 '15 at 19:49
2

Put \width inside braces. The space after \width is swallowed to give 2and which should be 2 and.

\documentclass[12pt, a4paper]{article}

\usepackage{amsthm, tikz}
\usetikzlibrary{arrows, shapes, trees, positioning}
\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}

    \newcommand*{\width}{2}%
    \newcommand*{\height}{0.5*\width}%

    \draw[fill=gray!40] (0, 0) ellipse ({\width} and {\height}); \node (Y) at (0,0) {\(Y\)};

\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

BTW, you can also use

\pgfmathsetmacro{\width}{2}
\pgfmathsetmacro{\height}{0.5*\width}

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.