7

I am currently trying to realize a slightly complicated graphic, wherein I have a bunch files all named with the value of a certain parameter, represented by its left-padded value. Namely, I have:

my_file_055.txt (param = 0.55)

my_file_105.txt (param = 1.05)

my_file_155.txt (param = 1.55)

So I set to generate a loop over {055, 105, 155}, and thought that using \pgfmathparse{#1/100}\pgfmathresult Would give me the correct result. This works for 105 and 155 (Rounding issues aside), but gives me the incorrect value of 0.45 for 055. This does appear with other zero-padded values, such as 035 that gives me 0.29.

M(N)WE:

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.13}

\begin{document}
  \begin{tikzpicture}
    \draw (0, 0) node {\pgfmathparse{055/100}\pgfmathresult};
  \end{tikzpicture}
\end{document}

How to get the correct value 0.35 out ?

  • 2
    Extract from pgf manual (section 89.1.1 "Commands", p.923, pgfmanual v3.0.1a): "An integer with a zero-prefix (excluding, of course zero itself), is interpreted as an octal number and is automatically converted to base 10." – Paul Gaborit Dec 14 '16 at 6:31
  • ...nice how we propagate one of the worst design decision in the hystory of software... ;-) – Rmano Dec 14 '16 at 11:36
10

Extract from pgf manual (section 89.1.1 "Commands", p.923, pgfmanual v3.0.1a): "An integer with a zero-prefix (excluding, of course zero itself), is interpreted as an octal number and is automatically converted to base 10."

So, with pgfmath, you can't use decimal numbers with zero-prefix!

  • Thanks for providing the correct explanation, however it is possible to convert zero-prefixed integers into regular integers, see my answer. – Urukann Dec 14 '16 at 9:26
  • @Urukann Yes, you are right. I should have written "with \pgfmathparse, you can't use decimal numbers with zero-prefix". – Paul Gaborit Dec 14 '16 at 13:44
8

The LaTeX3 FPU can be used here and doesn't need the intermediate value step

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff
\pgfplotsset{compat=1.13}

\begin{document}
  \begin{tikzpicture}
    \draw (0, 0) node {\fpeval{059/100}};
  \end{tikzpicture}
\end{document}
  • Hum I'd rather not pull in more packages, but this does work! – Urukann Dec 14 '16 at 9:11
4

A solution is to forcefully interpret the number as a base 10 number. The following code makes use of \pgfmathdectobase to achieve this:

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.13}

\begin{document}
   \begin{tikzpicture}
      \pgfmathdectobase\mynumber{055}{10}
      \draw (0, 0) node {\pgfmathparse{\mynumber/100}\pgfmathresult};
   \end{tikzpicture}
\end{document}

For more information, see section 71.2.7 of the pgf manual, "Base conversion functions".

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.