5

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

For example, this one should highlight like so enter image description here

but, not like so

enter image description here

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.

antshar
  • 4,238
  • 9
  • 30
  • This reference is not what you are asking for: https://tex.stackexchange.com/questions/233390/in-which-way-have-fake-spaces-made-it-to-actual-use . 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. – Steven B. Segletes May 20 '20 at 12:43
  • 1
    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, https://tex.stackexchange.com/questions/332012/translate-in-line-equations-to-tex-code-any-package/332061#332061 , 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) – Steven B. Segletes May 20 '20 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. – Steven B. Segletes May 20 '20 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? – antshar May 20 '20 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. – Steven B. Segletes May 20 '20 at 19:03
  • Actually, I found you that the problem was with my pdf viewer. The other one copies what I've set via the package. In fact, my document doesn't want to compile if there are such characters,,,² in ActualText parameter. I'm getting this error: TeX capacity exceeded, sorry [input stack size=5000]. } – antshar May 20 '20 at 20:03
  • To use non-ASCII characters in input, you have to compile with xelatex or lualatex, not pdflatex. – Steven B. Segletes May 20 '20 at 20:08

1 Answers1

7

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}

enter image description here


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}
antshar
  • 4,238
  • 9
  • 30