Centering and resizing figures in PDF export with pandoc
Not sure if bug or feature, but this is hard to pull off.
I stopped using full-blown LaTeX for quick documents many years ago (I still use it for my CV, though: its too flexible). Instead, I use Markdown and export with proprietary apps (if I don’t care much about looks) or Pandoc. A few days ago I wanted to convert a Markdown document with a couple embedded images to PDF, and I found something kind of tricky. I could either:
- Be unable to resize (or center) images by passing additional parameters to Pandoc’s Markdown parser, or
- Have figures with the “Figure” caption below, but resizable and centrable.
Totally unexpected! In the end, I found a way by modifying the injected LaTeX headers. This requires your local TeX tree to have some packages, but if you can use Pandoc to export from Markdown to PDF you probably have these already in place.
This should go in your YAML preamble:
header-includes: |
\usepackage{caption}
\captionsetup[figure]{
name=,
labelsep=none,
labelformat=empty}
And then the image can be centred and resized like this:
![ ](some-image-file.png){ width=150 margin=auto }
The
is to force Pandoc to generate a figure label, this makes it parse the additional sizing/margin arguments. You could also use the CLI argument -f markdown-implicit_figures
but I prefer not having to.
The full document, with some tweaks so you can customise headers and footers, could look like the following.
---
author: Your cool-looking name
fontfamily: helvet
geometry: margin=2cm
linkcolor: PineGreen
header-includes: |
\hypersetup{
colorlinks=true,
urlcolor=PineGreen,
citecolor=PineGreen,
}
\usepackage{fancyhdr}
\usepackage{caption}
\captionsetup[figure]{
name=,
labelsep=none,
labelformat=empty}
\pagestyle{empty}
\pagestyle{fancy}
\fancyhead[LE,RO]{Your cool-looking name}
\fancyhead[LO,RE]{\it Report title}
\fancyfoot[LE,RO]{\thepage}
\fancyfoot[C]{}
\renewcommand{\familydefault}{\sfdefault}
---
Welcome to the document, it's all fun and games.
![ ](some-image-file.png){ width=150 margin=auto }
Another strange surprise is that to get coloured hyperlinks you need both the colour override with hypersetup
and passing the linkcolor
argument to Pandoc.