Search code examples
pdflatexr-markdownpandocquarto

How to get code line numbers inside page margins?


I'm using pandoc to generate PDFs from markdown files, when i write a code block with line numbers using {.lang .numberLines} the line numbers goes outside page margins like that:

Code line numbers outside page margins

Is there anyway to fix that.

I tried searching for this issue but most of the answers like this one were for how to fix that behavior when writing pure latex not pandoc flavored markdown.


Solution

  • Pandoc uses LaTeX and the fancyvrb package to render highlighted code blocks. We can customize the output by configuring the Highlighting environment used by pandoc. That's possible by placing a raw LaTeX snippet at the start of the document (or anywhere, as long as it comes before the first code block).

    \DefineVerbatimEnvironment{Highlighting}{Verbatim}{%
      xleftmargin=18pt,commandchars=\\\{\}}
    

    You'll probably have to experiment a bit and should adjust the value of xleftmargin to your liking. See the package documentation of fancyvrb for more customization options.