Search code examples
htmlcommand-linemarkdownpandocfont-family

Pandoc: Change font family to sans while converting from Markdown to HTML


I successfully get a nice formatted text I could paste anywhere using:

cat myFile.md | pandoc -s -f markdown -t html | xclip -selection clipboard -t text/html

xclip is a command line interface to X selections (clipboard). With ... -t html -o myFile.html works fine too.

I'm trying to change the font family, from the default Serif to some other Sans-serif font family. I found a lot of examples with LaTex, PDF and DOC, but no one that works in this scenario. Tried a lot of fonts (listed from fc-list : family, even after installing texlive-xetex package). The Closest answer I could find was this one.

I'm trying to just use certain parameters on CLI, trying to avoid things like --css source/styles.css.

Using pandoc 1.19.2.4 over Ubuntu 18.04.

Some --variable I tried:

-V fontfamily:arev
-V fontfamily:Ubuntu
-V fontfamilyoptions:sfdefault
-V "mainfont:DejaVuSans"
-V mainfont="DejaVu Sans Serif"
-V "sansfont:DejaVuSans"

Edit 1:

Based on mb21's answer, since Pandoc 1.12.x (source) is possible to provide more metadata to Pandoc adding a YAML block code.

On newer Pandoc versions, I also added a title key to avoid the "[WARNING] This document format requires a nonempty element.".

---
title: My File
header-includes: |
  <style>
    body {
      font-family: "Liberation Sans";
    }
  </style>
---

I still don't see the fundamental difference in this aspect between coming from Markdown instead of LaTeX, and going to HTML instead of PDF.


Solution

  • Update: This is possible in pandoc 2.11. For details, see the MANUAL, but for example:

    ---
    mainfont: sans-serif
    ---
    
    my markdown
    

    If your font name includes spaces then specify name in quotes escaped with backslash:

    ---
    mainfont: \"Sanskrit 2020\"
    ---
    

    Old answer: The font variables you mention are only for LaTeX/PDF output. To style HTML, you need CSS. You can for example put this in your markdown file:

    ---
    header-includes: |
      <style>
        body {
          font-family: sans-serif;
        }
      </style>
    ---
    
    my markdown
    
    

    Alternatively you can:

    Also: pandoc 1.19 is ancient, see https://pandoc.org/installing.html