116

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

Tomas Aschan
  • 15,528

7 Answers7

121

You want xfrac

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

output

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

doncherry
  • 54,637
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
38

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}
\addfontfeatures{RawFeature=+frac}

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

\end{document}

output

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
    {\addfontfeatures{RawFeature=+numr}#1}%
    ⁄% That slash is U+2044 FRACTION SLASH, which has special spacing
    {\addfontfeatures{RawFeature=+dnom}#2}%
    }}

\begin{document}

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

\end{document}

output

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

doncherry
  • 54,637
  • I wish I could favourite this answer because it a) has a great new command, and b) provides great resources as well. 10/10, would upvote again – Alborz Jan 27 '16 at 18:39
  • 1
    Can you still get this with LuaLaTeX (as opposed to XeLaTeX)? – cfr Apr 05 '17 at 01:30
27

Perhaps nicefrac is something you want to use?

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

output

doncherry
  • 54,637
topskip
  • 37,020
  • 7
    Apparently, nicefrac works well for Computer Modern but not for other fonts (according to the xfrac documentation). – ShreevatsaR Sep 25 '10 at 03:46
23

A simple plain TeX construct produces very nice results too:

${}^1{\mskip -5mu/\mskip -3mu}_2$

rendered

Daniel Lyons
  • 2,031
  • 14
  • 23
ikrabbe
  • 603
  • 2
    Nice! It also works with fraction over fraction, a bit small though, but good enough. See example – Avatar May 02 '18 at 11:00
  • Yes that's a bit small, but of course 1/g(x) / 1/f(x) should be written as f(x)/g(x) ;) – ikrabbe Feb 27 '19 at 11:16
  • BTW: keeping the example in plain TeXnique I would prefer {1 \over g(x)} over \frac{1}{g(x)} – ikrabbe Feb 27 '19 at 11:18
  • This is great. If your compiler can handle unicode correctly (which I think means you're using XeTeX or LuaTeX, but not sure), the Fraction Slash character (U+2044) is even better than an ordinary slash. – BallpointBen May 02 '20 at 06:13
8

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

enter image description here

\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}
Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
7

\textfrac{1}{2} from Todd Lehman, Improved kerning in fractions?, might be another alternative for you. You can also find a graphical comparison of \nicefrac and \sfrac there.

Stephen
  • 14,890
4

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}}