# ,,Improper \halign inside 's with lmodern/microtype/fontspec This is less of an actual problem than a "why does this happen" question. When working on a larger document, I started getting spurious and (to me) inexplicable Improper \halign inside's. errors from amsmath's align and align* environments.

Bisecting to an MWE, I quickly found that I'd loaded both fontspec and lmodern, the latter likely left-over from before I switched to one of the Libertinus fonts. Not loading lmodern then fixed the problem, but I'm still intellectually curious as to why this happened.

Here is an MWE (I'm using XeTeX, in case it might make a difference):

%!TEX TS-program = xelatex
\documentclass{article}
\usepackage{amsmath}
\usepackage{lmodern}           % (1)
\usepackage{fontspec}          % (2)
\usepackage{microtype}         % (3)
\setmainfont{Libertinus Serif} % (4)
\begin{document}
\begin{align}
a & b
\end{align}
\end{document}


Commenting out any of the lines marked (1)--(4) makes the problem disappear (if you comment out (2) you'll also have to comment out (4), of course).

I'm quite puzzled by what's going on there behind the scenes. Can anyone shed light on this?

EDIT: This has been fixed in microtype v3.0.

This is a bug in microtype:

Instead of { and } it should use \begingroup and \endgroup in the following:

\def\MT@set@inh@list#1#2{%
\MT@ifempty\MT@inh@feat{%
\MT@map@clist@c\MT@features{\begingroup % <--
\MT@ifstreq{##1}{tr}\relax{\MT@declare@char@inh{##1}{#1}{#2}}%
\endgroup}% <--
}{%
\MT@map@clist@c\MT@inh@feat{\begingroup % <--
\KV@@sp@def\@tempa{##1}%
\MT@ifempty\@tempa\relax{%
\edef\@tempa{\csname MT@rbba@\@tempa\endcsname}%
\MT@ifstreq\@tempa{tr}\relax{%
\MT@exp@one@n\MT@declare@char@inh{\@tempa}{#1}{#2}}}%
\endgroup}% <--
}%
\MT@end@catcodes
}


which explains the four empty math atoms @David found. I'll change this for the next version, but for now adding the above to the preamble should fix it.

(BTW: Thanks for the perfect MWE!)

• ah I guessed it was { not \begingroup but \tracingall isn't always easy to read:-) Thanks for the quick fix. Mar 27 '21 at 23:00
• Excellent! You're very welcome, and thank you for the fix!
– chsk
Mar 28 '21 at 8:18

You only get the error if this is the first math expression. Some of the initial setup is happening at a delicate time. You can use a simple math expression in a discarded box.

\documentclass{article}
\usepackage{amsmath}
\usepackage{lmodern}           % (1)
\usepackage{fontspec}          % (2)
\usepackage{microtype}         % (3)
\setmainfont{Libertinus Serif} % (4)
\begin{document}
\sbox0{$x$}
\begin{align}
a & b
\end{align}
\end{document}

• I see, thank you! "Some of the initial setup is happening at a delicate time" --- spoken like a true wizard.
– chsk
Mar 27 '21 at 20:04
• @chsk I did run tracing and something is causing four {} empty math atoms but I seeing exactly where is a bit tricky it's not code I know well. the multiple font declarations are making microtype do font setup at the start of the display, I think. Mar 27 '21 at 20:21