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

share|improve this question
add comment

5 Answers

up vote 37 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.

share|improve this answer
    
Thanks for the pointer to/comment about xfrac! –  topskip Sep 22 '10 at 18:28
add comment

Perhaps nicefrac is something you want to use?

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

output

share|improve this answer
3  
Apparently, nicefrac works well for Computer Modern but not for other fonts (according to the xfrac documentation). –  ShreevatsaR Sep 25 '10 at 3:46
add comment

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

share|improve this answer
add comment

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}
share|improve this answer
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 Nov 29 '11 at 22:27
1  
What about \slantfrac{1}{10}? –  Paul Gaborit Nov 17 '12 at 7:42
add comment

(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}
share|improve this answer
add comment

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.