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

this small example:

An example code snippet: ~~~{.cpp} class A { public: static void f1 () {}; virtual void f2 () = override; }; ~~~ 

can be used to generate PDF output using:

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

resulting in

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

The font size of both code and text is equal. How to change the font size of code snippets for pandoc pdf (LaTex) output?

+11
latex pandoc
source share
5 answers

I solved this problem for me by writing a few LaTeX snippets into additional files that I save:

  cat make-code-footnotesize.tex \renewenvironment{Shaded} {\begin{snugshade}\footnotesize} {\end{snugshade}} 

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

To apply them when starting pandoc , simply include the corresponding LaTeX snippet with the -H option:

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

Result:

2ihIr.png

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

+4
source share

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

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

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

 --- header-includes: - \usepackage{setspace} --- 
+4
source share

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

Install this filter using pip :

 $ pip install pandoc-latex-fontsize 

add for example

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

in your metadata block and specify the lists you want to have footnotesize size:

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

then run pandoc with

 $ pandoc --filter pandoc-latex-fontsize ... 
+2
source share

In pandoc 1.16.0.2 I had the same problem, but it was not solved by previous answers.

When using the default code highlighting for this version and exporting it using the ray ( -t beamer ), I received the following generated output (for the output of the 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} 

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

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

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

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

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

 pandoc -t beamer input.md -o output.pdf --template beamer.template 
+1
source share

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

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

+1
source share

All Articles