# Microtype gives wrong letterspacing in math mode

Using the \lsstyle command gives wrong math latterspacing afterwards. I came upon this problem when using the classicthesis and putting math in a section (microtype is used there too). After the section is rendered, math letterspacing is wrong from then on.

Minimal working example

\documentclass{minimal}
\usepackage{amsmath}
\usepackage{microtype}

\DeclareMathOperator{\MyOp}{MyVeryLargeOperator}

\begin{document}
Before: $\MyOp(1,2,3)$. % this is how it should be.

{\lsstyle $X^2 + Y^2 = Z^2$} % \lsstyle and math inside a group.

After: $\MyOp(1,2,3)$. % now math has wrong letterspacing.

\end{document}


Output

Question

Any ideas how to fix this? I would especially be interesting in a fix working with classicthesis. You can see the problem here:

\documentclass[pdfspacing]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[eulermath]{classicthesis}
\usepackage{amsmath}

\DeclareMathOperator{\diag}{diagonal}

\begin{document}
\section{Usual section works fine}

$\int f(x) \mathrm dx, \; \diag(a_1, \ldots, a_n), \; \ker(f)$

\section{But some math in a title $X^2+Y^2=Z^2$ messes it up}

$\int f(x) \mathrm dx, \; \diag(a_1, \ldots, a_n), \; \ker(f)$

\end{document}


Yes, that's a bug in microtype. Until the package is updated, a temporary fix would be to add
\makeatletter\let\glb@currsize\@empty\makeatother

after the \lsstyle and math inside a group, which forces the math fonts to be set up anew.
EDIT: This has been fixed in microtype v2.6.
• I think that the fix in 2.6. is problematic (the change in \MT@set@tr@codes) see tex.stackexchange.com/questions/316231/…. Wouldn't it be better to add \aftergroup\glb@settings to the \lsstyle definition like e.g. \mathversion does? It seems to solve the problem too. – Ulrike Fischer Jun 23 '16 at 12:14