Pandoc Md To Pdf

Posted on  by admin

In this post I’ll describe how to use Pandoc to convert Markdown to a full HTML page (including header/footer).

  1. Pandoc Md To Pdf Online
  2. Pandoc Convert Md To Pdf
  3. Pandoc Md To Pdf Template

The Pandoc version used for the examples below is 2.11.2.

Create HTML or PDF output files with R, Knitr, MiKTeX and Pandoc. A simple, step-by-step example explaining exactly how to output HTML or PDF files from R. Bibtex to pdf converter free download. Pandoc Pandoc is a universal document converter able to convert files from a multitude of markup formats in. I use the following pandoc command to compile a PDF from a mardown file: pandoc metadata.yaml -o outfile.pdf my yaml file has the following content: - documentclass: extarticle fontsize. Pdf-to-markdown - A PDF to Markdown converter.

What is Pandoc?

Pandoc is an open-source document converter that is written in Haskell. It was initially released in 2006 and has been under active development since then.

The goal of Pandoc is to convert a document from one markup format to another. It distinguishes between input formats and output formats. As of writing this it supports 38 input formats and 59 output formats.

In this post we’ll use Markdown as an input format and HTML as an output format.

Preparing the HTML template

To generate a full HTML page we have to use Pandoc’s standalone mode which will use a template to add header and footer.

Pandoc ships with a default template, if you wish to use that skip this section and omit the --template argument.

The template we’ll use is this (save it to template.html):

Pandoc’s template variables can have different formats, the one we’re using here start and end with a dollar sign:

  • $date$: A date in a parsable format. See the date-meta docs for a list of recognized formats for the date variable. We use this to show when the document was created
  • $date-meta$: The date parsed to ISO 8601 format. This is automatically done
  • $title$: The document title
  • $body$: The document body in HTML (the converted Markdown)

We only need to set the date and title in the Markdown document via a metadata block.

Writing the Markdown file

Create a Markdown file with the following content:

The beginning of the document is the metadata block with required date and title variables mentioned above.


Several Markdown variants are supported such as GitHub-Flavored markdown. This example uses Pandoc’s extended markdown which is the default input for files with the md extension.

Converting the document

Run the following command to generate the HTML page:

Pandoc will try to guess the input format from the file extension (.md will use the Markdown input format) and output it to HTML (the default output format).

The output will be printed to the terminal:

To save the document to a file we can either redirect stdout or use the -o argument:


In this example we’ve converted Markdown to a standalone HTML page that is using a custom template.

This was just a simple example of what Pandoc is capable to do. The standalone mode coupled with the bundled default templates makes it easy to generate a wide variety of outputs such as HTML presentations, Jupyter notebooks or PDF documents.

Over the past few years, I have been using some dedicated note-taking softwareto manage my notes. But all these tools I have tried are unsatisfactory: theyare either slow or cumbersome when I want to search my notes. Finally, Idecided to take my notes in Markdown and convert them to PDF using Pandoc forreading. In this post, I will summarize how I do it.

本文的中文版本参见 这里。

Taking our notes in Markdown has several advantages:

  • We can edit the Markdown files with our favorite editor, for example,Sublime Text, which means more efficient editing and pleasant writingexperience.

  • Since a Markdown file is a textual file, we can search it using powerful

  • search tool such as grep or ripgrep.

  • We can covert the Markdown files to various formats such as PDF, HTML, epub,mobi etc., for better reading experience, with the help of Pandoc.

  • The notes are all text files and are small in size, which meanseasier and faster syncing or backup between your native PC and the cloudservice you use.

In this post, I would like to share how to generate beautiful PDF files fromMarkdown and give solutions to the issues I have encountered during theprocess.

Before we begin, you need to make sure that you have installed the followingtools:

  • First, Pandoc.After installation, you should add the path of the Pandoc executable tothe system PATH variable.

  • TeX distribution. Please make sure that TeX has been installed on yoursystem. You can use either TeX Live orMiKTeX or MacTeX base on yourplatform. You may need to set up the PATH variable1.

  • A powerful text editor. One of my favorite is SublimeText. You can also choose to use VSCode or evenNeovim.


For those who are not familiar with Pandoc, Pandoc is a powerful tool forconverting document between different formats. It is called the swiss knife ofdocument converter. There are actually two steps involved in convertingMarkdown files to PDF:

  1. Markdown files are converted to LaTeX source files.
  2. Pandoc invokes the pdflatex, xelatex or other TeX command and converts.tex source file to the final PDF file.

