Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve line breaks in title using pandoc

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.

like image 598
A. Donda Avatar asked Feb 02 '15 17:02

A. Donda


People also ask

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.

How do you insert a line break in Rmarkdown PDF?

To break a line in R Markdown and have it appear in your output, use two trailing spaces and then hit return .

Can pandoc convert PDF to markdown?

Commonly used markup languages include Markdown, ReStructuredText, HTML, LaTex, ePub, and Microsoft Word DOCX. In plain English, Pandoc allows you to convert a bunch of files from one markup language into another one. Typical examples include converting a Markdown file into a presentation, LaTeX, PDF, or even ePub.


3 Answers

% 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.

like image 124
Chris Avatar answered Sep 20 '22 13:09

Chris


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

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

Found the idea in this thread.

like image 26
slhck Avatar answered Sep 17 '22 13:09

slhck


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.

like image 23
tarleb Avatar answered Sep 18 '22 13:09

tarleb