[Tex/LaTex] Convert mixed markdown/yaml/latex to docx

pandoc

I'm trying to convert a markdown file to docx using pandoc. In particular, my markdown file makes use of YAML and LaTEX syntax (I use markdown as much as possible for simplicity). This is the document sample:

% Title

---
author: Name
header-includes:
    - \usepackage{cleveref}
toc-title: Content
---

# Heading

See \cref{image}.

![Image caption\label{image}](image.png)

If I run pandoc source.md -o output.docx --table-of-contents it does not render appropriately the \cref{image}. However, if I convert it to PDF (pandoc source.md -o output.pdf --table-of-contents) it does; but then other Markdown syntax (e.g. [see @citation]) does not.

What am I doing wrong?

Best Answer

These are two questions. It takes some time to get used to the proper syntax, you seem to get the yaml a little off (remember to refer to the user manual frequently). So here we go:

  1. Picture does not get printed in the pdf. This happens because pandoc usually does not understand plain latex within the markdown unless the -s flag is passed. So if you want this image to get rendered in both pdf and docx you need to use proper pandoc syntax: ![la lune](lalune.jpg "Voyage to the moon"). Remove the \label{image} part. If you want a label, you will need to pass the flag --filter pandoc-crossref. The syntax also changes slightly to accept the label: ![Caption](file.ext){#fig:label}

  2. Citation is not picked up by pandoc. Here you have some problems, first you have to point to a bibtex file in the yaml. The syntax is bibliography: '/file.bib'. Second you have to pass another flag --filter pandoc-citeproc to pandoc. Third, use @citation to refer to it.

Finally, your md should look more like:

---
title: Title
author: Name
toc-title: Content
bibliography: '/file.bib'
header-includes:
    - \usepackage{cleveref}
---

# Heading

See @fig:label. This dude has talked about it previously: @thedude2017

![Caption](file.ext){#fig:label}

# References

Build it with pandoc file.md --filter pandoc-crossref --filter pandoc-citeproc -o file.pdf.