Because I often use non-ASCII characters in my files and my Markdown files usequotation, table and other complex format, I have met a few problems during theconversion process. In the following text, I will introduce how to solve theseissues.

How to Handle Languages other than English

By default, Pandoc uses pdflatex command to generate PDF files, which can nothandle Unicode characters well. You will encounter errors when you try toconvert Markdown files containing Unicode characters to PDF files.

Pandoc Md To Pdf

In order to handle Unicode characters, we need to use xelatex commandinstead. For the CJK languages, you need to use CJKmainfont option to givethe proper font which supports the language you are using2. In this post, I willuse the Chinese language as an example.

On Windows systems, for Pandoc version above 2.0, you can use the followingcommand to generate the PDF file:

In the above command, KaiTi is the name of a font which supports the Chinesecharacters. How do we find a font supporting a particular language? First,you need to know the language codefor the language you are using. For example, the language code for Chinese iszh. Then, use the fc-list command to look up the fonts which support thislanguage3:

The output of command is like the following:

The font name is the string after the font location. Since the font names maycontain spaces, you need to quote the font name when you want to use aparticular font, e.g., -V CJKmainfont='Source Han Serif CN'.

In Pandoc version 2.0, --pdf-engine option replaces the old --latex-engineoption. OnLinux systems where the Pandoc version may be old, the above command will notwork. You need to use the following command instead4:

On Linux systems, the way to find the font supporting your language is the sameas Windows system.

Pandoc md to pdf converter

Block quote, table and list are not correctly rendered

The reason is that Pandoc requires that you leave an empty line before blockquote, list and table environment. If the lines in the block quote are notcorrectly broken, i.e., all the lines are merged as one paragraph, you can adda space after each line to solve this issue.

Add highlight to block code

Pandoc supports block code syntax highlighting for many languages and offersseveral highlight themes. To list the highlight themes that Pandoc provides,use the following command:

To list all the languages that Pandoc supports, use the following command:

To use syntax highlighting for different languages, you need to specify thelanguage in the block quote and use --highlight-style, e.g.,:

In the above command, we use the zenburn theme, I also recommend using thetango or breezedark theme.

Use numbered section and add TOC

By default, there is no table of contents (TOC) in the generated PDF and nonumbers in the headers5. To add TOC, use the --toc option; to add sectionnumbers, use the -N option. A complete example is as follows:

Add colors to links

According to the Pandoc user guide, we can add colors to different links viathe colorlinks option to separate the links from the normal texts:

colorlinksadd color to link text; automatically enabled if any of linkcolor,filecolor, citecolor, urlcolor, or toccolor are set

To customize the color of different types of links, Pandoc offers differentoptions:

linkcolor, filecolor, citecolor, urlcolor, toccolorcolor for internal links, external links, citation links, linked URLs,and links in table of contents, respectively: uses options allowed by xcolor,including the dvipsnames, svgnames, and x11names lists

For example, to set the URL color to NavyBlue and set the TOC color to Red,we can use the following command:

Pandoc Md To Pdf Online

Note that the urlcolor option will not color the raw URL links in the text.To color those raw links, you can enclose those links with <>, e.g.,<>.

Change the PDF margin

The default margin for the generated PDF is too large. According to the PandocFAQ,you can use the following option to change the margin:

The complete command is:

Error when using backslash inside Markdown

In ordinary Markdown format, it is fine to use backslash characters inside thefiles. But Pandoc interpret the backslash and string after it as LaTeX commandby default. As a result, you may encounter weirederrors when trying to compileMarkdown files containing backslash characters. Based on discussionshere andhere, the solution is to makePandoc treat the Markdown file as normal Markdown files and not interpret theLaTeX command. You need to use the following flag:

Or you can use two backslash to represent a literal backslash, e.g., sometxt . If you want to express a LaTeX command, enclose the command withinline code block, like this: textt{}.

Add background color to inline code

In translating Markdown source file to TeX files, Pandoc use thetextttcommand to represent the inline code. So inline code has no background color inthe generated PDF files. To increase the readability of inline code, we canmodify the texttt command to add background color to text.

First, we need to create a file named head.tex and add the following settingsto it:

When converting Markdown files, use the -H option to refer the head.texfile, e.g.,:

