# Listing inside align environment doesn't work

I found a very specific formatting error: when I use lstinline[mathescape] inside an align environment, some of the basic math formatting like subscripting stops working. It does however work in e.g., an equation environment. The strange thing is that when I don't load the syntax package, this bug does not occur.

A complete minimal example that reproduces the problem:

\documentclass{article}

\usepackage{amsmath}
\usepackage{listings}
\usepackage{syntax}

\begin{document}

\begin{align*}
e_1 \mapsto \lstinline[mathescape]|Stream.of($e_1$)|
\end{align*}

\begin{equation*}
e_1 \mapsto \lstinline[mathescape]|Stream.of($e_1$)|
\end{equation*}

\end{document}


As you can see, the subscription inside the align envirnoment doesn't work.

Is there a way to resolve this error cleanly? What is causing it?

EDIT Based on @UlrikFisher's comment, I could find a workaround specifically for the underscore. Unfortunately, this workaround does not work for other macros. Here is a more complete example:

\documentclass{article}

\usepackage{amsmath}
\usepackage{listings}
\usepackage[nounderscore]{syntax}
\usepackage[shorthand]{semantic}

\begin{document}

\begin{align*}
e_1 \mapsto \lstinline[mathescape]+Stream.of($|[e_1|]$)+
\end{align*}

\begin{equation*}
e_1 \mapsto \lstinline[mathescape]+Stream.of($|[e_1|]$)+
\end{equation*}

\end{document}


• the body of an align environment is essentially the argument of an internal command so you can not use verbatim-like commands such as \lstinline. This is not really related to the syntax package. Nov 4, 2021 at 14:24
• you could use \usepackage[nounderscore]{syntax}. Then the package will not redefine the underscore, something which seems to disturb listings. Nov 4, 2021 at 15:00
• @DavidCarlisle okay, good to know. Still, it must be somehow (indirectly) related to the syntax package, because when I leave \usepackage{syntax} everything works fine. Nov 4, 2021 at 15:52
• or rather you got lucky, it basically doesn't work but some characters don't need the verbatim catcode changes so sort of work anyway. but with the syntax package loaded _ is made active not its usual math subscript meaning, so you get less lucky Nov 4, 2021 at 15:56

The body of an AMS alignment is essentially the argument of an internal command (so the body can be used twice as part of the alignment measuring) This means that no verb-like commands work.

Sometimes you get lucky and the argument to lstinline doesn't need any special catcode changes so that the command appears to work, but basically it can not work in this setting.

Loading teh syntax package apparently globally redefined _ with a catcode 13 definition and listings can not revert this inside an align

You can set the text in a box before starting the alignment

\documentclass{article}

\usepackage{amsmath}
\usepackage{listings}
\usepackage{syntax}

\newbox\mybox
\begin{document}

\setbox\mybox\hbox{{\lstinline[mathescape]|Stream.of($e_1$)|}}
\begin{align*}
e_1 \mapsto \usebox{\mybox}
\end{align*}

\begin{equation*}
e_1 \mapsto \lstinline[mathescape]|Stream.of($e_1$)|
\end{equation*}

\end{document}