# How to make slash (/) paste with correct Unicode codepoint depending on context?

Unicode offers different characters for the slash:

• U+002F (/) for the ordinary slash
• U+2215 (∕) for the mathematical division operator
• U+2044 (⁄) to designate vulgar fractions

How can I have these automatically paste with the right Unicode codepoint? Some starter code is this:

\documentclass{article}
\input glyphtounicode
\pdfgentounicode=1
\usepackage[T1]{fontenc}
\usepackage{nicefrac}

\begin{document}

textual: w/o (intended: U+002F)

in an equation: $$12/4 = 3$$ (intended: U+2215)

in a vulgar fraction (produced by package \texttt{nicefrac}): 1\nicefrac{1}{2} (intended: U+2044)

\end{document}


Note that "-" in math mode already pastes correctly as the "minus sign" (U+2212, "−"), so that is why I suspect that addressing this issue is possible.

-

The three different slashes all use the same glyph name slash in the PDF file. Therefore only one mapping can be set via feature \pdfgentounicode. The PDF format provides another feature ActualText that can be used with package accsupp. The example lets the normal slash untouched, it is mapped to U+002F via \pdfgentounicode/glyphtounicode.tex. The other two are defined as macros. At some stage macros are necessary for accsupp, because it must put some markup in the PDF page contents stream before and after the glyph.

\documentclass{article}

\pdfgentounicode=1
\input{glyphtounicode}
\usepackage{accsupp}
\usepackage{nicefrac}
\usepackage{etoolbox}

% U+2215
\newcommand*{\mathdivide}{%
\ensuremath{%
\BeginAccSupp{method=hex,unicode,ActualText=2215}/\EndAccSupp{}%
}%
}
% U+2044
\newcommand*{\fractionslash}{%
\ensuremath{%
\BeginAccSupp{method=hex,unicode,ActualText=2044}/\EndAccSupp{}%
}%
}

% Patch nice frac macros to insert the right slashes
\makeatletter
\expandafter\patchcmd\csname \@backslashchar @UnitsUglyFrac \endcsname
{/}{\mathdivide}{%
\@latex@info@no@line{Successful patched \string\@UnitsUglyFrac}%
}{%
\@latex@error{Could not patch \string\@UnitsUglyFrac}%
}

\expandafter\patchcmd\csname \@backslashchar @UnitsNiceFrac \endcsname
{/}{\fractionslash}{%
\expandafter\patchcmd\csname \@backslashchar @UnitsNiceFrac \endcsname
{/}{\fractionslash}{%
\@latex@info@no@line{Successful patched \string\@UnitsNiceFrac}%
}{%
\@latex@error{Could not patch \string\@UnitsNiceFrac}%
}%
}{%
\@latex@error{Could not patch \string\@UnitsNiceFrac}%
}
\makeatother

\begin{document}
/ $a\mathdivide b$
\nicefrac{a}{b} $\nicefrac{a}{b}$
\end{document}


The result from copy and paste:

• "/ a∕b a⁄b a⁄b"
• "/ a\u2215b a\u2044b a\u2044b" (escape notation)
-
I know that this is advertising, but with xpatch instead of etoolbox you can more simply say \xpatchcmd{\@UnitsNiceFrac}{...}{...}{...}{...} – egreg Sep 20 '12 at 20:08
Thanks, nice to know. – Heiko Oberdiek Sep 20 '12 at 21:05
Thanks. The patch for nicefrac is great. I was wondering whether there is a way to simply write / instead of defining a new macro \mathdivide using accsupp. The reason I suspect such a solution could be possible is that the hyphen-minus is somehow being treated in two different ways by (La)TeX depending on the mode, leading to two different pastable Unicode characters in the output. So there might be a way to patch/amend TeX internals? – Lover of Structure Sep 21 '12 at 17:55
The hyphen and minus sign are different glyphs usually (the minus sign is longer). – Heiko Oberdiek Sep 21 '12 at 18:24
Yes -- What I mean is that I can input the same character "hyphen-minus" (-) in the source; when it is in text mode it will paste as the "hyphen-minus", but when it is in math mode it will paste as the "minus sign". That means that LaTeX has a mechanism by which it treats the same input character in a way that leads to two different, context-dependent output pastings. The fact that both slashes look the same (assuming that they do) shouldn't be an obstacle in theory: perhaps one can define two characters generated by textmode-/ and mathmode-/ that look the same but paste differently. – Lover of Structure Sep 21 '12 at 22:31