42

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?

2
  • This is also a question about wrapping inline code for example long paths or urls which of course can make sense.
    – Wolf
    Commented Jul 4, 2017 at 12:16
  • 2
    This question is answered here : tex.stackexchange.com/q/179926/34551 in a positive way!
    – Clément
    Commented Jan 26, 2018 at 19:00

2 Answers 2

36

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}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}

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

11
  • Good to know, thanks :) Are the original line numbers (if shown) preserved?
    – Wolf
    Commented Jan 30, 2018 at 9:25
  • thank you! I am creating some 2pdf scripts based on this. Is there a way to specify the line width before breaking the line? It seems to be extremely conservative compared to the page size. I have a font size that can handle 80 characters but it seems to break at 60.
    – fommil
    Commented Sep 26, 2018 at 10:10
  • to answer my own question, add \usepackage[textwidth=6in]{geometry}
    – fommil
    Commented Sep 26, 2018 at 10:20
  • Thanks @Clément, Do you know of a way to allow linebreaks anywhere? I have a dynamically markdown document which tends to have very long strings that I also would like to be wrapped....
    – ajendrex
    Commented Jun 17, 2019 at 17:00
  • 1
    @ajendrex There should be a way with the breakanywhere option of fvextra. Try adding breakanywhere after breaklines, i.e., replace ` - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}` by ` - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,breakanywhere,commandchars=\\\{\}}`, and let me know how it goes.
    – Clément
    Commented Jun 19, 2019 at 12:47
3

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.

7
  • 1
    There is really no way to automatically warp the lines that are too long in code blocks?
    – pimpampoum
    Commented May 23, 2014 at 8:39
  • 2
    @pimpampoum Since the rendering is done by LaTeX, I guess there might be some way to do so. But I suspect it might necessitate hacking LaTeX, which I don't know anything of. You might want to ask on tex.SE whether knows how to make the verbatim environment break lines, and then put that code into the pandoc template.
    – A. Donda
    Commented May 23, 2014 at 9:20
  • 1
    I use Pandoc for md -> html and then wkhtmltopdf for html -> pdf just because the LaTeX issues are too much to deal with. What I found is that Pandoc inserts <style type="text/css">code{white-space: pre;}</style> in the HTML file's head, and so overriding it with CSS doesn't work. Since I use a script to run all this, I just strip the style out with a sed command.
    – user766353
    Commented Nov 10, 2014 at 21:58
  • 5
    pre, code { white-space: pre-wrap !important; } worked for me. But if you're using .numberLines the numbers will be off after wrapping. Commented Jun 24, 2015 at 12:43
  • 1
    When you say wrap the code manually, do you mean just inserting a newline? For me LaTeX formats the first element of the next line as green, so it doesn't look wrapped...it looks like the start of a new line
    – chimeric
    Commented Sep 1, 2016 at 1:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.