tikz で楕円付きの \newcommand を使用する

tikz で楕円付きの \newcommand を使用する

を作成するときに、高さと幅などの定数を使用したいのですellipseが、 では機能しません\newcommand。以下に最小限の例を示します。

\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} 

次のエラーが発生します:

エラー: 14行目:

Package PGF Math Error: Unknown operator `a' or `an' (in '2and 0.5*2'). ...ay!40] (0,0) ellipse (\width and \height)

答え1

中括弧で囲みます\width。 の後のスペースはとなるよう\widthに飲み込まれます。2and2 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}

ここに画像の説明を入力してください

ちなみに、

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

関連情報