5

I am using pgfkeys as variables of a newcommand for TikZ pictures. I can transfer numerical values and simple math correctly. However, some math expressions are not transferred to the command. A MWE is provided below.

Why an equality expression and the \unitfrac are not working?

\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
  • Unrelated: In general I'd use \per instead of / in \si, and say \sisetup{per-mode=symbol} to get a / in the output. Jun 6, 2017 at 7:41

1 Answer 1

7

The = needs to be protected to stop PGF trying to parse it. For the second case, you need not to expand it so soon. If you use .store rather than .estore, this all works just fine.

The counter is only for testing because the uncommented code typeset all nodes in the same place.

\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}
8
  • Have you changhe your icon?:-) +1 for your answer.
    – Sebastiano
    Jun 6, 2017 at 7:04
  • @cfr: I have been using .estore for a reason that I do not remember now. There are some discussion about the use of .estore here. Do you have any comments?
    – berkus
    Jun 6, 2017 at 8:58
  • @cfr: Regarding $a=b$. How come the $-signs are ignored? I thought the math enviroment provides the necessary protection. Thoughts?
    – berkus
    Jun 6, 2017 at 9:00
  • @cfr: Seems like we need to add the { } to the $\unitfrac[]{}{}$ along with the .store. Why do we need the brackets here?
    – berkus
    Jun 6, 2017 at 9:05
  • @cfr: The MWE is actually a package I am working on. To make the code more suitable for end-user, is there any other option without the curly brackets?
    – berkus
    Jun 6, 2017 at 9:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .