[Tex/LaTex] Pandoc: code blocks in MarkDown with very long lines get cut off when outputting to PDF

line-breakingmarkdownpandocpdf

We have a lot of internal SOPs and manuals with long code lines in those. The code blocks are fenced with 3 backticks (```). However converting those to PDF causes long lines to be cut at the page margin.

If the same long line of code is fenced with single backtick (`) it wraps at the end of the page automatically.

Is there a way to replicate that behaviour for code blocks with 3 backticks? I looked through Pandoc: Markdown to PDF, without cutting off code block lines that are too long but I admit I am not sure how to apply the solution to my problem.

Finally the command I use to output to PDF is:

pandoc --toc -V geometry:"left=1cm, top=1cm, right=1cm, bottom=2cm" -V fontsize=12pt test.md -o test.pdf

You can test with something like:

example of a code block with long lines. The code block is framed with (“`):

“` { .bash .numberLines startFrom="1"}
default-preference-list SHA512 SHA384 SHA256 SHA224 SHA1 AES256 TWOFISH CAMELLIA256 AES192 CAMELLIA192 AES CAMELLIA128 3DES ZLIB BZIP2 ZIP Uncompressed
“““““““““““““““““““““““

I use Debian 8.5 with pandoc 1.12.4.2

Best Answer

You're almost there and the answer you link has the solution.

First create a new file in your preferred editor and add the lines:

% Contents of listings-setup.tex
\usepackage{xcolor}

\lstset{
    basicstyle=\ttfamily,
    numbers=left,
    numberstyle=\footnotesize,
    stepnumber=2,
    numbersep=5pt,
    backgroundcolor=\color{black!10},
    showspaces=false,
    showstringspaces=false,
    showtabs=false,
    tabsize=2,
    captionpos=b,
    breaklines=true,
    breakatwhitespace=true,
    breakautoindent=true,
    linewidth=\textwidth
}

Then save this file as listings-setup.tex. It is all there is to do. Now compile your file with:

pandoc --listings -H listings-setup.tex --toc -V geometry:"left=1cm, top=1cm, right=1cm, bottom=2cm" -V fontsize=12pt test.md -o test.pdf

And your code block might wrap appropriately.

Related Question