I am using the minted package for syntax highlighting code in my slides, but I have some problem to make this code easily copyable directly from the PDF to the console. it seems that the whitespaces used to format the code totally disappear when we select the code on the screen.

I was used to the option keepspaces=true and columns=flexible in the lstlisting package but I cannot find anything similar in the minted package documentation.

Did I miss something or is it just impossible with minted ?

Here is a code sample:


int main(int argc, char *argv[])
  extern int var;
  return var;

Compiled with: pdflatex -shell-escape sample.tex, it will produce:

code sample

But, when we copy-paste itdirectly from the PDF, we will get:

int main(int argc, char *argv[])
extern int var;
return var;

You should have noticed that all the white spaces formatting has been lost in the process.

What I would like to have is a copyable code with a copy-paste selection.

  • 2
    In ConTeXt, I usually attach the source code in the pdf (using \attachfile). This also avoids things such as selecting the header and footer when copy pasting directly from the pdf. I am sure there will be an equivalent method in LaTeX. – Aditya Nov 12 '18 at 13:15
  • This is an interesting possibility, thanks for that! But, it does not provide any syntax highlight for the code. I know I want a lot, but I would like to have both! :-) – perror Nov 12 '18 at 13:23
  • I do both. Provide syntax highlighted code for viewing on the pdf and an attached file for copy pasting. All hidden behind a macro, so that I only need to type \startcode ... \stopcode. It is relatively straight-forward to implement for someone who knows the innards of minted (minted saves the file to disk in order to run pygments; you just need to attach that file). – Aditya Nov 12 '18 at 13:32
  • Well, I tried and it does not fit my need at all. I would like to have the code only once (and not having to have two ways to access it). – perror Nov 12 '18 at 13:48
  • You can use the accsupp package to provide the actual text instead of the minted text for copying (and screen reading), but please help us help you by providing a minimal compilable code example of you setup. – TeXnician Nov 12 '18 at 16:10

Your Answer

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

Browse other questions tagged or ask your own question.