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

  • There is a simple plain tex solution too. See my answer below – ikrabbe Jun 19 '15 at 11:48
up vote 63 down vote accepted

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.

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

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

  • 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
  • @Alborz Thanks :) – doncherry Jan 27 '16 at 20:12
  • Can you still get this with LuaLaTeX (as opposed to XeLaTeX)? – cfr Apr 5 '17 at 1:30

Perhaps nicefrac is something you want to use?

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

output

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

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

A simple plain TeX construct produces very nice results too:

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

rendered

  • Nice! It also works with fraction over fraction, a bit small though, but good enough. See example – Kai Noack May 2 at 11:00

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
    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 LeBauer Nov 29 '11 at 22:27
  • 3
    What about \slantfrac{1}{10}? – Paul Gaborit Nov 17 '12 at 7:42

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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.