5

This is my code

\documentclass[12pt,a4paper]{article}
\usepackage[left=2cm, right=2cm, top=2cm, bottom=2cm]{geometry}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.18}
\usepackage{tikz}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[%
        declare function={a=1;b=-1;c=3;
      f(\x)=a*\x*\x+b*\x+c;x1=-b/(2*a);}
      ]
 \pgfmathparse{f(x1)}%
    $ f\left(\dfrac{1}{2}\right)=
    \pgfmathprintnumber\pgfmathresult$
\end{tikzpicture}
\end{document} 

I got f\left(\dfrac{1}{2}\right) = 2.75. How can I get the value of the f(1/2) is 11/4 not 2.75?

0

1 Answer 1

10

You can use /pgf/number format/frac and /pgf/number format/frac whole to control the format.

If you want 'proper' fractions in which the fractional part is less than unity, use /pgf/number format/frac whole=true rather than /pgf/number format/frac whole=false or omit the setting altogether.

fractional result

\documentclass[12pt]{article}
\usepackage{tikz} 
\usetikzlibrary{fpu}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[%
  declare function={%
      a=1;b=-1;c=3;
      f(\x)=a*\x*\x+b*\x+c;
      x1=-b/(2*a);
    }
  ]
  \pgfkeys{%
    /pgf/number format/frac,
    % comment the next line to get e.g. 2 3/4 rather than 11/4
    % and 3 rather than 3/1
    /pgf/number format/frac whole=false,
  }
   \pgfmathparse{f(x1)}%
   $ f\left(\dfrac{1}{2}\right)=
   \pgfmathprintnumber\pgfmathresult$
\end{tikzpicture}
\end{document} 

But note that you could just as soon use \frac{11}{4} in this case since the function always returns the same result. If you alter c, however, the result may be printed as a decimal because no fractional equivalent will be available.

Also, according to the manual, a function should always use return. (But it doesn't seem to mind.)

4
  • If I use {\pgfkeys{/pgf/number format/frac,/pgf/number format/frac whole=false} \pgfmathparse{3}% $ f(3)= \pgfmathprintnumber\pgfmathresult$}, I get f(3) = 3/1. Can I write 3 instead of 3/1? Commented Jul 9 at 4:11
  • @ThuyNguyen You'd probably have to do something fancier. I don't see an obvious out-of-the-box option. The reason it is doing this is because frac whole=false. If that were true, you'd get 3, but you'd also get 2 3/4 rather than 11/4. If you really need the in-between setting, you might have to test the result: see if it is approximately equal to an integer and format appropriately, but that would obviously involve rather greater complexity. For example, truncate or round the result and see if it close enough to the actual result; if so, set frac whole=true. Do you want the 11/4?
    – cfr
    Commented Jul 9 at 4:24
  • @ThuyNguyen Could you explain what this is meant to do? Because, as I say in my answer, as defined, the result is constant. So the result will always be 11/4 and this kind of formatting framework doesn't really make sense .... (I don't really use tikzmath much - LaTeX itself has a bunch of this stuff now and in tikz I just use pgfmath. (Or forestmath.)
    – cfr
    Commented Jul 9 at 4:27
  • 2
    Thank you very much. I used whole=true and get what I want. Commented Jul 9 at 4:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .