Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to write a .pdf, where my readers could copy the rendered formulae and paste them directly in their TeX documents.

Example formula

And the copied text would automatically be:

\int x\sin ax\;\mathrm{d}x = \frac{\sin ax}{a^2}-\frac{x\cos ax}{a}+C

Is this possible?

share|improve this question
10  
I added the accessibility tag here- I know it may not have been the original intention, but your question has very important connotations in this respect. –  cmhughes Jun 18 '13 at 22:36

1 Answer 1

up vote 117 down vote accepted

The PDF format supports a feature "ActualText" that is used for copy-paste instead of the actual typeset text. However, it is not supported by all PDF viewers, but Acrobat Reader does support it.

\documentclass{article}
\usepackage{accsupp}

\newcommand*{\copyable}[1]{%
  \BeginAccSupp{%
    ActualText=\detokenize{#1},%
    method=escape,
  }%
  #1%
  \EndAccSupp{}%
}

\begin{document}
\[
  \copyable{\int x\sin ax\;\mathrm{d}x = \frac{\sin ax}{a^2}-\frac{x\cos ax}{a}+C}
\]
\end{document}

Result

Copy-paste result (\detokenize adds spaces after command names):

\int x\sin ax\;\mathrm {d}x = \frac {\sin ax}{a^2}-\frac {x\cos ax}{a}+C

A more verbatim-like copy is much more complicated:

\documentclass{article}
\usepackage{accsupp}

\makeatletter
\newcommand*{\copyable}{%
  \begingroup
  \@sanitize
  \catcode`\%=14 % allow % as comment char, also needed for \%
  \@copyable
}
\newcommand*{\@copyable}[1]{%
  \endgroup
  \BeginAccSupp{%
    ActualText=\detokenize{#1},%
    method=escape,
  }%
  \scantokens{#1}%
  \EndAccSupp{}%
}
\makeatother

\begin{document}
\[
  \copyable{\int x\sin ax\;\mathrm{d}x = \frac{\sin ax}{a^2}-\frac{x\cos ax}{a}+C}
\]
\end{document}

Copy-paste result:

\int x\sin ax\;\mathrm{d}x = \frac{\sin ax}{a^2}-\frac{x\cos ax}{a}+C

However, the argument of \copyable is read with verbatim catcodes. Therefore this trick will not work, if \copyable is inside an argument of another macro (or in environments of package amsmath). In this case the \detokenize has a function to get the previous version with spaces after command names at least.

Discussion about areas

\copyable inserts whatsits that does not influence the mathematical spacing. Thus the whole equation can be split:

\[
  \copyable{\int x\sin ax\;\mathrm{d}x}
  \copyable{=}
  \copyable{\frac{\sin ax}{a^2}}
  \copyable{-}
  \copyable{\frac{x\cos ax}{a}}
  \copyable{+}
  \copyable{C}
\]

And where the mapping from formula to copy-paste text is correct, \copyable can be omitted:

\[
  \copyable{\int x\sin ax\;\mathrm{d}x}
  =
  \copyable{\frac{\sin ax}{a^2}}
  -
  \copyable{\frac{x\cos ax}{a}}
  + C
\]

The area that is shown in the PDF viewer for selecting depends on the formula and especially the PDF viewer.

Acrobat Reader usually only shows the area above the first symbol of the formula. In case of the integral sign, a small rectangle above it. The latest example with AR9/Linux:

AR selecting all

Evince 3.4.0 is a little better in the coverage of the rectangle. The first equation with the whole equation in \copyable:

Evince selecting the whole equation

But Evince has problems in the separation of the equations. Here I wanted to select the second equation only, but it gets mixed up with the third:

Evince selecting second equation

Okular 0.14.3 has a nicer selecting tool, but if the \copyable is split up into terms (second and third equations of the latest example), then the terms get mixed up. - Fatal for mathematicians.

share|improve this answer
    
Would it be possible to avoid this problem using a LuaTeX solution? (I am by all means not a LuaTeX expert, so I am just asking) –  Federico Poloni Jun 18 '13 at 11:59
    
@FedericoPoloni: Not really, LuaTeX does not change the way TeX reads it input and converts it to tokens. –  Heiko Oberdiek Jun 18 '13 at 12:09
3  
Just checked: it is also copyable in okular and evince viewers, even better than in Acrobat Reader. A very useful feature indeed. –  g.kov Jun 18 '13 at 14:09
1  
Would it possible to match the actual formular symbols with the area one can select? In Adobe Reader the area I am able to select in order to copy the formular in the MWE, is above the formular and only very small. Or is this only an Adobe Reader problem? –  maetra Jun 19 '13 at 9:32
2  
Is there a solution that works with Preview in Mac OS X? –  lhf Jun 19 '13 at 17:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.