Specifying metadata for input formats other than Markdown - pandoc

Pandoc allows you to include metadata at the beginning of a Markdown document using a header like
---
title: The Song That Never Ends
subtitle: It Goes On and On My Friends
author: Abraham Lincoln
lang: en_US
---
Is there any way to convey this information to Pandoc when the input format is not Markdown? I’m specifically interested in HTML input. I tried calling Pandoc with --from=html+yaml_metadata_block, but this didn’t seem to change the behavior at all—the YAML block is just interpreted as HTML.
(It is possible to include some metadata in the “percent format” shown in the “pandoc_title_block” section of the manual, but there doesn’t seem to be a way to give a separate title and subtitle with that syntax. It’s also possible to include the YAML header before the HTML and to force Pandoc to interpret the input as Markdown, but this seems hacky, and if you try to convert that to “real” Markdown then the output is full of HTML tags instead of Markdown formatting characters.)

You can use the --metadata (short -M) or --metadata-file options to supply metadata on the command line, for example:
pandoc -M title="The Song That Never Ends"

A simple solution would be to use Lua filters to augment the metadata read from the HTML file as described in the Lua filters doc. Below is an updated version:
-- file: additional-metadata.lua
function read_file_as_markdown_yaml (filename)
-- read metadata file into string
local metafile = io.open(filename, 'r')
local content = metafile:read('*a')
metafile:close()
-- get metadata
return pandoc.read(content, 'markdown').meta
end
function Meta (meta)
-- read YAML file and add its content to the metadata
local yaml_meta = read_file_as_markdown_yaml(meta.default_meta_file)
for k, v in pairs(yaml_meta) do
-- use YAML metadata as fallback
meta[k] = meta[k] or v
end
return meta
end
Use with
pandoc --lua-filter additional-metadata.lua \
--metadata default_meta_file:YOUR-FILE-HERE.yaml \
your-input-file.html

Related

How to include a latex document into a markdown using yaml metadata `include-after-body`

I want include a latex document into a markdown .md file using the YAML metadata. I have two files in a directory:
markdown.md
---
title: test fuer pdf eingeschlossen
author: AUF
keywords: test
abstract: Versuch
publish: True
include-after-body: testlatex.tex
---
A simple exampe for a test.
and testlatex.tex
some text without sense
\begin{itemize}
\tightlist
\item
firstly, a fundamental human need is;
\item
secondly, a cost-effective technical mean,
\end{itemize}
I can include the testlatex.tex file into the body of the markdown.md on the commandline with
pandoc --pdf-engine=lualatex --toc -o test.pdf markdown.md --include-after-body=testlatex.tex
but the equivalent value put into the YAML metadata seems not to have any effect (author and title are however used). I thought that the value included on the command line or in the YAML metadata would be equivalent. I checked in the pandoc latex template and see there an include for the include-after but I also wonder where the filename is converted to its content.
If I put
include-after: testlatex.tex
in the YAML metadata the name of the file is printed in the output, but the file content is not used!
Thank you for your help!

Is it possible to load a CSV file with Pandoc and producing markdown file for each line?

I have a CSV file similar to below:
0,Bob's Business,50 some address,zip,telephone
1,Jill's Business,25 some address,zip,telephone
...
I would like to take this CSV file and have Pandoc produce a markdown file for each line in the CSV file. Each column accessible from a variable to be used in a markdown template file.
Is it possible to load a CSV file and produce markdown/html files in this way?
I can see three ways.
Use a static site generator
I would probably just use a tool like jekyll with its data files.
Alternative 1: Convert to YAML and use pandoc's template engine
Put something like this in mytemplate.md:
$for(data)$
$data$
$endfor$
Convert the csv to a JSON or YAML file
load that file with the --metadata-file option and use the template to render the output:
echo '' | pandoc --metadata-file data.yaml -t markdown --template mytemplate.md -o output.md
Alternative 2: Write a pandoc filter
There are many pandoc filters (like pandoc-placetable or pantable) that read csv and convert it to a pandoc table. But you want to convert it to a pandoc metadata format (which is usually parsed from the YAML frontmatter of markdown files). I guess you could adjust one of those pandoc filters to your purposes.

Remove \hypertarget from pandoc LaTex output

I am using pypandoc to convert a markdown file to LaTex. My markdown file has a header, for example:
# Header Text #
When pypandoc renders the file as a .tex file, this appears as:
\hypertarget{header-text}{%
\section{Header Text}\label{header-text}}
While this is a nice feature to make it easy to link back to section headers, I don't necessarily want that and would prefer in this case for pypandoc to just generate:
\section{Header Text}
Is there a pandoc setting, or a pypandoc setting, that can be used to turn off the \hypertarget{} feature? I have reviewed the documentation for pandoc and didn't see it anywhere.
I had the same need, and I am using the -auto_identifiers switch,
pandoc -r markdown-auto_identifiers -w latex test.md -o test.tex
That will remove both
\hypertarget{header-text}{%
and
\label{header-text}}
leaving only
\section{Header Text}
like you requested.
Source
There is no such switch. If you want different output, you'd either have to use a pandoc filter or, as #mb21 already noted, post-process the output.
Neither of these options is very good: using a filter to manually define header output will lose you all kinds of other pandoc features, like --top-level-division and support for unnumbered headers. Post-processing, on the other hand, tends to be brittle and difficult to get right.
Anyway, below is a panflute filter, which will replace headers with a custom command. Save it to a file and pass it to pypandoc via the filters option; this should give you the desired output.
from panflute import *
sectionTypes = ["section", "subsection", "subsubsection",
"paragraph", "subparagraph"]
def reduce_header(elem, doc):
if type(elem) == Header:
cmd = "\\%s{" % sectionTypes[elem.level - 1]
inlines = [RawInline(cmd, "tex")]
inlines.extend(elem.content)
inlines.append(RawInline("}", "tex"))
return Plain(*inlines)
if __name__ == "__main__":
run_filter(reduce_header)

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

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
Can I set command line arguments using the YAML metadata - This one deals with how to specify output options, but I'm trying to tell pandoc about the structure of my input
What can I control with YAML header options in pandoc? - Answerers mention pandoc's templates, but neither the latex output template nor the markdown template indicate any sort of option for grid_tables. So, it's not clear to me from these answers how knowing about the templates will help me figure out how to structure my YAML.
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.
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
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.
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
...
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"]
----
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.

Pandoc: use variables in custom latex preamble

I have the file test.md which contains:
---
footertext: some text for the footer
headertext: this is in the header
---
here is the text body.
And the file format.tex which contains:
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[L]{$headertext$}
\fancyfoot[L]{$footertext$}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\setlength{\headsep}{0.25in}
I run the command:
pandoc -H format.tex test.md -o test.pdf
You can see what I want to do. I am trying to get the text "this is in the header" to show up in the header, but it does not, it only shows the string "headertext" (same problem for footer).
What am I doing wrong?
Edit: OK, I think I understand. Apparently variables are only available in templates, not in included begin or end code blocks (like I am using), or in the md itself. So new question: Why is this? It is unintuitive, inconvenient, and poorly documented.
You can easily modify a pandoc template. Access the default template with
pandoc -D latex > new_template.latex
Paste the content of your format.tex in the preamble. You should use $if$ to check if the variable exists before using it if you want to use this template for more than one document :
\usepackage{fancyhdr}
\pagestyle{fancy}
$if(headertext)$\fancyhead[L]{$headertext$}$endif$
$if(footertext)$\fancyfoot[L]{$footertext$}$endif$
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\setlength{\headsep}{0.25in}
Then compile with :
pandoc test.md -o test.pdf --template=new_template.latex

Resources