How to set the font size for code blocks in pandoc markdown?

You can simply add \small before the beginning of the code snippet and \normalsize after (to return to normal).

You can also add other similar commands. For instance, if your document is doublespaced, you can add \singlespace before the code snippet and \doublespacing after.

For this to work you need to add in the yaml at the beginning of your document the following:

---
header-includes:
    - \usepackage{setspace}
---

I solved this problem for me by writing several LaTeX snippets into extra files I keep around:

 cat make-code-footnotesize.tex

   \renewenvironment{Shaded} {\begin{snugshade}\footnotesize} {\end{snugshade}}

I have such snippets for all different sizes: huge, LARGE, Large, large, normalsize, small, footnotesize, scriptsize, tiny.

To apply them when running pandoc, just include the respective LaTeX snippet with the -H parameter:

 pandoc -o block-code.pdf block-code.txt \
    -H make-code-scriptsize.tex --highlight-style=espresso

Result:

Note, this controls the font sizes for all code blocks in the PDF. It does not allow you to vary sizes from block to block. Of course, it also doesn't work for HTML, ODT, EPUB or other output -- only for LaTeX and PDF output.


I've developed a filter for pandoc https://github.com/chdemko/pandoc-latex-fontsize for this purpose:

Install this filter with pip:

$ pip install pandoc-latex-fontsize

add for example

---
pandoc-latex-fontsize:
  - classes: [c, listing]
    size: footnotesize
---

to your metadata block and specify the listings you want to be of size footnotesize:

~~~{.c .listing}
int main(void) {
    return 0;
}
~~~

then run pandoc with

$ pandoc --filter pandoc-latex-fontsize ...

Tags:

Latex

Pandoc