pandoc doesn't text-wrap code blocks when converting to pdf

skud picture skud · Dec 26, 2013 · Viewed 10.7k times · Source

I'm using pandoc with xelatex engine to convert markdown to pdf. I'm running pandoc like this:

pandoc -s 'backbone-fundamentals'.md -o 'backbone-fundamentals'.pdf \
    --title-prefix 'Developing Backbone.js Applications' \
    --normalize \
    --smart \
    --toc \
    --latex-engine=`which xelatex`

If a code line is longer than the pdf document width it just gets cutoff. Is there anyway to have pandoc text wrap long code lines?

Answer

Clément picture Clément · Jan 29, 2018

If you have a recent installation of LaTeX that includes the fvextra package, then there is a simple solution, recently suggested by jannick0.

Modify your YAML header options to include

\usepackage{fvextra}
\begin{Highlighting}[breaklines,numbers=left]

and compile with xelatex.

For instance,

---
header-includes:
 - \usepackage{fvextra}
 - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}
---

~~~~~{.java}
this is a very long long long long long long long long long long long long long line which is broken
~~~~~~

when compiled with

pandoc input.md --pdf-engine=xelatex -o output.pdf

gives enter image description here

If you had the .numberLines option, i.e.,

---
header-includes:
 - \usepackage{fvextra}
 - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}
---


~~~~~{.java .numberLines}
this is a very long long long long long long long long long long long long long line which is broken
~~~~~~

then the same command would produce

enter image description here