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

15,029

Solution 1

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

Solution 2

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.

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

Solution 4

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.

Solution 5

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"]
----

Share:
15,029

Related videos on Youtube

briandk
Author by

briandk

I'm a graduate student in Science Education research. I like math and programming, but I'm not fantastic at either.

Updated on June 28, 2022

Comments

  • briandk
    briandk almost 2 years

    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.

  • Merchako
    Merchako over 6 years
    Wouldn't that be pandoc yourfile.rmd -o yourfile.pdf then?
  • meenaparam
    meenaparam over 6 years
    @Merchako No, the output type is md_document.
  • Merchako
    Merchako over 6 years
    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?
  • meenaparam
    meenaparam over 6 years
    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.
  • Merchako
    Merchako over 6 years
    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.
  • meenaparam
    meenaparam over 6 years
    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.