0

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}
5
  • 5
    \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? Jun 23 at 14:58
  • 2
    @AndrewStacey I was just about to write an answer with this :) That's indeed the issue and fix for it.
    – Skillmon
    Jun 23 at 14:59
  • 3
    Use \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).
    – frougon
    Jun 23 at 14:59
  • Since \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 of version2 and version 3 not correct ways to expand the expression?
    – lyl
    Jun 23 at 15:29
  • Similar to pgf issue #787: Drawing code clobbers \pgfmathresult. Can't say it's not fixable, but I'm afraid the behavior won't change in recent future. Jun 23 at 15:37

2 Answers 2

4

Description

I suggest two easier approaches:

  • V5: Use a variable to save the result and use it later (\let\mylinefactor\pgfmathresult). This also allows you to calculate multiple things with \pgfmathparse afterwards.
  • V6: If you need the variable only once, you could put the calculation inside the tikz expression if you add \usetikzlibrary{calc}.

Code

\documentclass{article}
\usepackage{geometry,tikz}
\geometry{showframe}

\usetikzlibrary{calc}

\parindent0pt
\begin{document}
\pgfmathparse{0.1+0.2}\pgfmathresult
\let\mylinefactor\pgfmathresult % save the result

\pgfmathparse{0.2+0.3}\pgfmathresult % so you could make other calculations afterwards

% Version 0:
V0: |\tikz\draw(0,0)--(\textwidth * \value{page} / 3,0);|

% Version 5: works
V5: |\tikz\draw(0,0)--(\mylinefactor\textwidth,0);|

% Version 6: works
V6: |\tikz\draw(0,0)--({(0.1+0.2)*\textwidth},0);|

\end{document}

Result

enter image description here

0

Lines without tikz:

\documentclass{article}
\usepackage{showframe}
\usepackage{xfp}
\parindent0pt
\def\factor{\fpeval{0.1+0.2}}
\begin{document}
V7: |\rule{\dimexpr \textwidth * \value{page} / 3}{0.5pt}|

V8: |\rule{\dimexpr \factor\textwidth}{0.5pt}|
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.