Verwenden von \newcommand mit Ellipse in Tikz

Verwenden von \newcommand mit Ellipse in Tikz

Ich möchte beim Erstellen eines einige Konstanten (Höhe und Breite) verwenden, ellipseaber das funktioniert nicht mit \newcommand. Hier ist ein Minimalbeispiel:

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

Ich erhalte die folgende Fehlermeldung:

Fehler: Zeile 14:

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

Antwort1

Setzen Sie es \widthin Klammern. Das Leerzeichen danach \widthwird verschluckt, um 2anddas folgende Ergebnis zu erhalten 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}

Bildbeschreibung hier eingeben

Übrigens können Sie auch

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

verwandte Informationen