0

I am writing text that should be displayed as a code block with some fancy math mode characters.

\documentclass{article}
\usepackage{listings}

\begin{document}
\lstinline[mathescape=true]{Integer $\not \sim$ String}
\end{document}

This renders as expected. However, I want to wrap this use of \lstinline in a customily named command, to be able to easily change colouring, type family, etc.. for all occurrences later on. What I want is:

\documentclass{article}
\usepackage{listings}

\begin{document}
\newcommand{\codem}[1]{\lstinline[mathescape=true]{#1}}
\codem{Integer $\not \sim$ String}
\end{document}

However, this does not compile and complains with the message:

! Improper alphabetic constant.
<to be read again> 
                   \not 
l.622 \codem{Integer $\not \sim$ String}
                                        
? 
! Missing $ inserted.
<inserted text> 
                $
l.622 \codem{Integer $\not \sim$ String}
                

Why can't I wrap \lstinline here, and how can I achieve a similar abstraction?

4
  • 2
    Please always provide a full minimal example that others can copy and test as is, and not sniplets that others have to add code to to be able to test.
    – daleif
    Jan 20 at 11:59
  • 3
    Note that \codem works if you define it as \newcommand{\codem}{\lstinline[mathescape=true]}
    – daleif
    Jan 20 at 12:00
  • Awesome, why does it work if you omit the explicit parameter? Jan 20 at 12:16
  • Verbatim material needs very special care and cannot normally be used in the arguments to macros. Remember that \lstinline{foo} literally writes is arguments, so \lstinline{#1} ought to actually just write #1.
    – daleif
    Jan 20 at 13:09

0

You must log in to answer this question.

Browse other questions tagged .