# lstinline inside new environment not able to escape maths

I am trying to create a new environment using \lstinline from the listings package. But it does not escape maths.

I have tried solutions in a couple of questions - 1, 2, but not able to get it working for my problem.

Below is the code that I am trying and its output

\documentclass[12pt,a4paper]{article}
\usepackage{listings}
\usepackage{parskip}
\lstset{mathescape,keepspaces}

\newenvironment{newEnv}[1]{\lstinline[basicstyle=\bfseries\ttfamily,escapeinside={||},mathescape=true]{#1}\\
}
{}

\begin{document}
\lstinline{let $v_1$ = 10 in $v_1$ + $v_2$}

\begin{newEnv}{let $v_1$ = 10 in $v_1$ + $v_2$}
Some random text.
\end{newEnv}
\end{document}


### Update: LaTeX escape not working

\documentclass[12pt,a4paper]{article}
\usepackage{listings}
\usepackage{parskip}
\lstset{mathescape,keepspaces}

\NewDocumentEnvironment{newEnv}{v}
{%
\begingroup
\everyeof{\noexpand}%
\expandafter
\endgroup
\scantokens
{%
\lstinline
[basicstyle=\bfseries\ttfamily,escapeinside=||,mathescape=true]%
{#1}%
}%
\\
}
{}

\begin{document}
\lstinline
[basicstyle=\bfseries\ttfamily,escapeinside=||,mathescape=true]%
{let $v_1$ = 10 in $v_1$ + $v_2$ |this should be escaped\textsuperscript{3}|}

\begin{newEnv}{let $v_1$ = 10 in $v_1$ + $v_2$ |this should be escaped\textsuperscript{3}|}
Some random text.
\end{newEnv}
\end{document}


### Output

• This can't work because the environment grabs the argument with normal category codes and forwards the argument. But \lstinline needs to read the tokens with other category codes (but tokenisation already took place by the time \lstinline starts). Jan 23 at 16:33

The following works by grabbing the argument verbatim and forwarding that to \lstinline (wrapped inside \scantokens so that \lstinline can assign whichever category codes it needs).

This solution only works for single-line verbatim arguments. If you need it to work for multi-line arguments as well I can adapt things (but it's a bit more work).

\documentclass[12pt,a4paper]{article}
\usepackage{listings}
\usepackage{parskip}
\lstset{mathescape,keepspaces}

\NewDocumentEnvironment{newEnv}{v}
{%
\begingroup
\everyeof{\noexpand}%
\expandafter
\endgroup
\scantokens
{%
\lstinline
[basicstyle=\bfseries\ttfamily,escapeinside={||},mathescape=true]%
{#1}%
}%
\\
}
{}

\begin{document}
\lstinline
[basicstyle=\bfseries\ttfamily,escapeinside={||},mathescape=true]%
{let $v_1$ = 10 in $v_1$ + $v_2$}

\begin{newEnv}{let $v_1$ = 10 in $v_1$ + $v_2$}
Some random text.
\end{newEnv}
\end{document}


• Math escape is working as expected but the latex escape is not working inside ||. e.g. if I add |\textsuperscript{3}| in the parameter of newEnv it's not rendering properly. Any suggestion for that? Jan 27 at 14:55
• @aaryan that doesn't work inside a {}-delimited \lstinline, not the fault of this code. Change the delimiters of the \lstinline call to something other than {} that you're absolutely sure will never show up in the argument (e.g., : if that is never used in your verbatim stuff, or "). Btw. the grabbing of the environment with {} is fine for this, but you could change this to two matching symbols as well. Jan 27 at 20:14
• Even after changing the delimiters it somehow does not escape the LaTeX code, what is wrong here? I have pasted the updated code in the question. Jan 30 at 11:15
• @aaryan your updated code doesn't show any changed delimiters... Jan 30 at 12:32
• Instead of {||}, now, there is only ||, and text enclosed inside || is not able to escape LaTeX code. e,g, it is not rendering \textsuperscript{3}. Jan 31 at 12:13