# How do I typeset arbitrary fractions like the standard symbol for .5 = ½?

I know I've seen somewhere how to typeset 1/2 as ½, but with arbitrary fractions (that is, a macro that takes two arguments and makes the first denominator and the second numerator...). Now I can't seem to find it.

How do I typeset an arbitrary fraction the same way as ½?

-

You want xfrac

\documentclass{article}
\usepackage{xfrac}
\begin{document}
\sfrac{12}{3}
\end{document}


There are differences between xfrac and nicefrac (detailed in the manual for xfrac), particularly if you are using fonts other than Computer Modern.

-
Thanks for the pointer to/comment about xfrac! –  topskip Sep 22 '10 at 18:28

Perhaps nicefrac is something you want to use?

\documentclass{article}
\usepackage{nicefrac}
\begin{document}
\nicefrac{12}{3}
\end{document}


-
Apparently, nicefrac works well for Computer Modern but not for other fonts (according to the xfrac documentation). –  ShreevatsaR Sep 25 '10 at 3:46

You can actually make your own pretty easily by using superscripts and subscripts: this would be a/b:

$\,^a\!/_b$


I use the following command to make it easier to use more than once:

\newcommand{\slantfrac}[2]{\,^#1\!/_#2}

-
Without having checked, I suspect that this doesn't look as nice as \xfrac or \nicefrac, as it does nothing about kerning. –  ShreevatsaR Sep 25 '10 at 3:44
@lee I have used ^1/_2; what do the other symbols do? –  David Nov 29 '11 at 22:27
What about \slantfrac{1}{10}? –  Paul Gaborit Nov 17 '12 at 7:42

If you're using XeTeX or LuaTeX with fontspec, you might be using a font that has the OpenType feature frac, which might either be limited to a certain number of fixed fractions (e.g. Junicode), or it works with any combination of digits, e.g. in Linux Libertine (libertineotf package) or EB Garamond.

Here's a MWE using libertineotf:

\documentclass{article}
\usepackage{fontspec}
\usepackage{libertineotf}
\begin{document}

1/2 3/4 5/6 7/8 9/10 11/12 31415/27182 1000/1000000

\end{document}


If the font contains the OpenType features numr (numerator) and dnom (denominator), we can get even fancier and typically include a greater variety of glyphs:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{EB Garamond 12 Regular}

\newcommand{\unifrac}[2]{\mbox{% making sure we don't get a line break
⁄% That slash is U+2044 FRACTION SLASH, which has special spacing
}}

\begin{document}

\unifrac{12}{14} \unifrac{31415}{27182} \unifrac{abc}{def} \unifrac{Foo!}{Bar?}
\unifrac{\#\\$\%+/<>=}{?\@[]\textbackslash\_|\{\}§†}

\end{document}


To find out which features your OpenType font has, see

To find our which fractions are included e.g. in Junicode's frac feature, see

To get started with Xe(La)TeX and/or Lua(La)TeX, see

-

(As there is always a solution using TikZ, it is here!)

This solution automates the usage of \frac, a modified version of \slantfrac, or \sfrac.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fpu}
\usepackage{fp}
\usepackage{parskip}
\usepackage{xfrac}
\pagestyle{empty}
\begin{document}
\pgfkeys{/pgf/number format/frac}
\pgfmathprintnumber{0.333333333333333}\hspace{1em}
\pgfmathprintnumber{0.5}\hspace{1em}
\pgfmathprintnumber{2.133333333333325e-01}\hspace{1em}
\pgfmathprintnumber{0.12}\hspace{1em}
\pgfmathprintnumber{2.666666666666646e-02}\hspace{1em}
\pgfmathprintnumber{-1.333333333333334e-02}\hspace{1em}
\pgfmathprintnumber{7.200000000000000e-01}\hspace{1em}
\pgfmathprintnumber{6.666666666666667e-02}\hspace{1em}
\pgfmathprintnumber{1.333333333333333e-01}\hspace{1em}
\pgfmathprintnumber{-1.333333333333333e-02}\hspace{1em}

\newcommand{\slantfrac}[2]{\,^{#1}\!/_{#2}}
\pgfkeys{/pgf/number format/frac TeX={\slantfrac}}
\pgfmathprintnumber{0.333333333333333}\hspace{1em}
\pgfmathprintnumber{0.5}\hspace{1em}
\pgfmathprintnumber{2.133333333333325e-01}\hspace{1em}
\pgfmathprintnumber{0.12}\hspace{1em}
\pgfmathprintnumber{2.666666666666646e-02}\hspace{1em}
\pgfmathprintnumber{-1.333333333333334e-02}\hspace{1em}
\pgfmathprintnumber{7.200000000000000e-01}\hspace{1em}
\pgfmathprintnumber{6.666666666666667e-02}\hspace{1em}
\pgfmathprintnumber{1.333333333333333e-01}\hspace{1em}
\pgfmathprintnumber{-1.333333333333333e-02}\hspace{1em}

\pgfkeys{/pgf/number format/frac TeX={\sfrac}}
\pgfmathprintnumber{0.333333333333333}\hspace{1em}
\pgfmathprintnumber{0.5}\hspace{1em}
\pgfmathprintnumber{2.133333333333325e-01}\hspace{1em}
\pgfmathprintnumber{0.12}\hspace{1em}
\pgfmathprintnumber{2.666666666666646e-02}\hspace{1em}
\pgfmathprintnumber{-1.333333333333334e-02}\hspace{1em}
\pgfmathprintnumber{7.200000000000000e-01}\hspace{1em}
\pgfmathprintnumber{6.666666666666667e-02}\hspace{1em}
\pgfmathprintnumber{1.333333333333333e-01}\hspace{1em}
\pgfmathprintnumber{-1.333333333333333e-02}\hspace{1em}
\end{document}

-