# Automatically add fractions and reduce the result (if neccessary)

I'm trying to figure out how to reduce fractions. Adding fractions is no problem, see this example:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}

\pgfmathsetmacro{\newnumerator}{#1*#4+#3*#2}
\pgfmathsetmacro{\newdenominator}{#4*#2}
\pgfkeys{/pgf/number format/.cd,int detect,precision=2}
$\frac{#1}{#2} + \frac{#3}{#4} = \frac{\pgfmathprintnumber{\newnumerator}}{\pgfmathprintnumber{\newdenominator}}$
}

\begin{document}

\end{document}


In the eample, the result of adding 3/4 and 1/2 is 10/8, which obviously can be reduced to 5/4. In order to do so, I tried the frac format provided by TikZ (see tikz manual section 66 "number printing"): I computed the decimal value of my fraction, and had the result returned via \pgfmathprintnumber.

This works, however only to a certain extent. Even "simple" tasks line 4/7 + 3/8 would return a denominator of 10000 and above, instead of 53/56. I also followed the directions for increasing accuracy (frac shift and \usepackage{fp}), but still no luck.

Now I know how to manually reduce fractions, you have to find the greatest common divisor. But how to do this in an automated fashion? My guess would be that something like this is buried somewhere in TikZ as it seems to be applied when using the frac format.

So it basically comes down to this: how to find the greatest common divisor of two given numbers (preferably using TikZ)?

-
Some of the answers to tex.stackexchange.com/questions/20404/… contain code to reduce fractions. –  Caramdir Sep 16 '11 at 2:54

Using How to create a random math problem in LaTeX?, the link Cramdir pointed out, we can adapt two of the solutions there to do this. One uses Euclid's algorithm, and one uses tkz-fct.

In order to be able to package both of these solutions into a macro we need to use pgfmathtruncatemacro to ensure we are working with integers as \pgfmathsetmacro is always a decimal value. Both these produce identical results.

Here is the MWE using Euclid's algorithm:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}

\makeatletter
% Use Euclid's Algorithm to find the greatest
% common divisor of two integers.
\def\gcd#1#2{{% #1 = a, #2 = b
\ifnum#2=0 \edef\next{#1}\else
\@tempcnta=#1 \@tempcntb=#2 \divide\@tempcnta by\@tempcntb
\multiply\@tempcnta by\@tempcntb  % q*b
\@tempcntb=#1
\advance\@tempcntb by-\@tempcnta % remainder in \@tempcntb
\ifnum\@tempcntb=0
\@tempcnta=#2
\ifnum\@tempcnta < 0 \@tempcnta=-\@tempcnta\fi
\xdef\gcd@next{\noexpand%
\def\noexpand\thegcd{\the\@tempcnta}}%
\else
\xdef\gcd@next{\noexpand\gcd{#2}{\the\@tempcntb}}%
\fi
\fi}\gcd@next
}
\newcommand\reduceFrac[2]
{%
\gcd{#1}{#2}{\@tempcnta=#1 \divide\@tempcnta by\thegcd
\@tempcntb=#2 \divide\@tempcntb by\thegcd
\ifnum\@tempcntb<0\relax
\@tempcntb=-\@tempcntb
\@tempcnta=-\@tempcnta
\fi
\xdef\rfNumer{\the\@tempcnta}
\xdef\rfDenom{\the\@tempcntb}}%
}
\makeatother

\newcommand*{\fracReduced}[2]{\reduceFrac{#1}{#2}\ensuremath{\frac{\rfNumer}{\rfDenom}}}%

\pgfmathsetmacro{\newnumerator}{#1*#4+#3*#2}
\pgfmathsetmacro{\newdenominator}{#4*#2}
\pgfmathtruncatemacro{\newnumeratorTrunc}{\newnumerator}
\pgfmathtruncatemacro{\newdenominatorTrunc}{\newdenominator}
\pgfkeys{/pgf/number format/.cd,int detect,precision=2}
$\frac{#1}{#2} + \frac{#3}{#4} = \frac{\pgfmathprintnumber{\newnumerator}}{\pgfmathprintnumber{\newdenominator}} = \fracReduced{\newnumeratorTrunc}{\newdenominatorTrunc}$
}

\begin{document}
\end{document}


And here is the one using tikz-fct

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usepackage{tkz-fct}

\newcommand*{\fracReducedTkz}[2]{\tkzReducFrac{#1}{#2}\ensuremath{\frac{\tkzMathFirstResult}{\tkzMathSecondResult}}}%

\pgfmathsetmacro{\newnumerator}{#1*#4+#3*#2}
\pgfmathsetmacro{\newdenominator}{#4*#2}
\pgfmathtruncatemacro{\newnumeratorTrunc}{\newnumerator}
\pgfmathtruncatemacro{\newdenominatorTrunc}{\newdenominator}
\pgfkeys{/pgf/number format/.cd,int detect,precision=2}
$\frac{#1}{#2} + \frac{#3}{#4} = \frac{\pgfmathprintnumber{\newnumerator}}{\pgfmathprintnumber{\newdenominator}} = \fracReducedTkz{\newnumeratorTrunc}{\newdenominatorTrunc}$
}

\begin{document}

The addfraction commands are identical, except the last line calls a different macro in the two examples.
@HTG: I can not reproduce the behavior you describe. It seems that tik-fct version has an issue if the numerator is negative, but the other version seems to be ok. –  Peter Grill Nov 5 '12 at 2:26