21

this small example:

An example code snippet:

~~~{.cpp}
class A 
{
    public:
        static void f1 () {}; 

        virtual void f2 () = override; 
};
~~~

can be used to generate a PDF output with:

pandoc -o block-code.pdf block-code.txt

resulting in

Font sizes of the code snippet and the text are equal.

The font sizes of both the code and the text that are equal. How can I change the font size of the code snippets for the pdf (LaTex) pandoc output?

tmaric
  • 5,347
  • 4
  • 42
  • 75

5 Answers5

16

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}
---
Vlad
  • 912
  • 9
  • 9
  • This worked, thanks! And I didn't even need to include the package in the header. – Motiejus Jakštys Apr 06 '22 at 05:41
  • Is this documented somewhere? Because I've been unable to locate something more comprehensive about what can be used in slides for beamer output from markdown. – Álex Jun 04 '22 at 15:41
  • @Álex I think you can use any latex with beamer output. Pretty much any time you find that markdown can't do something, you can just switch to latex in mid-stream. – Vlad Aug 30 '22 at 17:39
11

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.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • Can the `renewenvironment` be added to a pandoc Latex template? I've tried adding it and after adding `\usepackage{framed}` I get an error message `! LaTeX Error: Undefined color `shadecolor'`. – tmaric Nov 28 '14 at 15:59
  • 1
    @tmaric: The Shaded environment will only become active if you use syntax highlighting. – Kurt Pfeifle Nov 28 '14 at 17:23
  • 1
    I get the same `Package xcolor Error: Undefined color 'shadecolor'.` whenever I try a style not espresso. I was able to define it by adding `\colorlet{shadecolor}{white}` to my template. – Rovanion Jun 29 '17 at 12:54
  • 3
    This is the way to go. But it only works with highlighting styles that have a special background. see here: https://hackage.haskell.org/package/pandoc-1.9.4/docs/Text-Pandoc-Highlighting.html#v:formatLaTeXBlock Else you have to leave out the begin and end of the snugshade environment: \renewenvironment{Shaded} {\footnotesize} {} – Philip Stark Aug 07 '17 at 10:45
7

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 ...
3

On pandoc 1.16.0.2 I did get the same problem but wasn't solved by previous answers.

When using default code highlighting of this version and exporting it with beamer (-t beamer) I got following generated output (for outputting a jmeter command):

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{./jmeter.sh} \NormalTok{-q prod.properties -p jmeter.properties  -n -t mytest.jmx -l mylog.log}
\end{Highlighting}
\end{Shaded}

By searching directly in the pandoc code with grep -r "Highlighting" * I found following code:

\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}

And I replaced it with the following to have a tiny font size in my custom pandoc template (see pandoc -D beamer):

\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},fontsize=\tiny}

Now I have a tinier font size by running the following command:

pandoc -t beamer input.md -o output.pdf --template beamer.template
albinscode
  • 51
  • 3
2

I use pandoc with the --listings option and a custom eisvogel.latex template.

  • Download the original eisvogel-template to a new file eisvogel_custom.latex
  • Open the file, find the line basicstyle = \color{listing-text-color}\small\ttfamily{}\linespread{1.15},
  • Change the \small to \footnotesize, or \tiny
  • Save the file

Now run pandoc with the following options:

pandoc --pdf-engine=xelatex --template=eisvogel_custom --listings -o block-code.pdf block-code.txt

To disable the line-numbering, add the following lines at the top of block-code.txt

---
listings-disable-line-numbers: true
...

All options can be found here: https://github.com/Wandmalfarbe/pandoc-latex-template#custom-template-variables

Tested with pandoc 2.6 using docker-image dalibo/pandocker:stable

MadMike
  • 1,391
  • 1
  • 16
  • 38