Similar to New command to display a symbol and its command I would like to have a command to display and execute code. The code should be displayed with the listings package.
For example something like
\documentclass{scrartcl}
\usepackage{amsmath,listings}
\newcommand*{\codeExample}[1]{Code: \lstinline!\string{#1}! yields #1}
\begin{document}
\codeExample{$\lVert a\rVert$}
\end{document}
should for best of cases display $\lVert a \rVert$ as verbatim (\lstinline!...!) code and after that the rendered math code. Is something like that possible? The code above yields several mistakes. I think I miss something to avoid #1 being tokenised?


\lstinline[language={[LaTeX]TeX}]{#1}instead of the\texttt(which I prefer for further markup). Great! – Ronny Aug 28 '19 at 08:19\noexpand? It seems to work without it too. – AlexG Aug 28 '19 at 08:40\scantokensa kind of reverse operation to\detokenize? – AlexG Aug 28 '19 at 08:47\noexpanddoesn't add an extra space when the\scantokensis processed. – siracusa Aug 28 '19 at 22:58\scantokensmight break things (e.g., try\codeExample|abc|\\). While the\noexpandis supposed to fix the\scantokenseof-leakage, it also breaks immediately following macros, you can use\scantokens{#1\noexpand}\relaxas a quick-fix for this instead. – Skillmon Dec 08 '19 at 11:01\codeExample|abc|\\. – Skillmon Dec 08 '19 at 11:23