0

I'm pretty new to using LaTeX as a teacher and I'm experimenting with fractions. Most things seem to work fine, but I'm just not able to simplify these fractions. I guess I'd need to find the greatest common divisor to do so.

I've tried to define a greatest common divisor on my own and with help provided to others who asked this question, but it doesn't seem to work.

Is there a package and/or command that makes this easy for me? Something a novice could easily understand?

Thanks in advance!

6
  • 4
    Welcome to TeX SX! What you want is not very clear. Could you explain more?
    – Bernard
    Mar 18, 2020 at 23:01
  • 1
    Please submit a MWE or at least picture of what you would like to do. Are you seeking a package for typesetting, i.e., printing the greatest common factor formula, or calculating it?
    – hesham
    Mar 18, 2020 at 23:02
  • 1
    pgf and (probably also) xint have functions that compute the greatest common divisor.
    – user194703
    Mar 18, 2020 at 23:07
  • 1
    @Schrödinger's cat I wouldn't do it in latex though. Typesetting is enough of a complications!!
    – hesham
    Mar 18, 2020 at 23:16
  • 2
    @hesham Dunno. Maybe something like this can help teachers: \usepackage{pgf} \newcommand{\SimplifyFraction}[2]{\pgfmathtruncatemacro{\mygcd}{abs(gcd(#1,#2))}% \pgfmathtruncatemacro{\simplernumerator}{abs(#1)/\mygcd}% \pgfmathtruncatemacro{\simplerdenominator}{abs(#2)/\mygcd}% \pgfmathtruncatemacro{\itest}{2*(\simplerdenominator==1)+(#1/#2<0)}% $\displaystyle\frac{#1}{#2}=\ifcase\itest \frac{\simplernumerator}{\simplerdenominator} \or -\frac{\simplernumerator}{\simplerdenominator} \or \simplernumerator \or -\simplernumerator \fi$}.
    – user194703
    Mar 18, 2020 at 23:26

1 Answer 1

3

There are certainly fancier options and more foolproof ones, but maybe this goes in the right direction.

\documentclass[fleqn]{article}
\usepackage{pgf}
\newcommand{\SimplifyFraction}[2]{\pgfmathtruncatemacro{\mygcd}{abs(gcd(#1,#2))}%
\pgfmathtruncatemacro{\simplernumerator}{abs(#1)/\mygcd}%
\pgfmathtruncatemacro{\simplerdenominator}{abs(#2)/\mygcd}%
\pgfmathtruncatemacro{\itest}{2*(\simplerdenominator==1)+(#1/#2<0)}%
$\displaystyle\frac{#1}{#2}=\ifcase\itest
\frac{\simplernumerator}{\simplerdenominator}
\or
-\frac{\simplernumerator}{\simplerdenominator}
\or
\simplernumerator
\or
-\simplernumerator
\fi$}
\begin{document}
\SimplifyFraction{12}{8}
\medskip

\SimplifyFraction{-12}{8}
\medskip

\SimplifyFraction{12}{4}
\medskip

\SimplifyFraction{-12}{4}
\end{document}

enter image description here

Such tricks have been used here on this site by several users to pretty-print ticks in plots, and I just recycle them here. You can, of course, change the syntax and so on, which is why you were asked to provide us with an MWE such that we know what the intended usage is.

You must log in to answer this question.

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