How can I specify pandoc's markdown extensions using a YAML block?
Asked Answered
F

5

30

Background

Pandoc's markdown lets you specify extensions for how you would like your markdown to be handled:

Markdown syntax extensions can be individually enabled or disabled by appending +EXTENSION or -EXTENSION to the format name. So, for example, markdown_strict+footnotes+definition_lists is strict markdown with footnotes and definition lists enabled, and markdown-pipe_tables+hard_line_breaks is pandoc’s markdown without pipe tables and with hard line breaks.

My specific question

For a given pandoc conversion where, say, I use grid tables in my source:

pandoc myReport.md --from markdown+pipe_tables --to latex -o myReport.pdf

How can I write a pandoc YAML block to accomplish the same thing (specifying that my source contains grid tables?)

A generalized form of my question

How can I turn extensions on and off using pandoc YAML?

Stack Overflow Questions that I don't think completely answer my question

There may also not be a way to do this

It's always possible that pandoc isn't designed to let you specify those extensions in the YAML. Although, I'm hoping it is.

Flammable answered 10/9, 2015 at 2:30 Comment(2)
I think this is not possible with pandoc. See this answer by John MacFarlane on the pandoc mailing list: groups.google.com/d/msg/pandoc-discuss/F5p85SQ7ejY/3cRny3RqTgkJCretonne
I wrote a small script that makes this possible: github.com/mb21/panrunPomace
P
10

You can use Markdown Variants to do this in an Rmarkdown document. Essentially, you enter your extensions into a variant option in the YAML header block at the start of the your .Rmd file.

For example, to use grid tables, you have something like this in your YAML header block:

---
title: "Habits"
author: John Doe
date: March 22, 2005
output: md_document
    variant: markdown+grid_tables
---

Then you can compile to a PDF directly in pandoc by typing in your command line something like:

pandoc yourfile.md -o yourfile.pdf

For more information on markdown variants in RStudio: http://rmarkdown.rstudio.com/markdown_document_format.html#markdown_variants

For more information on Pandoc extensions in markdown/Rmarkdown in RStudio: http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#pandoc_markdown

Pitapat answered 22/12, 2016 at 10:58 Comment(6)
Wouldn't that be pandoc yourfile.rmd -o yourfile.pdf then?Nepotism
@Nepotism No, the output type is md_document.Pitapat
I think it would be helpful to add some clarifying text to this answer. It sounds like you're taking two steps, but you don't explicitly say that. Where does yourfile.md come from?Nepotism
Say your original file is called yourfile.Rmd - following the instructions to add the extra text in your YAML header results in the yourfile.md file being created, which you can then use in pandoc.Pitapat
I get that, but I think it should be incorporated into your answer for proper flow. It doesn't make sense to those who don't already use Rmarkdown. Since this question is about Pandoc, you can't assume familiarity with Rmarkdown.Nepotism
The original question asker puts Rmarkdown and RStudio in their tags for the question, and accepted the answer. If you need more clarity, go ahead and edit as you see fit or post another answer.Pitapat
E
7

You can specify pandoc markdown extension in the yaml header using md_extension argument included in each output format.

---
title: "Your title"
output:
  pdf_document:
    md_extensions: +grid_tables
---

This will activate the extension. See Rmarkdown Definitive Guide for details.

Electrotonus answered 20/10, 2018 at 11:34 Comment(0)
M
5

For people stumbling across this in or after 2021, this can be done without Rmarkdown. You can specify a YAML "defaults" file, which basically includes anything you could want to configure.

In order to do what OP wanted, all you'd need to do is

from: markdown+pipe_tables

in the defaults file, then pass it when you compile. You can also specify the input and output files, so you can end up with the very minimal command pandoc --defaults=defaults.yaml and have it handle the rest for you. See https://pandoc.org/MANUAL.html#extensions for more.

Marvelofperu answered 21/6, 2021 at 15:0 Comment(0)
B
3

Outside Rmarkdown scope, you can use Pandocomatic to it, or Paru for Ruby.

---
 title: My first pandocomatic-converted document
 pandocomatic_:
     pandoc:
         from: markdown+footnotes
         to: html
 ...
Brest answered 14/2, 2019 at 3:22 Comment(0)
I
2

As Merchako noted, the accepted answer is specific to rmarkdown. In, for instance, Atom md_extensions: does not work.

A more general approach would be to put the extensions in the command line options. This example works fine:

----
title: "Word document with emojis"
author: me
date: June 9, 2021
output:
  word_document:
    pandoc_args: ["--standalone", "--from=markdown+emoji"]
----

Ironist answered 9/6, 2021 at 19:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.