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}
Output
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}
\lstinline
needs to read the tokens with other category codes (but tokenisation already took place by the time\lstinline
starts).