tikz/pgf 中的擴展行為

tikz/pgf 中的擴展行為

很長一段時間,我以為我已經理解了 LaTeX 是如何擴展命令的。但顯然,我不這麼認為。

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \edef\r{1 and 2}
  \draw (0,0) ellipse (2 and 4); % this works
  \draw (0,0) ellipse (\r); % this causes the error
\end{tikzpicture}
\end{document}

當嘗試編譯上面的程式碼時,我收到以下錯誤:

! Package PGF Math Error: Unknown operator `a' or `an' (in '1 and 2').

有人可以向我解釋為什麼會發生上述錯誤,以及可以採取什麼措施來避免這個問題?

答案1

這個問題是如此普遍,以至於 PGF 實際上開發了一種規範的方法來繞過它。

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \edef\r{1 and 2}
  \draw (0,0) ellipse (2 and 4); % this works
  \edef\pgfmarshal{\noexpand\draw (0,0) ellipse (\r);}
  \pgfmarshal
\end{tikzpicture}
\end{document}

\pgf@marshal(整個包裝有232個。)

相關內容