0

I'm trying to create a PDF that will contain code snippets, and since they will mostly be in Python, the indentation is important. I found the fancyvrb package, which looks great in the PDF, but my issue is that when I select and copy the text in the PDF, it is not including any leading whitespace on each line.

Is this a limitation with PDF, or perhaps with my local environment? I've tried many different methods to generate the PDF (TeXstudio, Markdown to PDF with pandoc, writing it in Libreoffice and exporting to PDF, and even replacing the tabs with spaces), and with all those methods I've managed to get the PDF to look presentable, but they all had this issue with copying the code snippet. I would like to be able to copy code out of the PDF without needing to reformat it. Thanks for any help.

Attempting to select code

Below is an example of the TeX that I rendered for that image:

\documentclass[]{article}
\usepackage{fancyvrb}

%opening \title{Report} \author{vee} \begin{document} \maketitle \section{Test} asdf \begin{Verbatim} def test(): print("hello") \end{Verbatim} \end{document}

If I select the code snippet there, then when I paste it into an editor it does not include any indentation:

def test():
print("hello")
vee
  • 1
  • 1
    it is a limitation of PDF (or more precisely of the current pdf viewers) see e.g. https://tex.stackexchange.com/a/472112/2388 and https://tex.stackexchange.com/a/323354/2388. – Ulrike Fischer Oct 21 '23 at 22:53
  • For code formatting, listings and minted are the usual suggestions. You'll still have problems with copy/paste, but the code can have syntax highlighting and other nice features. – Teepeemm Oct 21 '23 at 23:03

0 Answers0