[Tex/LaTex] “TeX capacity exceeded” error when converting markdown to PDF using XeLaTeX with russian language

markdownpandocpdfrussianxetex

I've installed pandoc and texlive-full packages on recent Ubuntu 14.04 LTS 64bit to convert markdown files with russian language to PDF.

Everything works fine when I run the following command:

pandoc --latex-engine=xelatex --toc --chapters -V mainfont="Ubuntu" -f markdown -o test.pdf test.md

But in order to have russian language also in table of contents, in pictures' descriptions and etc I specify lang variable like this:

pandoc --latex-engine=xelatex --toc --chapters -V mainfont="Ubuntu" -V lang="russian" -f markdown -o test.pdf test.md

and execution ends with the following message:

pandoc: Error producing PDF from TeX source.
! TeX capacity exceeded, sorry [input stack size=5000].
<inserted text> 
                -1
l.61 \tableofcontents

Everything works again if I specify another language, e.g. french or dutch:

pandoc --latex-engine=xelatex --toc --chapters -V mainfont="Ubuntu" -V lang="french" -f markdown -o test.pdf test.md

It doesn't matter what contents of test.md are. They could be a simple text file:

# Первая глава

Текст первой главы.

# Вторая глава

Текст второй главы.

# Третья глава

Текст третьей главы.

I use default latex template:

pandoc -D latex > mytemplate.tex

and guess that something goes wrong when main language is set:

\ifxetex
    \usepackage{polyglossia}
    \setmainlanguage{$mainlang$}
\else
    \usepackage[$lang$]{babel}
\fi

Am I doing something wrong or is it a bug? Unfortunately, I was unable to find workaround to fix the problem. As I can see there is a lot of issues that may cause this error. I'm absolute beginner in pandoc, TeX and all related things so any help will be appreciated!

UPDATE

Tried on Windows 7 Professional 32bit using MiKTeX as recommended by pandoc documentation specifying -V mainfont="Times New Roman" for font and got exactly the same error…

Best Answer

The problem seems to reduce to this minimal example:

\documentclass{book}
\usepackage{fontspec}
\usepackage{polyglossia}

\defaultfontfeatures{Scale=MatchLowercase}

\setmainfont{Linux Libertine O}
\setmainlanguage{russian}

\begin{document}
x
\end{document}

which aborts with

! TeX capacity exceeded, sorry [input stack size=5000].
<to be read again> 
                   {

If the Scale=MatchLowercase option is removed, the document compiles. If I save the LaTeX file from your test with

pandoc --latex-engine=xelatex --toc --chapters -V mainfont="Linux Libertine O" -V lang="russian" -f markdown -w latex -s test.md > test.tex

and then remove the option, running XeLaTeX on test.tex is successful.

This is quite probably a bug in the Russian module for Polyglossia.

Related Question