2020-04-14: Updated to properly output case when denominator is 1.
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{%
\ifnum\rfDenom=1
\rfNumer
\else
\frac{\rfNumer}{\rfDenom}%
\fi
}%
}%
\newcommand{\addfraction}[4]{% 1N, 1D, 2N, 2D
\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}
\addfraction{3}{4}{1}{2}
\addfraction{3}{4}{5}{4}
\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{
\ifnum\tkzMathSecondResult=1
\tkzMathFirstResult
\else
\frac{\tkzMathFirstResult}{\tkzMathSecondResult}
\fi
}
}
\newcommand{\addfraction}[4]{% 1N, 1D, 2N, 2D
\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}
\addfraction{3}{4}{1}{2}
\addfraction{3}{4}{5}{4}
\end{document}
The addfraction
commands are identical, except the last line calls a different macro in the two examples.
A couple of versions, you probably need to fiddle with the spacing to get exactly what you need but:
\documentclass{article}
\def\cFrac#1#2{%
\begin{array}{@{}c@{}}\multicolumn{1}{c|}{#1}\\%
\hline\multicolumn{1}{|c}{#2}\end{array}}
\def\cFracB#1#2{%
\vcenter{\hbox{\strut$#1$\,\vrule}\hrule\hbox{\strut\vrule\,$#2$}}}
\begin{document}
$ A + \cFrac{B}{C} + \cFrac{D}{E}$.
$ A + \cFracB{B}{C} + \cFracB{D}{E}$.
\end{document}
Best Answer
You want
xfrac
There are differences between
xfrac
andnicefrac
(detailed in the manual forxfrac
), particularly if you are using fonts other than Computer Modern.