12

I would like to reduce each fraction i/n to its irreproducible form (and let's assume i is less than n) and displayed not as a \frac fraction but like it is used e.g. in the frame numbers used in latex beamer, i.e. also as j/m

When using pgfmath the result is a little surprisig and the terms are usual fractions, MWE:

\documentclass[border=4pt]{standalone}
\usepackage{pgfmath,fp}
\usetikzlibrary{calc}
\pgfkeys{/pgf/number format/frac}
\newcommand{\fractions}[1]{%
  \foreach \x in {1,...,#1}
    {\pgfmathparse{\x/#1}\pgfmathprintnumber{\pgfmathresult}\hspace{1em}}
}
\begin{document}
  \fractions{16}
\end{document}

which results in eWrong fractions

How can i get that to 1/16 1/8 3/16 1/4 5/16 3/8 7/16 ... ?

Ronny
  • 6,110

4 Answers4

13

You could use the gcd() function to reduce the fractions:

\documentclass[border=4pt]{standalone}
\usepackage{pgfmath,pgffor}

\newcommand{\reducedfractions}[1]{%
    \foreach \x in {1,...,#1} {%
        \pgfmathtruncatemacro{\numerator}{\x/gcd(\x,#1)}%
        \pgfmathtruncatemacro{\denominator}{#1/gcd(\x,#1)}%
        $\frac{\numerator}{\denominator}\hspace{1em}$%
    }%
}

\begin{document}
  \reducedfractions{16}
\end{document}
Martin Heller
  • 11,391
  • gcd is not in the pgfmanual, where is the doc on the hidden macros ;-) – Tarass May 28 '14 at 15:40
  • 1
    Actually gcd is documented in the 3.0.0 version on page 939 :) Despite that - my request included the format i/n instead of a fraction itself, but changing your line 8 to \numerator/\denominator\hspace{1em} yields my required result - well done. – Ronny May 28 '14 at 17:54
5

fractions

\documentclass [a4paper]{article}
\usepackage [margin=1cm]{geometry}
\usepackage{xintfrac}
\usepackage{xintexpr}

\newcommand{\fractions}[1]{%
    \xintFor ##1 in {\xintintegers} \do {%
        \xintIrr {##1/#1}\ \ \ 
        \ifnum#1=##1\expandafter\xintBreakFor\fi
    }
}
% Note to the techies: ##1 from \xintintegers is in fact a \numexpr thing;
% generally a \numexpr needs to be prefixed by \the to be used in the xintfrac
% macros, but it is ok if it contains only at most eight tokens. As the macro is
% unlikely to be called as is with #1>99999999, no need to bother, and it is a
% bit more efficient to not do \the.

\newcommand{\mathfractions}[1]{%
    \xintFor ##1 in {\xintintegers} \do {%
        \xintFrac{\xintIrr {##1/#1}}\allowbreak\ 
        \ifnum#1=##1\expandafter\xintBreakFor\fi
    }
}

\begin{document}
\noindent\fractions {420}

\noindent\baselineskip14pt $\mathfractions {420}$\par

\bigskip
\edef\Result{\xinttheexpr reduce (40!*60!*80!/(100!*30!*50!))\relax }
$$\frac{40!*60!*80!}{100!*30!*50!}=\Result=\xintFrac{\Result}$$
\end{document}
  • Also: \xintNumerator and \xintDenominator: \edef\Result{\xintIrr{whatever/whatever}}, \xintNumerator{\Result}, \xintDenominator{\Result}. –  May 28 '14 at 21:05
  • While this works quite fine, the other solution using pgfmath was not only first but is also my favourite one - though that's really just personal favour :) – Ronny May 29 '14 at 04:58
  • @Ronny the question had the pgfmath tag, I knew my answer was borderline ;-). –  May 29 '14 at 07:03
4

Did't know about gcd, make it by myself ;-(

enter image description here

\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\PGCD}[2]{%
\edef\Num{#1}%
\edef\Den{#2}%
\loop%
    \pgfmathtruncatemacro\Mod{mod(\Num,\Den)}%
    \ifnum\Mod>0%
    \edef\Num{\Den}%
    \edef\Den{\Mod}%
    \repeat%
    \edef\Pgcd{\Den}%       
    \pgfmathtruncatemacro\Num{#1/\Pgcd}%
    \pgfmathtruncatemacro\Den{#2/\Pgcd}%    
}

\newcommand{\fractions}[1]{%
    \foreach \x in {1,...,#1} {%
        \PGCD{\x}{#1}%
        $\frac{\Num}{\Den}\hspace{1em}$%
    }
}

\begin{document}
\fractions{16}
\end{document}
Tarass
  • 16,912
  • Why all the \xdef, when \edef is sufficient? Contrary to \foreach, \loop doesn't perform cycles in groups. – egreg May 28 '14 at 16:04
  • OK, after I read your comment about groups, I took a look on the definition of xdef: \xdef is a synonym for \global\edef, and now I have understood the difference. Thank you. – Tarass May 28 '14 at 16:13
0

An implementation with the CAS Sage(math) and SageTeX:

enter image description here

I use arara: sagetex for compiling.

% arara: pdflatex
% arara: sagetex
% arara: pdflatex

\documentclass{article}
\usepackage{amsmath}
\usepackage{sagetex}

\begin{document}
\section{In}
\begin{sageblock}
x = 420       # Denominator
MyRange = 222

M = []
for n in range (1,MyRange):
    if x == denominator(n/x):
        M.append('$' + latex(n/x).replace(' ','') + '$')
    else:
        M.append('$' + '\\frac{' + "{}".format(n) + '}{' + "{}".format(x) + '}=' + latex(n/x).replace(' ','') + '$')

MyOut = ', '.join(M)
#print MyOut
\end{sageblock}

\section{Out}
\baselineskip16pt \sagestr{MyOut}
\end{document}
cis
  • 8,073
  • 1
  • 16
  • 45