# How to print \frac{1}{2} by a single Unicode character?

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?

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

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.

-
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
@Igor That's what you asked! Of course it's better not to abuse the existing command. – egreg Sep 21 '11 at 7:55
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
@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\/\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

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
\begin{document}
\noindent Here are some vulgar fractions: \textonehalf, \textonequarter,
\textthreequarters.

\noindent Here are some built vulgar fractions:  \nicefrac{1}{2},
\nicefrac{1}{4}, \nicefrac{3}{4}, \nicefrac{11}{7}.
\end{document}


-

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?

-
Haha, \ifmodern ;-) – ℝaphink Sep 19 '11 at 18:43
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

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.

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