在 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給了2and應有的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}

在此輸入影像描述

順便說一句,你也可以使用

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

相關內容