29

I am looking for a command which would print simple fractions like 1/2, 1/3 etc using glyphs available in modern TTF/OTF fonts. Ideally, I want this command to output ordinary \frac{1}{2}, \frac{1}{3} in case if my document is compiled with (PDF)LaTeX, but outputs something like $\text{\char"00BD}$ in case if the compiler is XeLaTeX of LuaLaTeX (and fontspec and / or unicode-math is loaded) and required Unicode character is available in current font.

Is such a command already invented by somebody?

  • 5
    The xfrac package typesets "text" style fractions. It does not use complete Unicode characters, but, because of that, is able to handle arbitrary numerator and denominator values. – Andrey Vihrov Sep 19 '11 at 9:15
  • 2
    If you are using an editor that supports Unicode you might like to look at the newunicodechar package: this supports XeLaTeX, LuaLaTeX or (pdf)LaTeX (using the inputenc package along with the utf8 option). – mas Sep 19 '11 at 18:22
  • 1
    @mas: Unfortunately, WinEdt does not support unicode at such a level. It seems to convert UTF8 encoded files to ANSI at read|write stage. – Igor Kotelnikov Sep 21 '11 at 8:13
9

You might try

\documentclass[a4paper]{article}
\usepackage{ifxetex,ifluatex}

\newif\ifunicode
\ifxetex\unicodetrue\else\ifluatex\unicodetrue\fi\fi 

\ifunicode
  \usepackage{unicode-math}
  \setmainfont[Ligatures=TeX,Fractions=On]{XITS}
  \setmathfont{XITS Math}

  \makeatletter
  \let\@@@frac\frac
  \def\frac#1#2{\sbox0{#1/#2}\sbox2{#1\phantom{/}#2}%
    \ifdim\wd0=\wd2 % no corresponding character
      \@@@frac{#1}{#2}%
    \else
      \mbox{#1/#2}%
    \fi}
  \makeatother
\fi

\begin{document}
1/2$\frac{1}{2}\frac{12}{11}$
\end{document}

Here I assume that a vulgar fraction width is different than the width of the slashed form, which seems rather likely to happen.

However I would advise not to use vulgar fractions in a mathematical context.

  • 1
    Perhaps, it is better to introduce new command rather than touch \frac or modify \tfrac from amsmath. – Igor Kotelnikov Sep 21 '11 at 7:53
  • 2
    @Igor That's what you asked! Of course it's better not to abuse the existing command. – egreg Sep 21 '11 at 7:55
  • 1
    There is another solution: $\mbox{1/2}$. If amsmath is loaded than $\text{1/2}$ is better. By the way, if Fractions is on, how one can break ligatures and obtain ordinary 1/2? I tried 1/{}2, 1{/}2 etc but did not succeed. The only way i found is $1/2$. – Igor Kotelnikov Sep 21 '11 at 8:02
  • 1
    @Igor This is not a solution, as it will render with the slash if the fraction is not among the available characters. To disable the ligature in text mode, use 1/\/2 :) – egreg Sep 21 '11 at 8:17
  • 1
    1\/\2 prints both 1 and 2 in position of superscrips though 2 should be in position of subscript. Secondly, I did not manage to find a fraction which would activate first brach (\@@@frac{#1}{#2}) of your code. Even \frac{1}{Q} takes second branch (\mbox{#1/#2}) but outputs Q in larger size than 1. It seems that comparing dimentions (\ifdim\wd0=\wd2) on my computer (MiKTeX 64bit) always evaluates to false. And \ifdim\wd0<\wd2 is always evaluated to true. – Igor Kotelnikov Sep 22 '11 at 3:38
23

This answers your question title, but not your explanation (which seems to imply that you cannot use vulgar fractions with pdfLaTeX). The textcomp package makes available \textonehalf, \textonequarter, and \textthreequarters. If your font and chosen encoding supports these, then they can be used directly. (If your font does not support these, however, you will get an error message like "\textonequarter" unavailable in encoding OT1" or a strange symbol.) This works with pdfLaTeX:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{mathpazo}
\usepackage{textcomp}
\usepackage{nicefrac} % For comparison
\usepackage{xfrac}    % Works better with other fonts
\begin{document}
\noindent Here are some vulgar fractions: \textonehalf, \textonequarter,
\textthreequarters.

\noindent Here are some \texttt{nicefrac} fractions:  \nicefrac{1}{2},
\nicefrac{1}{4}, \nicefrac{3}{4}, \nicefrac{11}{7}.

\noindent Here are some \texttt{xfrac} fractions:  \sfrac{1}{2},
\sfrac{1}{4}, \sfrac{3}{4}, \sfrac{11}{7}.
\end{document}

Example of vulgar fractions with Palatino.

Notice that, although the xfrac fractions look good, they do not have the same weight as the vulgar fractions designed with the font. Unfortunately, textcomp only seems to provide access to these three fractions. If you use LuaTeX or XeTeX, then you may be able to access additional glyphs provided in your font as described by doncherry.

  • 1
    Note that this isn't available in math-mode. – Nikana Reklawyks Sep 16 '16 at 8:24
  • 2
    '\textonehalf' seems to remove all subsequent spaces, what am I doing wrong? edit: writing 'spin-{\textonehalf}' fixed it – gen Jan 19 '18 at 7:41
  • 2
    @gen Many LaTeX commands will eat subsequent spaces as they look for arguments. See this answer for solutions. In short \textonehalf{}, or as you did, or wrap the command with the xspace package. – mforbes Feb 1 '18 at 5:18
  • 1
    @mforbes Is there a list available of which fractions textcomp manages? – Lars Abrahamsson Jul 28 '18 at 18:39
  • 1
    @LarsAbrahamsson Looking at the source, I believe that ½, ¼, and ¾ are the only ones supported. – mforbes Aug 1 '18 at 5:42
5

Do you mean something like the following?

\documentclass{article}
\usepackage{amsmath}

\usepackage{iftex}
\newif\ifmodern\modernfalse
\ifXeTeX\moderntrue\fi
\ifLuaTeX\moderntrue\fi

\ifmodern
    \usepackage{unicode-math}
    \setmainfont[Ligatures=TeX,
        Extension=.otf,
        UprightFont= *-regular,
        BoldFont=*-bold,
        ItalicFont=*-italic,
        BoldItalicFont=*-bolditalic]{xits}
    \setmathfont{xits-math.otf}
\else
    \usepackage[T1]{fontenc}
\fi


\ifmodern
    \newcommand\half{\text{\char"00BD}}
\else
    \newcommand\half{\ensuremath{\frac12}}
\fi

\begin{document}
$a\half b$\half
\end{document}

Or do you want to redefine \frac itself?

  • 3
    Haha, \ifmodern ;-) – ℝaphink Sep 19 '11 at 18:43
  • 2
    This is almost what I'd like to have. But final goal is a command like \xfrac{nominator}{demumerator} which would analise its arguments and outputs corresponding \char's or ordinary \frac depending on availability of required glyphs. – Igor Kotelnikov Sep 21 '11 at 5:20
5

The answer of @mforbes is correct, however, it can be simplified:

  • The mathpazo package is not required.
  • For XeLaTeX, the fontspec package is enough and textcomp package is not required.

Based on these, the minimal examples are the following:

For pdfLaTeX:

% !TeX program = pdflatex
\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{lmodern}

\begin{document}
    \textonehalf \textonequarter \textthreequarters
\end{document}

For XeLaTeX:

\documentclass{article}

\usepackage{fontspec}

\begin{document}
    \textonehalf \textonequarter \textthreequarters
\end{document}

The result:

enter image description here

  • 1
    It there a list of available fractions available somewhere – Lars Abrahamsson Jul 28 '18 at 18:44
3

Not sure if this is what you want but an approach such as

\documentclass{article}

\usepackage{ucs}
\usepackage[utf8x]{inputenc}

\begin{document}
\unichar{"00BD}

\end{document}

compiles with both pdftex and xelatex.

  • 1
    What id I don't want to load usc and inputenc? Where is \unichar defined? And how it differs from \char? – Igor Kotelnikov Sep 21 '11 at 5:14
  • 1
    If you don't want to use usc and inputenc with utf8x (or at least utf8) then this approach is not for you. To quote from the unicode package documentation: "The utf8x.def definition file for use with inputenc covers a wider range of Unicode characters than does utf8.def in the LaTeX distribution. The ucs package provides facilities for efficient use of large sets of Unicode characters." In the example given \unichar is defined in ucs.sty (see the definition of uni@char). – mas Sep 21 '11 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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