I am trying to reference the first character of a string within a pgfmathsetmacro
, however, the macro \StrLeft
from xstring
, I believe, is not playing well with pgfmathsetmacro
.
Take the following example that replicates this problem:
\documentclass{article}
\usepackage{tikz}
\usepackage{xstring}
\begin{document}
\begin{tikzpicture}
\newcommand{\str}{hello};
\newcommand{\firstChar}
{
\StrLeft{\str}{1}
};
\pgfmathsetmacro{\halfStr}{width("\str") * 1pt / 2cm};
\pgfmathsetmacro{\halfFirstChar}{width("\firstChar") * 1pt / 2cm};
\node at (0, 0) {\str};
\draw [color=black] (\halfFirstChar - \halfStr, 0cm) -- (\halfFirstChar - \halfStr,-1cm);
\end{tikzpicture}
\end{document}
The following code should output the following:
(The string "hello" printed, along with a vertical line beginning from the halfway point of the "h" character and then moving downwards 1 centimeter.)
However, \halfFirstChar
generates an error that reads:
Argument of \reserved@a has an extra }. Runaway argument? Extra }, or forgotten \endgroup.
I am able to print \firstChar
without any problems by doing the following:
% ...
\node at (0, 0) {\firstChar};
% ...
However, I am unable to reference \firstChar
within \halfFirstChar
. Does anyone have any idea what might be causing this error and a solution to this problem?