13

Is there a way to copy from a PDF to a LaTeX document? Whenever I do it, it copies in with weird symbols in the place of LaTeX commands for example:

$$f:\mathbb{R}\rightarrow \mathbb{R}$$

copies in as

f : R ! R;

I did a quick search and I couldn't really find anything (some sort of PDF to LaTeX converter that worked well)

Am I missing something obvious or can this not be done (for some reason)?

egreg
  • 1,121,712
hmmmm
  • 1,115
  • 3
    You are probably out of luck: see http://tex.stackexchange.com/questions/8503/how-to-convert-pdf-to-latex. Your question will probably be closed as a duplicate. – Ethan Bolker Dec 11 '12 at 18:32
  • Are you interested in getting useful symbols or do you really want the complete LaTeX code back? – Stephan Lehmke Dec 11 '12 at 18:40
  • 2
    For instance, if I add \input{glyphtounicode} \pdfgentounicode=1 to the preamble of the document, I get f : R → R. – Stephan Lehmke Dec 11 '12 at 18:46
  • @EthanBolker I just found out how to make it paste back as TeX code ;-) – Stephan Lehmke Dec 11 '12 at 19:40
  • 1
    @hmmmm could you disambiguate whether you want to copy LaTeX from any PDF document or want to be able to make a PDF from which LaTeX can be copied? – Stephan Lehmke Dec 11 '12 at 21:41
  • @StephanLehmke Well the first (which would of course do the second as well) however it seems that this is not really possible (I am intrigued why it can't really be done) however it seems as if the second part has been answered which is still useful. I don't think you need the @ hmmmm if your commenting on my comment btw – hmmmm Dec 11 '12 at 23:27

1 Answers1

18

Try this:

\documentclass{article}
\usepackage{amssymb}
\input{glyphtounicode}
  \pdfgentounicode=1
\usepackage{accsupp}

\newcommand\pasteablelatex[1]
{%
  \edef\next
  {%
    \noexpand\BeginAccSupp{method=escape,ActualText=\detokenize{#1}}%
  }%
  \next#1\EndAccSupp{}%
}

\begin{document}

foo

\pasteablelatex{$$f:\mathbb{R}\rightarrow \mathbb{R}$$}

bar

\end{document}
  • To be clear, can you confirm that this only works if the person who compiled the PDF used this method? I guess that without that there's no way to do this, isn't there? – Andrew Stacey Dec 11 '12 at 19:57
  • @AndrewStacey Not sure what you mean. It's a method to make PDF paste back LaTeX code. – Stephan Lehmke Dec 11 '12 at 19:59
  • I mean that if you give me a PDF without the source code then I can't use your method to recover that source code. But if I as an author want to be kind to readers (assuming that there are some) then I can use this to make my PDF so that copying gives the LaTeX code. – Andrew Stacey Dec 11 '12 at 20:05
  • Exactly. It has to be used when compiling the LaTeX to PDF. – Stephan Lehmke Dec 11 '12 at 20:05
  • 4
    Is there a way to make all the document pasteable? – Manuel Dec 11 '12 at 20:23
  • @Manuel From what I understand about the mechanism behind accsupp, this won't be easy. It's really meant for giving an alternate text analogous to HTML. So even using it on a whole math display is abuse (you'll notice you get a strange copy highlight). \pasteablelatex should really be used on small simple chunks of text. – Stephan Lehmke Dec 11 '12 at 20:31
  • Ok, I understand. And may be not absolutely related, but... is there any way to do the same but with LaTeX source code? I mean, instead of f : R → R you get f:\mathbb{R}\rightarrow \mathbb{R}. – Manuel Dec 11 '12 at 20:36
  • @Manuel Did you try my answer at all? Maybe it only works with Acrobat. – Stephan Lehmke Dec 11 '12 at 20:37
  • I wrote your code, copied everything and got this foo bar f:R→R 1. Ok, as you said, may be it only works with Acrobat, I'm not using it. – Manuel Dec 11 '12 at 20:39
  • This is fantastic. Windows: works with Adobe, not with SumatraPDF or FixIt. It seems worth asking for as a SumatraPDF feature, as that's the only reader that doesn't lock files. But I'm not sure exactly what to ask for... – Mohan Dec 12 '12 at 00:32
  • It worked in Evince (3.6.1 Linux) and Acrobat Reader (9.4.1 Linux). Interestingly enough, even partial portions of the code may be copied and pasted from Evince. (In Acrobat it is not possible to select only part of the equation). – alfC Apr 22 '13 at 01:11