Pandoc: How can I not escape backslashes in YAML metadata?

Viewed 227

Pandoc can use a YAML header in Markdown or a defaults or metadata file for all source types where one can store variables to use in the template. Is it possible to not escape backslashes and LaTeX commands inside these metadata variables?

For example, I'd have this line in my YAML header: phone: +49\,123\,456789. When converting, Pandoc escapes the backslashes, so it becomes +49\textbackslash,123\textbackslash,456789. This is unwanted, as I want LaTeX to interpret \, and turn it into thin spaces.

I also tried wrapping it in '' or as block with |, so YAML itself doesn't escape anything, but this seems to be Pandoc itself doing the conversion, not the YAML interpreter.

Minimum reproducible example

src.md:

Some text

defaults.yaml:

template: ./tpl.tex
metadata:
  phone: +49\,123\,456789

tpl.tex:

Phone number: $phone$

Markdown body:
$body$

Output with pandoc --defaults defaults.yaml src.md --to latex:

Phone number: +49\textbackslash,123\textbackslash,456789

Markdown body:
Some text

Should be:

Phone number: +49\,123\,456789

Markdown body:
Some text
2 Answers

You can use this unicode character: https://www.compart.com/en/unicode/U+202F

Then

template: ./tpl.tex
metadata:
  phone: +49 123 456789

gets translated via pandoc --defaults defaults.yaml src.md --to latex -o test.tex to the following .tex file:

Phone number: +49\,123\,456789

Markdown body:
Some text

Metadata will always be escaped; the easiest way is to do what you want is to define a variable instead of metadata.

---
variables:
  phone: +49\,123\,456789
---

Contrary to metadata values, variables are inserted verbatim into the template.

Related