In the generated PDF, the inline code will have grey background color. You canchange the background color as you wish.

Change the default style of block quote

By default, when converting Markdown to PDF, Pandoc use thequote environment forMarkdown block quotes. The texts inside quotation are only indented, making ithard to recognize the environment.

We can create a custom environment to add background color and indentation tothe quotation environment. Add the following setting to head.tex:

When you want to convert Markdown file to PDF, you can use the followingcommand:

Pandoc Convert Md To Pdf

The produced PDF is like the following:


  • Change background color of quotation.
  • Redefining existing environment in LaTeX.

Put the settings to head.tex

You may have noticed the clumsiness if you try to customize a lot of settings.When converting Markdown to PDF, we often need to use several settings. If youspecify all these options on the command line, it would be time consuming andcumbersome to edit. A good way is ease the issue is to put some commandsettings to head.tex file and refer to this file during Markdown file conversion.

For example, we can put the settings related to margin, inline codehighlighting, and link color to head.tex:

Click to see the code.

Nested list level exceed the limit

Pandoc Md To Pdf Template

One reader Karl Liu mentioned thatif the nested list level exceeds 6, you will encounter the following error whentrying to generate PDF file:

! LaTeX Error: Too deeply nested.

More detailed discussions can be foundhere. The solution proposed is toadd the following settings in head.tex:

Click to see the code.

Add the -H head.tex option when compiling PDF files.

Add anchors in Markdown

I try to use anchors in Markdown following the discussionhere.Unfortunately, in the generated PDF, the anchor does not work: when I click thelinking text, there is no jump to the destination page.

Instead, we should use the attribute to give an id to the location we want tojump to and then refer to it in other places using the id. Here is an example:

How to resize image

We can also resize images using the attribute. You can specify width or heightin absolute pixel values or as percentage relative to the page or column width.For example:

How to start a new page for each section

By default, when you generate PDF from Markdown files, each section started bythe level 1 header do not start from the new page: it will continue from wherethe last section ends. If you want to start a new page when a new sectionstarts, you need to add the following settings to head.tex according tothis:

But when I tried to produce PDF with the updated head.tex files, I got anerror:

According to discussionshere,it is because Pandoc’s default LaTeX redefines the pragraph command and wehave to disable this behaviour. We need to use -V subparagraph when invokingthe pandoc command:

Start a new page only after TOC

Pandoc Md To Pdf

What if we only want to add a new page after the table of contents page? Aneasy way is to hack the tableofcontents command. Add the following commandto head.tex to redefine tableofcontents command:

In the above command, we first save the old command and then redefine it toavoid recursive calls.

Line breaks

In Markdown, you can create a hard linebreak by appending two spaces after a line:

Using space at the line end for formating is annoying since it cause thetrailing whitespace warning. The space characters are also not visible.

Pandoc also provides anescaped_line_breaksextension. You can use in the end of a line followed by newline characterto represent a hard line break:

Images references

Pandoc supports LaTeX command inside Markdown, to refer to an image, you can use the LaTeX syntax:

It is cumbersome to switch to the terminal and use Pandoc to generate the PDFfiles and preview it after finishing writing the Markdown files. To simply theprocess, I use the Sublime Text build system for building PDF file andpreviewing. I use the light-weight Sumatra PDFreader for PDFpreviewing.

An example build system is shown below:

Click to see the code.

You can download the build system and head.tex filehere.

Pandoc is not recognized on Windows systems


For some reasons unknown to me, when using the above build systems to compileMarkdown files, I encountered the following errors:

‘pandoc’ is not recognized as an internal or external command, operableprogram or batch file.

After looking up the Sublime Text documentation, I find that we can add pathin the build system. So I adjust the above build system:

Click to see the code.

After that, everything goes well.

In this post, I give a complete summary on how to generate beautiful PDF filesfrom Markdown. I also share several solutions to the issues I have encountered.I hope that you can now generate beautiful PDF from Markdown files.

  • Anchors in Pandoc
  • Pandoc hard line break
  1. Make sure that you can use latex command on the command line. ↩︎

  2. For other languages, you need to use --mainfont option. ↩︎

  3. For Windows system, you can use fc-list command after installing the TeX Live full edition. For Linux systems, this command is usually pre-installed. ↩︎

  4. Tested on Pandoc version ↩︎

  5. Only the font size varies for different header levels. ↩︎