我想在创建时使用一些常量,高度和宽度,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}