TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to be able to output something like \frac{\pi}{2} for a input number like .5*pi automatically.

I've played a bit with some of the number printing macros available with tikz (closely linked to the fpu library, available at least in the cvs version).

The idea I had is to divide the number by pi, then use the number format/frac and then add a \pi to the result. But it does not work as expected as rounding errors occur. Here is the result for 2*pi, 1.5*pi and 0.5*pi.

trig labels

Here is what I did so far.


% Adapted from the file /generic/pgf/math/pgfmathfloat.code.tex
  /pgf/number format/.cd,
  pi/.code = \pgfmath@set@number@printer{pgfmathprintnumber@pi}}


% Nothing really new here, just a copy of
% \pgfmathprintnumber@frac@formatresult only altered to add
% \pi to the result
    \pgfkeysgetvalue{/pgf/number format/frac TeX}\pgfmathresult
            \pgfkeysvalueof{/pgf/number format/frac whole format/.@cmd}}%
      \the\toks1 \ifnum#2=0 \else\the\toks0 {#2}{#3}\fi \pi}%


\pgfkeys{/pgf/number format/pi}




share|improve this question
If your denominators are always small, you could do something like multiplying with 2520, rounding to the nearest integer, and dividing by 2520 again (in the hope that this doesn't introduce new rounding errors). Note that 2520 is the least common multiple of the numbers 1 to 10; thus this will not work e.g. for pi/11 (but it could work for pi/12, because 12 divides 2520). I don't know if/how this is possible with pgf, however. – celtschk Feb 20 '12 at 16:54
@celtschk Thanks for the suggestion. However, it does not seem to help with the quick tests I did. – cjorssen Feb 20 '12 at 17:07
There is one inherent problem of that frac format: it is numerically instable. That means: it will magnify any (small) input errors considerably. See the result of Yiannidis for an illustration (that is inherent to the task, not to the impl.). Since the division by pi will already introduce a rounding error of order 1e-4 or 1e-5, you will always see distortions in the output. Perhaps it is possible to tune the algorithm if you provide additional knowledge (like the expected denominator). – Christian Feuersänger Feb 26 '12 at 16:05
@ChristianFeuersänger It seems that I underestimated the problem here. Do you have some bibliography to recommend on the subject of decimal to fraction algorithms? – cjorssen Feb 27 '12 at 13:40
I have no bibliography at hand. In case you find something which improves the task - let me know. – Christian Feuersänger Feb 27 '12 at 21:00

This is a common problem with decimal to fraction converters in general. Here is a non-TikZ solution, that illustrates the problem:

enter image description here

The algorithm is iterative and finds solutions in various degrees of accuracy, for example in the scan above you can see the "correct solution" is line 3. This is not always so, think for example as to how you would represent the decimal 0.313 in a fractional form, can be anything from 5/16 -> 641/2048 depending on the accuracy you want to achieve.

%helper macro

%% Set initial values
\FPadd\X{\X}{0.0000000001} % avoid overflows and divisions by zero
%% begin loop
%% numerator term
%% inverse
%% Find Dnext
%% Di x Int{Zi+1}

%%% Find Ni+1


\(Z_i=\Znext\to \Nnext/\Dnext =\ratio\)


\advance\count@ by1
%% end of loop



  %The number $#1=\frac{\NUM}{\DEN}$


The code is based on an algorithm that was programmed using 'C' and was a proof of concept for something else I was working on. No guarantees, just a proof of concept done in moment of madness:) I am sure can be done in Lua much easier.

share|improve this answer
Hum, rather complex problem indeed. The context I have in mind is to automatically put ticks in a pgfplot. Maybe I need to reformulate to narrow my needs. – cjorssen Feb 21 '12 at 10:06
Indeed, why don't you just scale your plots by Pi? :-) – krlmlr Feb 21 '12 at 10:20
@cjorssen Sorry I cannot provide a solution to your problem but maybe my answer helps to understanding it better. – Yiannis Lazarides Feb 21 '12 at 11:38

Substitute \pgfmathparse{#1/3.141592654} by the following code to produce the desired output for the given input:


Note that 326 = ceil(1024 / pi).

It seems to me that \pgfmathfloatgetfrac does not work as expected. I was unable to generate the fraction 1/7, even using the floating-point routines. So, while the above code should work for a large part of multiples of powers of two, don't expect it to work for anything like 1/13.

share|improve this answer
Why did you choose 1024? – cjorssen Feb 21 '12 at 10:02
Well... My intuition was that keeping it in the powers of two would make the result representable as IEEE floating-point without additional loss from the conversion from fixed to floating. And 1024 was just large enough to work and just small enough to not throw an error during compilation. But I didn't bother deriving a proof or bounds or something. – krlmlr Feb 21 '12 at 10:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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