Search code examples
macospdfmarkdownpandocbeamer

How do I convert a Markdown document with Japanese to Beamer?


For example, I have this Markdown document:

## Markdown test

Japanese 日本語

I run:

pandoc mwe.rmd -t beamer -o mwe.pdf --latex-engine=xelatex -V mainfont=MS\ Mincho

The words 日本語 simply disappeared in the resulted file. If I don't use the Beamer template then it works correctly.

I don't have to use pandoc. Anything that gets me from Markdown to PDF (slides) on a Mac (with MacTex) would work for me.

If there is no easy solution, I'd be okay with anything that results in non-Beamer PDF slides.


Solution

  • I'm assuming that you do have installed a font named MS Mincho on your system, and it shows up when your start the Font Book.app? (Looks like, otherwise your "normal" PDF output wouldn't work, but you said it does...)

    There are various options to check and verify, which exact font name you should use.

    1. Font Book.app (GUI application)

    1. Start Font Book.app.
    2. Type mincho into the top right search box.
    3. All installed fonts with 'Mincho' in their names show up.
    4. Click on one of the font faces (NOT the main entry!) in the list.
    5. A font sample will be displayed.
    6. Click on the button with the little i-logo.
    7. The font's metadata will display.

    From the font's metadata you can infer the PostScript name and the Full name of the font. Both should work with XeLaTeX. (I usually put quotes around font names with spaces: -V mainfont="YuMincho Medium"

    Here is a screenshot with the relevant parts of the Font Book.app UI highlighted in red. Sorry, I do not have MS Mincho installed, I can only show it with another font:

    Font Book.app, screenshot

    2. fc-list (command line utility)

    1. fc-list is a command line utility that is available via the MacPorts fontconfig package.
    2. If you have it installed, use it.
    3. To get a list of font names available for XeTeX, you can simply run:

      fc-list -f "%{family}\n"
      fc-list :outline -f "%{family}\n"
      

      The second command suppresses the listing of bitmap only fonts. Such fonts are unusable for TeX. -- For some more verbosity, and a nice formatting of the info, you could also run:

      fc-list :outline -f "  family: %{family}\nfullname: %{fullname}\n    file: %{file}\n\n"
      
    4. To get a list of names containing 'Mincho', run:

      fc-list -f "%{family}\n" | grep -i mincho
      

    Change your setup

    Now that this smaller problem ("Which font names should I use?") is out of the way, lets deal with your main one:

    • The Pandoc Beamer template (and standard Beamer itself) does not use the \setmainfont command. Therefor putting -V mainfont=... onto the Pandoc command line does not do anything.

    You can check this by querying the default internal template used by Pandoc to produce beamer output:

    $ pandoc -D beamer | less
    

    Search for a $mainfont$ variable in there and you'll find none!

    You have to modify your setup a bit to get success:

    1. First, create a simple text file named mincho.tex with the following two lines of content (I'm using my Mincho font name here, so I can really test if my advice will work):

      \usepackage{xeCJK}
      \setCJKmainfont{YuMincho Medium}
      

      The xeCJK package is required by XeLaTeX for supporting Japanese (and Chinese+Korean) fonts.

    2. Second, add -H mincho.tex to the command line so the above code snippet is included into the LaTeX code generated by Pandoc.

      This is the complete command to convert your Markdown to Beamer-PDF:

      pandoc                   \
         mwe.rmd               \
        -t beamer              \
        -o mwe.pdf             \
        --latex-engine=xelatex \
        -H mincho.tex
      

    Result (screenshot):

    mwe.pdf (screenshot)

    The fonts used by the Beamer-PDF are these:

    $ pdffonts mwe.pdf
    
      name                                type         encoding    emb  sub  uni  objID
      ----------------------------------- ------------ ----------- --- ----- ---- -----
      TZVOMD+LMSans8-Regular-Identity-H   CID Type 0C  Identity-H  yes  yes  yes   7  0
      WMSBXQ+LMSans12-Regular-Identity-H  CID Type 0C  Identity-H  yes  yes  yes  30  0
      FXCTKJ+LMSans10-Regular-Identity-H  CID Type 0C  Identity-H  yes  yes  yes  32  0
      NXJKDD+YuMin-Medium-Identity-H      CID Type 0C  Identity-H  yes  yes  no   34  0