I trid to draw a line with TikZ
and failed, as the code of version 1
shows. I think this is because the expression \pgfmathresult\textwidth
for the end coodinate does not expand. So I tried the code of version 2
and version 3
, attempting to expand \pgfmathresult\textwidth
first. But both does not work either. Why?
Though the code of version 4
typesets the line as expected, I don't want to introduce an auxilary macro(here is \x
).
So, how to expand an expression in coodinates in the scope of TikZ
itself?
MWE:
\documentclass{article}
\usepackage{geometry,tikz}
\geometry{showframe}
\parindent0pt
\begin{document}
\pgfmathparse{0.1+0.2}\pgfmathresult
% Version 0:
V0: |\tikz\draw(0,0)--(\textwidth * \value{page} / 3,0);|
% Version 1: fail
V1: |\tikz\draw(0,0)--(\pgfmathresult\textwidth,0);|
% version 2: fail
V2: |\tikz\draw(0,0)--(\expanded{\pgfmathresult\textwidth},0);|
% version 3: fail
V3: |\tikz\draw(0,0)--(\the\dimexpr\textwidth * \pgfmathresult\relax,0);|
% version 4: works, but I don't want to introduce an new auxilary macro.
V4: |\edef\x{\noexpand\tikz\noexpand\draw(0,0)--(\pgfmathresult\textwidth,0);}\x|
\end{document}
\pgfmathresult
is not guaranteed to persist beyond the immediate calculation. Does it work if you save the result in a different macro immediately after calculating it?\pgfmathsetmacro{\mytmp}{0.1+0.2}
and\tikz\draw(0,0)--(\mytmp*\textwidth,0);
. “I don't want” is not good enough in front of reality (what Andrew mentioned).\pgfmathresult
is used to store the result of calculation, why does another temp macro have to be used to store again the result? And why are my codes ofversion2
andversion 3
not correct ways to expand the expression?