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

1
  • There is a simple plain tex solution too. See my answer below
    – ikrabbe
    Jun 19, 2015 at 11:48

7 Answers 7

122

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.

4
  • Thanks. I still use nicefrac today ... I should stop doing that! Apr 6, 2017 at 11:31
  • 4
    Note that \sfrac looks nice only in normal text, not in math mode (at least in my case it is like that). Apr 29, 2017 at 16:45
  • Unfortunately, sfrac, xfrac, nicefrac do not work with MathJax.
    – Avatar
    May 2, 2018 at 10:50
  • 3
    This is an alternative (if you depend on Mathjax): tex.stackexchange.com/a/251123/63540
    – Avatar
    May 2, 2018 at 11:01
40

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

2
  • 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, 2016 at 18:39
  • 1
    Can you still get this with LuaLaTeX (as opposed to XeLaTeX)?
    – cfr
    Apr 5, 2017 at 1:30
27

Perhaps nicefrac is something you want to use?

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

output

1
  • 7
    Apparently, nicefrac works well for Computer Modern but not for other fonts (according to the xfrac documentation). Sep 25, 2010 at 3:46
23

A simple plain TeX construct produces very nice results too:

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

rendered

4
  • 2
    Nice! It also works with fraction over fraction, a bit small though, but good enough. See example
    – Avatar
    May 2, 2018 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, 2019 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, 2019 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. May 2, 2020 at 6: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}
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.

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}}
3
  • 4
    Without having checked, I suspect that this doesn't look as nice as \xfrac or \nicefrac, as it does nothing about kerning. Sep 25, 2010 at 3:44
  • @lee I have used ^1/_2; what do the other symbols do? Nov 29, 2011 at 22:27
  • 3
    What about \slantfrac{1}{10}? Nov 17, 2012 at 7:42

You must log in to answer this question.

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