pgfkeys 和 tikz 图片中的数学/方程式键

pgfkeys 和 tikz 图片中的数学/方程式键

我正在将pgfkeys其用作 TikZ 图片的新命令的变量。我可以正确地传输数值和简单的数学运算。但是,某些数学表达式未传输到命令。下面提供了 MWE。

为什么相等表达式和\unitfrac不起作用?

\documentclass[]{standalone}
\usepackage{units}
\usepackage{siunitx}
\usepackage{tikz}

\pgfkeys{/mycom/.is family, /mycom,
default/.style={text = $?$},
text/.estore in = \mytext}

\newcommand{\mycom}[1][]{
\pgfkeys{/mycom, default, #1}
%some tikx command to display the math
\draw (0,0) node{\mytext};
}

\begin{document}

\begin{tikzpicture}

%\mycom[text = aa]                         % Does work
%\mycom[text = $5x$]                       % Does work
%\mycom[text = $5x+6$]                     % Does work
%\mycom[text = $5x=6$]                     % Does not work
%\mycom[text = $\unitfrac[10]{kN}{m}$]     % Does not work
%\mycom[text = $\si{\kilo\newton/\meter}$] % Does work

\end{tikzpicture}

\end{document}

答案1

需要=保护 以阻止 PGF 尝试解析它。对于第二种情况,您不需要这么快展开它。如果您使用.store而不是.estore,那么一切都会正常进行。

该计数器仅用于测试,因为未注释的代码将所有节点排版在同一个位置。

\documentclass[border=10pt]{standalone}
\usepackage{units}
\usepackage{siunitx}
\usepackage{tikz}
\pgfkeys{/mycom/.is family, /mycom,
  default/.style={text = $?$},
  text/.store in = \mytext}
\newcounter{foo}
\newcommand{\mycom}[1][]{%
  \pgfkeys{/mycom, default, #1}%
  %some tikx command to display the math
  \draw (\thefoo,0) node {\mytext};%
  \stepcounter{foo}%
}

\begin{document}
\begin{tikzpicture}
  \mycom[text = aa]                         % Does work
  \mycom[text = $5x$]                       % Does work
  \mycom[text = $5x+6$]                     % Does work
  \mycom[text = {$5x=6$}]                     % Does not work
  \mycom[text = {$\unitfrac[10]{kN}{m}$}]     % Does not work
  \mycom[text = $\si{\kilo\newton/\meter}$] % Does work
\end{tikzpicture}
\end{document}

相关内容