Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 784
skud Avatar asked Dec 26 '13 17:12

skud


People also ask

Can Pandoc convert from PDF?

How can I convert PDFs to other formats using pandoc? You can't.

How do I use Pandoc markdown PDF?

Generating PDF from Markdown with Pandoc There are actually two steps involved in converting a Markdown file to a PDF file: The Markdown source file is converted to a LaTeX source file. Pandoc invokes the pdflatex or xelatex or other TeX command and converts the . tex source file to a PDF file.

Can Pandoc convert HTML to markdown?

Pandoc can convert between numerous markup and word processing formats, including, but not limited to, various flavors of Markdown, HTML, LaTeX and Word docx.


2 Answers

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

like image 144
Clément Avatar answered Oct 02 '22 18:10

Clément


Not having the text wrapped is (part of) the point of code blocks. As far as I know, the only way to wrap the code is manually. For most languages, not exceeding a certain line length is considered good style anyway.

If your lines are length-limited but still too long for your LaTeX-generated pdf, consider reducing the font size for code blocks. For this you need to change the LaTeX template used by pandoc. A look at this answer to "How to set font size for all verbatims in Beamer presentation?" should get you started.

like image 30
A. Donda Avatar answered Oct 02 '22 19:10

A. Donda