37

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?

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

2 Answers2

32

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

Clément
  • 2,358
  • 28
  • 32
  • Good to know, thanks :) Are the original line numbers (if shown) preserved? – Wolf Jan 30 '18 at 09: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 Sep 26 '18 at 10:10
  • to answer my own question, add `\usepackage[textwidth=6in]{geometry}` – fommil Sep 26 '18 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 Jun 17 '19 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 Jun 19 '19 at 12:47
  • @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 https://github.com/jgm/pandoc/issues/4302#issuecomment-360669013 – Clément Jun 24 '19 at 22:03
  • 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. – Leo Jun 10 '20 at 00:16
  • @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. – Clément Jun 10 '20 at 13:24
  • 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,commandchars=\\\{\}}`. – Sergey Vyacheslavovich Brunov Jun 23 '21 at 17:57
  • This should be the accepted answer instead! Works like a charm, thanks! – Greenfly77 Oct 21 '21 at 21:48
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.

Community
  • 1
  • 1
A. Donda
  • 8,381
  • 2
  • 20
  • 49
  • 1
    There is really no way to automatically warp the lines that are too long in code blocks? – pimpampoum May 23 '14 at 08: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 May 23 '14 at 09: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 `` 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 Nov 10 '14 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. – Beni Cherniavsky-Paskin Jun 24 '15 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 Sep 01 '16 at 01:36
  • @user766353 you may [replace the HTML template](https://github.com/jgm/pandoc/wiki/User-contributed-templates) for removing the inline style for the `code` tag. – Wolf Jul 04 '17 at 11:43
  • @A. Dona "For most languages, not exceeding a certain line length is considered good style anyway". Sure it is, but I don't want the LaTeX page width to be dictating what that line length should be. – user32882 Mar 09 '23 at 16:04