[Tex/LaTex] How to produce unicode copyable alternative text? (with accsupp)

accsuppcopy/pastepdfunicodeviewers

In the following example, I can't manage to make accsupp store the unicode alternative text. The resulting PDF looks as expected but the copyable text is not the correct one. I tried this with different accsupp options: method=plain,...=escape, ...=pdfstringdef, and unicode. (accsupp manual is here). I tried with Acrobat 9.4.1 (Linux) and Evince. I am using LuaLaTeX to compile.

The idea is that when I select the equation in the next document the text ⅆa is copied. (In most cases only a gets copied.)

\documentclass{article}
\usepackage{fontspec}
\usepackage{accsupp}

\begin{document}

\BeginAccSupp{unicode,ActualText=ⅆa} %method=plain, ...=escape, ...=pdfstringdef
$da$
\EndAccSupp{}

\end{document}

Best Answer

Big chars (characters with more than 8 bits) of LuaTeX/XeTeX are not yet fully supported. Workaround:

\documentclass{article}
\usepackage{fontspec}
\usepackage{accsupp}
% \usepackage{luatex85} % might be needed as a workaround https://github.com/ho-tex/oberdiek/issues/36

\usepackage{tikz}%\usepackage{pgfmath}
\usepackage{stringenc}
\usepackage{pdfescape}

\makeatletter
\newcommand*{\BeginAccSuppUnicode}[1]{%
  \EdefSanitize\asu@str{#1}%
  \edef\asu@str{%
    \expandafter\expandafter\expandafter\asu@ToSpaceOther
    \expandafter\asu@str\space\@nil
  }%
  \expandafter\let\expandafter\asu@str\expandafter\@empty
  \expandafter\asu@ToHexUC\asu@str\relax
  \EdefUnescapeHex{\asu@str}{\asu@str}%
  \StringEncodingConvert{\asu@str}{\asu@str}{utf32be}{utf16be}%
  \EdefEscapeHex{\asu@str}{\asu@str}%
  \BeginAccSupp{%
    unicode,%
    method=hex,%
    ActualText=\asu@str
  }%
}
\begingroup
  \lccode`\9=`\ %
\lowercase{\endgroup
  \def\asu@SpaceOther{9}%
}
\def\asu@ToSpaceOther#1 #2\@nil{%
  #1%
  \ifx\\#2\\%
    \expandafter\@gobble
  \else
    \asu@SpaceOther
    \expandafter\@firstofone
  \fi
  {\asu@ToSpaceOther#2\@nil}%
}
\def\asu@ToHexUC#1{%
  \ifx#1\relax
  \else
    \pgfmathHex{\the\numexpr`#1+"10000000\relax}%
    \edef\asu@str{%
      \asu@str
      0\expandafter\@gobble\pgfmathresult
    }%
    \expandafter\asu@ToHexUC
  \fi
}
\makeatother

\begin{document}

\BeginAccSuppUnicode{ⅆa}
$da$
\EndAccSupp{}

\end{document}