# Copyable math formulas

I'm curious whether there is a package that allows to copy math formulas correctly.

For example, this one should highlight like so

but, not like so

And if I copy one, I should get:

∫₃⁵ (x/(1-x²)) dx


If there is no such a package, I'm looking for an any manual way of doing this.

For example, I can make a phantom text to make the selection highlight correct. But I don't know how to get custom text by copying.

• This reference is not what you are asking for: tex.stackexchange.com/questions/233390/… . However, it may be adaptable for what you need, in that it allows you to copy the tex code from a PDF's equation and paste that. May 20, 2020 at 12:43
• Beyond what I have said above, to do what you are asking may not be possible, because there will be some math (symbols, formats) for which an easy inline pasted portrayal is not possible. This unusual answer of mine, tex.stackexchange.com/questions/332012/… , attempts to do almost the opposite of what you are asking, which is to convert a custom math format into equivalent TeX code (rather than converting TeX code into a custom math format) May 20, 2020 at 12:51
• There is the added problem that thecopyable text inside a PDF is does not cover the full unicode spectrum. Example, which one might think would give the desired behavior: \documentclass{article} \usepackage{accsupp} \begin{document} \BeginAccSupp{method=escape,ActualText={∫₃⁵ (x/(1-x²)) dx}} $\int_3^5\frac{x}{1-x^2}\,dx$ \EndAccSupp{} \end{document} When copy paste is applied to the PDF, the following is the result: ^53X03 965 (x/(1-x²)) dx, such that the non-ASCII things do not represent themselves accurately. May 20, 2020 at 14:49
• @StevenB.Segletes I have the latest version of accsupp, but it doesn't work, I still copy the actual text. Do you know why the problem may occur? May 20, 2020 at 18:20
• I don't think the problem is accsupp, but PDF itself. It is limited in what it can copy/paste...while it can draw a Greek letter, for example, on the visual paper, it does not have the means to store it in the underlying information, I think. May 20, 2020 at 19:03

I managed to do that! Thanks for this answer.

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

\usepackage{tikz}
\usepackage{stringenc}
\usepackage{pdfescape}
\usetikzlibrary{calc}
\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}

\begin{tikzpicture}[remember picture,overlay]
\node[text opacity=0]at ($(current page.north west)+(70.5ex,-37ex)$){
\BeginAccSuppUnicode{∫₃⁵ (x/(1-x²)) d}
\scalebox{14}[7.2]{x\rule{1pt}{0pt}}
\EndAccSupp{}
\hspace{-23pt}\scalebox{0.1}[7.2]{x}
};
\end{tikzpicture}

$\int_3^5\frac{x}{1-x^2}\,dx$

\end{document}


## Edit

Found another solution that works the same. Although it requires you to manualy convert all symbols to unicode, it is way more compact.

\documentclass{article}
\usepackage{accsupp}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[remember picture,overlay]
\node[text opacity=0]at ($(current page.north west)+(70.5ex,-37ex)$){
\BeginAccSupp{method=hex,unicode,ActualText=222b20832075 00280078002f00280031002d007800b200290029 0064}
\scalebox{14}[7.2]{x\rule{1pt}{0pt}}
\EndAccSupp{}
\hspace{-23pt}\scalebox{0.1}[7.2]{x}
};
\end{tikzpicture}
`