# Macro definition not working when using LuaLaTeX and polyglossia (with \setmainlanguage{german})

I'm compiling the following MWE with LuaLaTeX in an updated TexLive 2017 installation on Ubuntu:

% !TeX program=lualatex
\documentclass[a4paper,12pt,twoside=semi,openright]{scrbook}

\usepackage{polyglossia}
\setmainlanguage{german}
\setotherlanguage{english}
\usepackage{fontspec}

\usepackage{subfig}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{bm}

\renewcommand{\vec}[1]{\bm{#1}}

\begin{document}
\begin{align}
\vec{x} = 1
\end{align}
\end{document}


Here's what I get:

The desired output would of course be x=1.

Note that this fails only when using \setmainlanguage{german}. It also works fine using pdfLaTeX and babel.

 *File List*
scrbook.cls    2017/09/07 v3.24 KOMA-Script document class (book)
scrkbase.sty    2017/09/07 v3.24 KOMA-Script package (KOMA-Script-dependent basi
cs and keyval usage)
scrbase.sty    2017/09/07 v3.24 KOMA-Script package (KOMA-Script-independent ba
sics and keyval usage)
keyval.sty    2014/10/28 v1.15 key=value parser (DPC)
tocbasic.sty    2017/09/07 v3.24 KOMA-Script package (handling toc-files)
scrsize12pt.clo    2017/09/07 v3.24 KOMA-Script font size class option (12pt)
typearea.sty    2017/09/07 v3.24 KOMA-Script package (type area)
polyglossia.sty    2015/03/25 v1.42.4 Alternative to Babel for XeLaTeX and LuaLa
TeX
etoolbox.sty    2017/01/02 v2.4 e-TeX tools for LaTeX (JAW)
makecmds.sty    2009/09/03 v1.0a extra command making commands
xkeyval.sty    2014/12/03 v2.7a package option processing (HA)
xkeyval.tex    2014/12/03 v2.7a key=value parser (HA)
fontspec.sty    2017/11/09 v2.6g Font selection for XeLaTeX and LuaLaTeX
xparse.sty    2017/11/14 L3 Experimental document command parser
expl3.sty    2017/11/14 L3 programming layer (loader)
expl3-code.tex    2017/11/14 L3 programming layer
l3pdfmode.def    2017/03/18 v L3 Experimental driver: PDF mode
fontspec-luatex.sty    2017/11/09 v2.6g Font selection for XeLaTeX and LuaLaTeX
fontenc.sty
tuenc.def    2017/04/05 v2.0i Standard LaTeX file
fontspec.cfg
ifluatex.sty    2016/05/16 v1.4 Provides the ifluatex switch (HO)
ifxetex.sty    2010/09/12 v0.6 Provides ifxetex conditional
luatexbase.sty    2015/10/04 v1.3 luatexbase interface to LuaTeX
ctablestack.sty    2015/10/01 v1.0 Catcode table stable support
gloss-german.ldf    polyglossia: module for german
babelsh.def    2013/04/30 Babel common definitions for shorthands
Taken verbatim from babel.def (2013/04/15 v3.9e)
gloss-english.ldf    polyglossia: module for english
subfig.sty    2005/06/28 ver: 1.3 subfig package
caption.sty    2016/02/21 v3.3-144 Customizing captions (AR)
caption3.sty    2016/05/22 v1.7-166 caption3 kernel (AR)
amsmath.sty    2017/09/02 v2.17a AMS math features
amstext.sty    2000/06/29 v2.01 AMS text
amsgen.sty    1999/11/30 v2.0 generic functions
amsbsy.sty    1999/11/29 v1.2d Bold Symbols
amsopn.sty    2016/03/08 v2.02 operator names
amssymb.sty    2013/01/14 v3.01 AMS font symbols
amsfonts.sty    2013/01/14 v3.01 Basic AMSFonts support
bm.sty    2017/01/16 v1.2c Bold Symbol Support (DPC/FMi)
umsa.fd    2013/01/14 v3.01 AMS symbols A
umsb.fd    2013/01/14 v3.01 AMS symbols B
***********

• Thanks! While that works, I use the ifluatex package to make the document compilable with pdfLaTeX too and to conditionally load and configure the engine specific packages in a single \ifluatexblock. Cluttering commands based on the pickiness of the engine is therefore not what I was hoping for. – Christoph90 Nov 21 '17 at 15:43
• As a general rule, amsmath, amssymb, and bm should always be loaded before both fontspec and polyglossia. – Mico Nov 21 '17 at 16:09

You can use \AtBeginDocument to delay \setmainlanguage (in opposite to babel polyglossia does language depending catcode changes immediate at least for german, so it is somehow recommended to do language selections as late as possible to avoid problems loading other packages or additional preamble definitions):

% !TeX program = lualatex

\documentclass[a4paper,12pt,twoside=semi,openright]{scrbook}

\usepackage{polyglossia}
\AtBeginDocument{%
\setmainlanguage{german}%
\setotherlanguage{english}%
}
\usepackage{fontspec}

\usepackage{subfig}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{bm}

\renewcommand{\vec}[1]{\bm{#1}}

\begin{document}
\begin{align}
\vec{x} = 1
\end{align}
\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-engine: luatex
%%% TeX-master: t
%%% End:


BTW: The problem seems to be related to a combination of lualatex, polyglossia with main language german, caption and bm. A MWE would be:

% !TeX program = lualatex
\listfiles
\documentclass{article}

\usepackage{polyglossia}
\setmainlanguage{german}
\usepackage{caption}
\usepackage{bm}

\begin{document}
$\bm{x} = 1$
\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-engine: luatex
%%% TeX-master: t
%%% End:


If you use xelatex instead of lualatex or delay \setmainlanguage{german}, e.g., using \AtBeginDocument, or load bm before polyglossia the problem does not longer exist.

So you can also use:

% !TeX program = lualatex
\listfiles
\documentclass{article}

\usepackage{bm}
\usepackage{polyglossia}
\setmainlanguage{german}
\usepackage{caption}
\usepackage[german=quotes]{csquotes}
\begin{document}
$\bm{x} = 1$
\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-engine: luatex
%%% TeX-master: t
%%% End:


You should not use \AtBeginDocument to load a package, because unused global options are reported before execution of the \AtBeginDocument hook. And you cannot use \AtBeginDocument to load package csquotes. But you can use \AtEndPreamble of etoolbox, because the corresponding hook is executed at the very beginning of \begin{document}:

% !TeX program = lualatex
\listfiles
\documentclass{article}

\usepackage{etoolbox}
\usepackage{bm}
\usepackage{polyglossia}
\AtEndPreamble{\setmainlanguage{german}}
\usepackage{caption}
\AtEndPreamble{\usepackage[german=quotes]{csquotes}}
\begin{document}
$\bm{x} = 1$
\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-engine: luatex
%%% TeX-master: t
%%% End:


Last but not least, you can use babel instead of polyglossia to have German as main language and English as second language with lualatex too:

% !TeX program = lualatex
\listfiles
\documentclass{article}

\usepackage[main=ngerman,english]{babel}
\usepackage{bm}
\usepackage{caption}
\usepackage[german=quotes]{csquotes}
\begin{document}
$\bm{x} = 1$
\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-engine: luatex
%%% TeX-master: t
%%% End:

• Any change to make this work when also using \usepackage[german=quotes]{csquotes} ? Through trail and error i could not find any place in the preamble to put this without breaking the workaround and throwing Undefined control sequence. \begin{document}' – Christoph90 Nov 21 '17 at 16:27
• Ah, I did not try putting csquotes there as well. When inserting \usepackage[german=quotes]{csquotes} in the \AtBeginDocument block ofyour first code snippet, I get: Can be used only in preamble. {}}} and Can be used only in preamble. \AtEndPreamble – Christoph90 Nov 21 '17 at 16:34
• @Christoph90 csquotes examples added. – Schweinebacke Nov 21 '17 at 16:39
• Thanks! I accepted your answer because of it's completeness for all my additional concerns. Would you consider (for future readers) to move the note about delaying \setmainlanguage{german} after the package loading instructions to the top of your post (and even better to point to egregs answer for an explanation)? – Christoph90 Nov 21 '17 at 17:04
• @Christoph90 Using \AtBeginDocument (to delay \setmainlanguage) was already at the top of my answer. However I've changed it to make it (hopefully) more clear. – Schweinebacke Nov 21 '17 at 17:16

Under LuaLaTeX, the packages amsmath, amssymb, and bm should always be loaded before both fontspec and polyglossia. (It also used to be standard to advise to load fontspec after any packages that modify math-related macros -- such as, of course, amsmath, amssymb, and bm. However, at some point in the recent past, an update to fontspec has occurred which no longer makes this recommendation a necessity.)

If, for some reason, you simply must load polyglossia first, then at least defer the instructions \setmainlanguage{...} and \setotherlanguage{...} to a point later on in the preamble, i.e., after amsmath, amssymb, bm and fontspec have been loaded.

Do note that I deliberately started the answer with "Under LuaLaTeX, ...": I'm afraid I have no experience with package loading orderings under XeLaTeX.

The following, modified version of your MWE compiles fine:

% !TeX program=lualatex
\documentclass[a4paper,12pt,twoside=semi,openright]{scrbook}

\usepackage{amsmath,amssymb,bm}
\usepackage{fontspec}

\usepackage{polyglossia} % load it *after* amsmath, amssymb, bm, and fontspec
\setmainlanguage{german}
\setotherlanguage{english}

\usepackage{subfig} % it doesn't seem to matter when this package is loaded

\renewcommand{\vec}[1]{\bm{#1}}

\begin{document}
$\vec{x} = 1$
\end{document}

• Great answer and thanks for the remark on loading the ams packages first! – Christoph90 Nov 21 '17 at 17:05

The problem is that \setmainlanguage{german} sets " to a shorthand character and loading bm after that declaration completely breaks the definitions made by the package when LuaLaTeX is used.

The bizarre string you get is essentially random. Without loading subfig you get nothing (but no x either).

Loading amsmath and bm before or after polyglossia is irrelevant, as long as the language declarations are made after package loading.

\documentclass{article}

\usepackage{polyglossia}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{bm}

\usepackage{subfig}

\setmainlanguage{german}

\renewcommand{\vec}[1]{\bm{#1}}

\begin{document}

$\vec{x}$

\end{document}


Try moving \usepackage{bm} after \setmainlanguage{german} and the issue will appear again.

• Great answer and insight! This is how I actually did it, though it required me to give up on bundling all LuaLaTeX specific commands in a single block and use a second \ifluatex \fi block after loading both packages to make it compatible with pdfLatex too. I accepted @Schweinebackes answer, since it did contained the part on moving \setmainlanguage{german} (though without the explanation of the actual problem). – Christoph90 Nov 21 '17 at 17:09
• @Christoph90 A well formed preamble should have the package loading separate from the settings part. Mixing them is a sure recipe for code duplication and confusion. – egreg Nov 21 '17 at 23:28
• Is it safe to set package settings via optional arguments on loading then, as for example \usepackage[ngerman]{babel}? – Christoph90 Dec 19 '17 at 13:27
• @Christoph90 polyglossia doesn't fetch languages from document class options; languages are only specified with \setmainlanguage and \setotherlanguage`. – egreg Dec 19 '17 at 13:34