# How to configure tcblisting with minted to allow copy/paste from listing with whitespace characters?

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).

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{hyperref}
\tcbuselibrary{theorems,listings,skins,breakable,minted}

% Patch accsupp to avoid copying line numbers when copying from listing
\usepackage{accsupp}
\newcommand\emptyaccsupp[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}
\let\theHFancyVerbLine\theFancyVerbLine
\def\theFancyVerbLine{\rmfamily\tiny\emptyaccsupp{\arabic{FancyVerbLine}}}

\newcounter{listings}

\begin{document}

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

\begin{tcblisting}{%
theorem={Listing}{listings}{Example Listing}{lst:example},
fonttitle=\scriptsize\bfseries,
listing engine=minted,
minted language=python,
minted options={%
linenos,
breaklines,
autogobble,
fontsize=\small,
numbersep=2mm,
baselinestretch=1},
overlay={%
\begin{tcbclipinterior}
\fill[gray!25] (frame.south west) rectangle ([xshift=4mm]frame.north west);
\end{tcbclipinterior}},
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])
\end{tcblisting}

\end{document}


Which results in the following pdf.

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

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

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

• Welcome to TeX.SE! Nice first question with MWE! – Mensch Jan 27 at 15:24

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):

\documentclass{article}
\usepackage{tagpdf}
\tagpdfsetup{activate-mc,uncompress,interwordspace=true}
\begin{document}
x\ \pdffakespace\ \pdffakespace\ \pdffakespace\ \pdffakespace    y
\end{document}


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

[<007400670067006700670067>525<0076>]TJ


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

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

\documentclass{article}
\usepackage{tagpdf}
\tagpdfsetup{activate-all,uncompress}
\begin{document}
\tagstructbegin{tag=Document}
\tagmcbegin{tag=P,raw={/ActualText <FEFF007800200020002000200079>}}
\verb+x     y+
\tagmcend
\tagstructend
\end{document}


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