Saturday, December 28, 2013

Better copy - paste in LaTeX generated PDF files

Research papers are often read in pdf format, with a lot of equations. One annoying fact is that copying the pdf equation and pasting it elsewhere makes almost no sense.

For example, it would be better to copy equations in the pdf file directly in the LaTeX source form, and then it can be pasted into presentations, using beamer, or via plugins to PowerPoint or LibreOffice.

As another example, as I am updating a manual for Mathematica code, it would be very useful for the user to copy the equations (which are Mathematica code) into Mathematica directly.

The solution is mentioned here and tested to work well.

\usepackage{accsupp}

\begin{equation}
\BeginAccSupp{ActualText={The text to paste}}
The text to display
\EndAccSupp{}
\end{equation}

Here \begin{equation} and \end{equation} are not necessary -- if advanced copy - paste is needed  outside the math environment, this can and should be omitted.

As pointed out by Oberdiek, if the paste text is the LaTex source code (to be used, for example, in making presentations), one can further ease the above process by

\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}

No comments:

Post a Comment