\text{ } subscript size in tikzmath macro is not correct
Add a \noexpand
can fix it directly. But I would think the robustify approach makes more sense if this is an ongoing issue.
\documentclass[]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{math}
%%%To fix the text subscript
%\usepackage{etoolbox}
%\makeatletter
%\robustify{\text}
%\makeatother
\begin{document}
\begin{tikzpicture}
\node at (0,0) {$\text{b}_\text{b}$: Correct};
\tikzmath{let \test=\text{b}_{\text{b}};}
\node at (0,-1cm) {$\test$: Not Correct};
\tikzmath{let \test=\text{b}_{\noexpand\text{b}};}
\node at (0,-2cm) {$\test$: Corrected with noexpand};
\end{tikzpicture}
\end{document}
You can see what's going on if you add \show\text
after the \tikzmath
declaration:
> \test=macro:
->\protect \unhbox \voidb@x \hbox {b}_{\protect \unhbox \voidb@x \hbox {b}}.
Indeed, the definition of \text
is found in amstext.sty
:
% amstext.sty, line 28:
\DeclareRobustCommand{\text}{%
\ifmmode\expandafter\text@\else\expandafter\mbox\fi}
You also have to know that \tikzmath
does full expansion; since \text
is not found in math mode, it just does \mbox
. Since at the time of let
the meaning of \protect
is \relax
, it goes on untouched; then the conditional is expanded and since TeX is not in math mode, you get \mbox{b}
, which becomes
\leavemode\hbox{b}
and finally \unhbox\voidb@x\hbox{b}
. This should explain the output of \show
above.
Possibly you want to evaluate something instead of having b
in the argument to \text
; in this case you need to be careful about what to fully expand and what not.
\tikzmath{let \test=\noexpand\text{b}_{\noexpand\text{b}};}
will do, but if instead of b
you have something like \textbf{abc}
, this will die horribly anyhow.
You need \noexpand
in front of both occurrences of \text
, in order to suppress its expansion and get the right version when \test
is indeed used in math mode.