This answer from a similar question gets very close to a final copy/paste solution, but does not work for me as it does not select whitespace. I am asking a new question as the linked one is several years old, and my question focuses specifically on the copy/paste of whitespace in listings, preferably in a cross-platform solution. That is the resulting pdf will allow copy/paste from the document into an editor on windows/mac/linux, using acrobat/evince/ocular/etc. If cross platform is this sense is not realistic, I would at least like this capability on linux with evince if it is possible.

I am working on a Fedora 29 machine and need to be able to cut and paste python snippets from the pdf into an editor, and be able to run the snippet without manual edits. This is critical with python snippets as code blocks are specified by changes in whitespace indentation.

Here is my minimal working example (MWE).


 % Patch accsupp to avoid copying line numbers when copying from listing



Listing~\ref{lst:example} shows an example code snippet.

     theorem={Listing}{listings}{Example Listing}{lst:example},
     listing engine=minted,
     minted language=python,
     minted options={%
           \fill[gray!25] (frame.south west) rectangle ([xshift=4mm]frame.north west);
     breakable, enhanced, listing only}
import os
import sys

SCRIPT_PATH = os.path.abspath(os.path.dirname(__file__))

class CopyPasteTest:
    Testing that copy/paste works from pdf listings.
    def __init__(self, numbers):
        for number in numbers:
            print number

if __name__ == "__main__":
    CopyPasteTest([1, 2, 3, 4])


Which results in the following pdf.

enter image description here

But when I select text in evince, you can see it does not highlight white space.

enter image description here

And when pasted into gedit, has lost its indentation and empty lines, which breaks the example.

enter image description here

Any help getting python code snippets to copy/paste out of a pdf into another editor would be greatly appreciated!

  • 1
    Welcome to TeX.SE! Nice first question with MWE! – Kurt Jan 27 at 15:24
  • @Kurt, Thanks for the welcome. Everyone should do their homework before posting a question. – Kenneth E. Bellock Jan 27 at 17:22

In my opinion it won't work reliably. The pdf viewers don't respect space chars and don't handle them in the same way. E.g. if you compile this document with lualatex (pdflatex gives similar results):

x\ \pdffakespace\ \pdffakespace\ \pdffakespace\ \pdffakespace    y

Then you get in the pdf four real space glyphs (0067 is the space):


But copy & paste (adobe + sumatra) gives only one space: x y.

I also tried with /ActualText (you could use accsupp for something similar):

\tagmcbegin{tag=P,raw={/ActualText <FEFF007800200020002000200079>}}
\verb+x     y+

This copies correctly with adobe but not with sumatra.

Similar differences/problems are mentioned here How to change character code in Type1 font?.

Better embed/attach the code instead of relying on copy & paste.

  • Thank you for the answer and low level explanation. It's unfortunate this is how it is. The use case I have to satisfy requires a one file submission, text formatting, and copy/paste of code snippets. I can satisfy the requirements with Word, but not LaTeX... that hurts... – Kenneth E. Bellock Jan 27 at 17:29
  • The problem is not latex but pdf. Does it work if you save your word file as pdf? – Ulrike Fischer Jan 27 at 17:36
  • Thank you for the correction. It is unfortunate that this is how pdf is. I'm guessing it would also not work if word was exported to pdf, but I don't have to export to pdf from word to satisfy the requirements of "one file submission, text formatting, and copy/paste of code snippets". I'm not trying to praise the merits of word here, I'm trying to get away from it. Once you get used to using LaTeX it's hard to go back to word for anything, it's like telling a well trained painter they can only use crayola crayons... – Kenneth E. Bellock Jan 27 at 18:45
  • Well you could submit the tex-file. It fullfills all your requirements too .... Or create an docx from latex with tex4ht. – Ulrike Fischer Jan 27 at 18:59
  • LaTeX .tex files do not have "text formatting" until it is rendered to another format, so that requirement would fail. I have been researching pandoc, latex2rtf, latex4ht and more. None of those toolchains have given me everything I need yet. – Kenneth E. Bellock Jan 27 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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