Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 ... ?

share|improve this question

3 Answers 3

up vote 12 down vote accepted

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}
share|improve this answer
    
gcd is not in the pgfmanual, where is the doc on the hidden macros ;-) –  Tarass May 28 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 at 17:54

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}
share|improve this answer
    
Why all the \xdef, when \edef is sufficient? Contrary to \foreach, \loop doesn't perform cycles in groups. –  egreg May 28 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 at 16:13

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}
share|improve this answer
    
Also: \xintNumerator and \xintDenominator: \edef\Result{\xintIrr{whatever/whatever}}, \xintNumerator{\Result}, \xintDenominator{\Result}. –  jfbu May 28 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 at 4:58
    
@Ronny the question had the pgfmath tag, I knew my answer was borderline ;-). –  jfbu May 29 at 7:03

Your Answer

 
discard

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.