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

15,035

Solution 1

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.

Solution 2

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

Share:
15,035
skud
Author by

skud

JavaScript/Node.js/Backbone.js/CSS3/HTML5

Updated on June 03, 2022

Comments

  • skud
    skud almost 2 years

    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?

  • pimpampoum
    pimpampoum almost 10 years
    There is really no way to automatically warp the lines that are too long in code blocks?
  • A. Donda
    A. Donda almost 10 years
    @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.
  • user766353
    user766353 over 9 years
    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.
  • Beni Cherniavsky-Paskin
    Beni Cherniavsky-Paskin almost 9 years
    pre, code { white-space: pre-wrap !important; } worked for me. But if you're using .numberLines the numbers will be off after wrapping.
  • chimeric
    chimeric over 7 years
    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
  • Wolf
    Wolf almost 7 years
    @user766353 you may replace the HTML template for removing the inline style for the code tag.
  • Wolf
    Wolf over 6 years
    Good to know, thanks :) Are the original line numbers (if shown) preserved?
  • fommil
    fommil over 5 years
    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
    fommil over 5 years
    to answer my own question, add \usepackage[textwidth=6in]{geometry}
  • ajendrex
    ajendrex almost 5 years
    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....
  • Clément
    Clément almost 5 years
    @ajendrex There should be a way with the breakanywhere option of fvextra. Try adding breakanywhere after breaklines, i.e., replace ` - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breakline‌​s,commandchars=\\\{\‌​}}` by ` - \DefineVerbatimEnvironment{Highlighting}{Verbatim}{breakline‌​s,breakanywhere,comm‌​andchars=\\\{\}}`, and let me know how it goes.
  • Clément
    Clément almost 5 years
    @ajendrex Indeed… I don't know the solution to your issue. I'll let you know if I find out how to do it. You could open another question on this website, or at github.com/jgm/pandoc/issues/4302#issuecomment-360669013
  • Leo
    Leo almost 4 years
    Is there a way to do this with R Markdown styled blocks? E.g. ````{shell}`? For some reason I'm not getting wrapped or colored output, though it is styled as monospaced code text.
  • Clément
    Clément almost 4 years
    @Leo Ask with details in a different question and let me know here, and I'll have a look. Based on your comment, I have no idea what it is that you are trying to achieve.
  • Sergey Vyacheslavovich Brunov
    Sergey Vyacheslavovich Brunov almost 3 years
    Additional definition is should be added to work with the languages that do not have the corresponding highlighting: for example, plain text (```text): \DefineVerbatimEnvironment{verbatim}{Verbatim}{breaklines,co‌​mmandchars=\\\{\}}.
  • Greenfly77
    Greenfly77 over 2 years
    This should be the accepted answer instead! Works like a charm, thanks!