18

Consider the following title block in pandoc-flavored Markdown:

% Higgelty Pigglety Pop!
  or
  There Must Be More to Life
% Maurice Sendak

Here, line breaks are part of the title. It is possible to reformat the title in order to insert it into regular text flow, e.g. "Higgelty Pigglety Pop! Or, There Must Be More to Life", but when not talked about but used on the title page of a document, preserving the line breaks is crucial. Depending on the style, it might look like this:

          Higgelty Pigglety Pop!
                   or
        There Must Be More to Life
             Maurice Sendak

My question: How can I achieve a proper multi-line title display in the output of pandoc?

A portable version would be preferred, but I'd also be content with a LaTeX-only hack.

A. Donda
  • 8,381
  • 2
  • 20
  • 49

3 Answers3

20
% Higgelty Pigglety Pop! \
  or \
  There Must Be More to Life
% Maurice Sendak

Pandoc Markdown enables the escaped_line_breaks extension by default:

A backslash followed by a newline is also a hard line break. Note: in multiline and grid table cells, this is the only way to create a hard line break, since trailing spaces in the cells are ignored.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • Thanks! I thought I had tried that and it didn't work... apparently not. The only problem I now have is that the latex code does not compile, which seems to be due to a problem with my modified template. But it does work with the standard template. – A. Donda Feb 02 '15 at 17:40
  • 1
    Hm, doesn't seem to work for me. In LaTeX I would have added a double-backslash to force a line break, but this doesn't do it. – slhck Dec 13 '17 at 16:20
  • @slhck, could you add some more info on the problems you have with this approach? Current pandoc versions use double-backslashes for line breaks in LaTeX – I'm probably missing something. – tarleb Sep 02 '18 at 17:43
  • When I add three lines to a document containing `# test`, `## foo` (then two backslashes), and `bar` and run `pandoc -o test.pdf test.md`, the second heading contains `foo` with a single backslash, and there is a normal paragraph containing `bar`: https://i.stack.imgur.com/hjITf.png — pandoc 2.2.2.1. – slhck Sep 03 '18 at 09:04
  • @slhck If I understand correctly, then it's the headers which are causing the problem? There is no pure-Markdown solution for this, but you can write ## foo\`\\\`{=latex}bar to force a linebreak in LaTeX/pdf output. Does that solve the problem? – tarleb Sep 05 '18 at 20:48
13

When using YAML metadata blocks, the following works, too:

---
title: |
    | First line
    | Second line
---

Found the idea in this thread.

slhck
  • 36,575
  • 28
  • 148
  • 201
9

A very general, but less simple method is to use raw HTML to indicate line breaks, and to convert them into proper line breaks using a pandoc filter. Below is a Lua filter which translates any <br> (or <br />) in the source into a hard line break.

--- Transform a raw HTML element which contains only a `<br>`
-- into a format-indepentent line break.
function RawInline (el)
  if el.format:match '^html' and el.text:match '%<br ?/?%>' then
    return pandoc.LineBreak()
  end
end

Save the file as linebreaks.lua.

The title could then be written as

% Higgelty Pigglety Pop!<br>or<br>There Must Be More to Life
% Maurice Sendak

The above script must be passed to pandoc via the --lua-filter option:

$ pandoc -o test.pdf --lua-filter ./linebreaks.lua test.md

The advantage of this method is that it is more universal and also works in other locations that one might want to add line breaks. E.g., a line break in a header can be added using the same syntax: # first line<br>second line.

slhck
  • 36,575
  • 28
  • 148
  • 201
tarleb
  • 19,863
  • 4
  • 51
  • 80
  • The better solution, I'm trying align the text, but I don't know how to this, see https://stackoverflow.com/questions/64466021/how-to-align-text-using-pandoc-with-lua-filters, maybe you can help me. Thanks by good answer. – PerduGames Oct 21 '20 at 16:38