37

I have some reports written in MultiMarkDown and want to convert them to pdf (via xelatex) with pandoc. As I am in an professional environment, a certain font-family is mandatory. The fonts are sitting right in the windows/fonts folder, named font-regular.otf, font-italic.otf and so on.

I know about the command line switch -V mainfont:font-regular.otf and I am able to switch font with it, but that only sets the regular font. I can do the run for a .tex instead of a .pdf and edit the file manualy like

\setmainfont[
BoldFont = Font-Bold.otf,
ItalicFont = Font-Italic.otf,
BoldItalicFont = Font-BoldItalic.otf
]{Font-Regular.otf}

But that is cumbersome, since it is additional work for each document to be converted.

Is there a way to:

  • easily create a "font-family" that I can pass, or
  • pass the fout .otf via command line (and therefore via script or batch)
32

YAML header

---
mainfont: Font-Regular.otf
mainfontoptions:
- BoldFont=Font-Bold.otf
- ItalicFont=Font-Italic.otf
- BoldItalicFont=Font-BoldItalic.otf
---

command line

$ pandoc in.md --pdf-engine=xelatex \
         -V 'mainfont:Font-Regular.otf' \
         -V 'mainfontoptions:BoldFont=Font-Bold.otf, ItalicFont=Font-Italic.otf, BoldItalicFont=Font-BoldItalic.otf'
         -o out.pdf

Since my answer to the original question, a lot has changed. So here is an update on how to set fonts with pandoc. There are several ways to change the font in pandoc and they are quite well documented nowadays. Here is a brief overview:

1. Changing fonts for XeLaTeX and LuaLaTeX:

You can specify any font on your system that can be used with fontspec

In the YAML header of your markdown file, set the variables like this for XeLaTeX:

---
mainfont: DejaVuSerif.ttf
sansfont: DejaVuSans.ttf
monofont: DejaVuSansMono.ttf 
mathfont: texgyredejavu-math.otf 
---

or, for LuaLaTex:

---
mainfont: DejaVuSerif
sansfont: DejaVuSans
monofont: DejaVuSansMono
mathfont: TeXGyreDejaVuMath-Regular
---

If you need more fine grained control, you can specify options:

---
mainfont: DejaVuSerif
mainfontoptions:
- Extension=.ttf
- UprightFont=*
- BoldFont=*-Bold
- ItalicFont=*-Italic
- BoldItalicFont=*-BoldItalic
---

Call pandoc with --pdf-engine set to xelatex or lualatex:

$ pandoc in.md --pdf-engine=xelatex -o out.pdf

You can also set the fonts on the command line:

$ pandoc in.md --pdf-engine=xelatex \
         -V 'mainfont:DejaVuSerif.ttf' \
         -V 'sansfont:DejaVuSans.ttf' \
         -V 'monofont:DejaVuSansMono.ttf' \
         -V 'mathfont:texgyredejavu-math.otf' \
         -o out.pdf

or

$ pandoc in.md --pdf-engine=lualatex \
         -V 'mainfont:DejaVuSerif' \
         -V 'sansfont:DejaVuSans' \
         -V 'monofont:DejaVuSansMono' \
         -V 'mathfont:TeXGyreDejaVuMath-Regular' \
         -o out.pdf

And again, with options:

$ pandoc in.md --pdf-engine=xelatex \
         -V 'mainfont:DejaVuSerif' \
         -V 'mainfontoptions:Extension=.ttf, UprightFont=*, BoldFont=*-Bold, ItalicFont=*-Italic, BoldItalicFont=*-BoldItalic' \
         -V 'sansfont:DejaVuSans.ttf' \
         -V 'monofont:DejaVuSansMono.ttf' \
         -V 'mathfont:texgyredejavu-math.otf' \
         -o out.pdf

Note, that xelatex wants the filename, while lualatex is content with the fontfamily name.

Example

2. Change the template

Some packages like dejavu-otf or libertinus-otf do the fontspec setup for you, but you have to modify the template in order for them to work.

Get the default template and save it to some file:

$ pandoc -D latex > template.latex

Open the file and remove all the font configuration code and replace it with, e.g.:

\usepackage{dejavu-otf} 

3. Setting the fontfamily for pdflatex

If you are using neither lualatex nor xelatex but pdflatex, you can use:

---
fontfamily: dejavu
---

or

$ pandoc in.md --pdf-engine=pdflatex \ 
         -V 'fontfamily:dejavu' \
         -o demo.pdf

4. Setting the font for context

---
mainfont: DejaVuSerif
sansfont: DejaVuSans
monofont: DejaVuSansMono
mathfont: TeXGyreDejaVuMath
---

or

$ pandoc in.md --pdf-engine=context \
         -V 'mainfont:DejaVuSerif' \
         -V 'sansfont:DejaVuSans' \
         -V 'monofont:DejaVuSansMono' \
         -V 'mathfont:TeXGyreDejaVuMath' \
         -o out.odf

|improve this answer|||||
  • with pandoc 1.19.2.4 , I had to use --latex-engine instead of --pdf-engine – mfit Oct 11 '19 at 10:31
  • @zm0 Right, old versions of pandoc (<2.0) use that flag. With newer versions you can also use other programs beside latex to generate pdfs. See: pandoc.org/MANUAL.html#option--pdf-engine and pandoc.org/changelog.txt – DG' Oct 11 '19 at 10:46
  • Is there not any way to do this without XeLaTeX/LuaTeX? – jeffmcneill Oct 20 '19 at 6:50
5

I just saw this while looking for how to set the heading font using pandoc -> xelatex

It is possible to set the mainfont and mainfont options using metadata settings. For example, your configuration would look like this:

mainfont: Font-Regular.otf
mainfontoptions: BoldFont=Font-Bold.otf
mainfontoptions: ItalicFont=Font-Italic.otf
mainfontoptions: BoldItalicFont=Font-BoldItalic.otf

You can also use the full names of the fonts. Here is how I setup Gentium Basic as my main font:

mainfont: Gentium Basic
mainfontoptions: BoldFont=Gentium Basic Bold
mainfontoptions: ItalicFont=Gentium Basic Italic
mainfontoptions: BoldItalicFont=Gentium Basic Bold Italic
|improve this answer|||||
  • This is a nice approach, but you need to set the mainfontoptions at once or else only the last one will be taken. – DG' Jun 15 '16 at 19:40
2

Get the default tex template from pandoc by running pandoc -D latex and then add the font definitions you want to the document. save as my-font.latex. Then run pandoc with the switch --template=my-font.

|improve this answer|||||
  • I hoped to avoid fiddling around with templates for now. Actually I'm trying this right now, but I cant get them to work. Somehow my template is not found in the default folder (from --version) – Oliver Mar 24 '15 at 11:59
  • @my last comment: I fixed it.. :-) – Oliver Mar 24 '15 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.