1

Criteria for good code formatting:

  • Syntax highlighting
  • Line numbers
  • Code inside should tolerate additional markup (e.g. highlighting salient points)
  • Copy/pasting the code from a PDF should Just Work™

With that last point, I would like for whitespace to be preserved so copy/pasting code from the PDF doesn't result in a completely indent-less file.

Ingmar
  • 6,690
  • 5
  • 26
  • 47
  • You can use amongst others listings and minted. Your last point still is a desideratum, cf. https://tex.stackexchange.com/questions/472099/how-to-configure-tcblisting-with-minted-to-allow-copy-paste-from-listing-with-wh/472112#472112 – DG' Aug 01 '23 at 20:21
  • 3
    you can use either minted or the listings package. But copy&paste will not "just work". It gets better if you tag the pdf and use lualatex, but it is not reliable. https://tex.stackexchange.com/a/323354/2388 – Ulrike Fischer Aug 01 '23 at 20:22
  • 1
    Consider this example. Here you can copy just the listed code or not depending on the PDF viewer. E.g., in Linux, atril or texworks viever cannot the take the code without the line numbers, but in okular you can select only the framed code without problems. – Fran Aug 01 '23 at 21:01
  • For Python, OCaml and C++, you can use the package piton (but, as with other packages, copy&paste will note "just work"). – F. Pantigny Aug 03 '23 at 07:47

0 Answers0