Generation of Pandoc in markdown PDF, not considering header formatting

I use pandoc to create pdf code from some markdown. I use h1-h4 through hash characters. Example h1 = #, h4 = ####. When I create my document as follows:

pandoc input.md -o output.pdf 

I get a document where h1, h2 and h3 have a new line after them, but h4 does not have a new line. The text begins on the same line as the heading (it is not formatted the same way, but there is no new line between the characters).

I tried adding spaces after #### and adding a manual line using my editor, but nothing works.

Any ideas?

+11
markdown pdflatex latex pandoc
source share
4 answers

pandoc creates PDF files through LaTeX. In LaTeX, headers are generated using the following commands:

  • \section
  • \subsection
  • \subsubsection
  • \paragraph
  • \subparagraph

As you can see, the “fourth level” corresponds to the \paragraph command that appears in the description. The \subsubsubsection command is simply not used.

The only way to get what you want is to override the \paragraph command, which is pretty tricky. I was unable to get it to work with Pandoc.

+11
source share

Header Migration

Perhaps the best way to solve this problem is to completely avoid the problem by moving the level 4 heading. By default, pandoc uses the \section command for level 1 and the \paragraph command for level 4 headings. This can be changed using the --top-level-division parameter:

--top-level-division=[default|section|chapter|part]

Treat top-level headers as the specified division type in LaTeX output [...]. The hierarchy order is a part, a chapter, then a section; all headers are offset so that the top-level header becomes the specified type. The default behavior is to determine the best type of division using heuristics [...]

Thus, with --top-level-division=chapter , the 4th level heading will be generated using the \subsubsection command.

Styling with LaTeX

If this is not an option, the next best way is to customize the layout of the corresponding LaTeX command: for headings of the fourth level, the default is \paragraph . The following methods are taken from TeX StackExchange answers .

Standard Document Classes

By default, you can configure \paragraph through the titlesec package. To do this, we can use the include-header metadata field that pandoc will include in the LaTeX intermediate document.

 --- header-includes: | ''' {=latex} \usepackage{titlesec} \titlespacing*{\paragraph}{0pt}{1ex}{-\parskip} \titleformat{\paragraph}[hang] {\normalfont\bfseries} {} {0pt} {} ''' --- 

KOMA Document Classes

Using titlesec will not work properly for documents that use KOMA classes (e.g. scrartcl), because KOMA has its own ways of doing things. To do this, use this alternative snippet:

 --- documentclass: scrartcl header-includes: | ''' {=latex} \makeatletter \renewcommand\paragraph{\@startsection{paragraph}{4}{\ z@ }% {-3.25ex \@plus -1ex \@minus -0.2ex}% {0.01pt}% {\raggedsection\normalfont\sectfont\nobreak\ size@paragraph }% } \makeatother ''' --- 
+2
source share

Although @tarlebs's answer is certainly the best (except that it indicates the wrong amount of vertical space), here it is "simpler" (in some respects) but a more hacky (at least in LaTeX terms) solution that additionally uses the Pandoc filter Lua or LaTeX hack, but avoids downloading another LaTeX package.

We want the LaTeX source to look something like this:

 \hypertarget{level-4-heading}{% \paragraph{Level 4 heading}\label{level-4-heading}} \hfill Lorem ipsum dolor sit amet. 

This LaTeX looks awful, but if you do not need to save or share the LaTeX source, it does what you probably want: the space between the level 4 heading and the paragraph after it are equal to the gap between the level 3 heading and the paragraph after it.

Level 4 heading with paragraph break

Here's how it works: since \hfill about to close on a separate line, how can you get to an empty paragraph in LaTeX you will get the first paragraph - the one that comes with the heading - containing only a horizontal space to the end of the line, and then immediately after the new paragraph - the actual first paragraph after the heading is just a normal paragraph between it and the heading. This one probably also upsets LaTeXs idea of ​​how small \paragraph should be.

The manual way to do this is as follows:

  #### Level 4 heading ''''{=latex} \hfill '''' Lorem ipsum dolor sit amet. 

It uses a relatively new syntax for raw Pandocs markup - “code block”. this is actually a raw LaTeX block - but it looks even worse than the resulting LaTeX source! It is also a tedious routine to insert this after each level 4 header. In other words, you want to insert this Raw LaTeX automatically, and this can be done using the Lua filter:

  --[======================================================================[ latex-h4-break.lua - Pandoc filter to get break after a level 4 heading. Usage: $ pandoc --lua-filter latex-h4-break.lua input.md -o output.pdf --]======================================================================] -- create it once, use it many times! local hfill_block = pandoc.RawBlock('latex', '\\hfill') function Header (elem) if 4 == elem.level then return { elem, hfill_block } else -- ignore headings at other levels! return nil end end 

However, you can also do a simple LaTeX hack in the header-includes Metadata block to get the same effect:

  --- header-includes: - | ''' {=latex} \let\originAlParaGraph\paragraph \renewcommand{\paragraph}[1]{\originAlParaGraph{#1} \hfill} ''' --- #### Level 4 heading Lorem ipsum dolor sit amet. 

This works by first creating an alias of the \paragraph command and then redefining the \paragraph command itself using the alias in the new definition, so now wherever the LaTeX source is created, Pandoc contains \paragraph{Foo} if it instead contains \paragraph{Foo} \hfill that does what we want with zero dependency gain! (In case you are interested in the strange spelling of an “alias”, the Team minimizes the risk that it runs into something that already exists, since the TeX \let command does not verify this. We certainly do not want to overwrite any existing command !)

NOTE. If you really need more or less space than usual To break a paragraph after the heading, just add the appropriate \vspace command after \hfill : \hfill \vspace{-0.5\parskip} .

+1
source share

I'm not sure why, but this works for me:

Put $\ \\ $ in the first line after #### headline

0
source share

All Articles