1

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

enter image description here

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

Output

aaryan
  • 423
  • 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). – Skillmon Jan 23 '22 at 16:33

1 Answers1

2

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}

enter image description here

Skillmon
  • 60,462
  • 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? – aaryan Jan 27 '22 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. – Skillmon Jan 27 '22 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. – aaryan Jan 30 '22 at 11:15
  • @aaryan your updated code doesn't show any changed delimiters... – Skillmon Jan 30 '22 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}. – aaryan Jan 31 '22 at 12:13
  • @aaryan try to create some code that has the desired behaviour with \lstinline. If you manage that, use the same code for your environment. You'll have problems using stuff like \textsuperscript{3} in the escaped code if you use {<code-argument>} with \lstinline, instead use \lstinline[<options>]:<code-argument>: (or any other delimiter than :). – Skillmon Jan 31 '22 at 12:28