10

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

martin
  • 103

1 Answers1

5

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.

lf_araujo
  • 801
  • 1
  • 8
  • 22
  • Amazing! Works and does exactly what I was looking for. Thank you for your help lf_araujo – martin Aug 30 '16 at 14:32
  • It works very well! But I had to do the following in addition (issue between listings and utf8): https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings#Encoding_issue – Gregoire Cattan Jun 21 '22 at 09:40