I have the following issue:

% also sets some other options but this bug is indepent of them
\newcommand*{\code}[1]{PREFIX \lstinline[mathescape=true]{#1} SUFFIX}

\lstinline[mathescape=true]{test 1}

\lstinline[mathescape=true]{$test$ 2}

\code{test 3}

\code{$test$ 4}


generates the following and complains about an Undefined control sequence in test 4

test 1

test 2



Obviously I can just use \lstinline directly if necessary but I would prefer not too.

I cannot use the answer suggested here as I need to append the prefix and suffix.

Edit: I have determined that my suffix (\xpace) is not necessary and can be handled in a separate way so I will just use the linked answer.

  • What are the PREFIX and the SUFFIX?
    – egreg
    Aug 13, 2020 at 21:16
  • PREFIX is a just a string nothing fancy (which was easy to fit into the linked answer). The suffix is \xspace. Thinking about it the \xspace isn't necessary anymore. I will use the suggested answer
    – AltF4
    Aug 13, 2020 at 21:29
  • And what is \xspace supposed to do? There is no reason for using \xspace other than for parameterless macros (and it's not even useful in that case).
    – egreg
    Aug 13, 2020 at 21:32
  • \xspace performs fancy look ahead to insert whitespace if necesarry. ctan.org/pkg/xspace?lang=en
    – AltF4
    Aug 13, 2020 at 21:34
  • No, it has no function whatsoever in the case of \lstinline. A space after the closing delimiter will be respected.
    – egreg
    Aug 13, 2020 at 21:34

1 Answer 1


You can add the prefix and the suffix:




\code{$x=1$ abc}




The idea comes from https://tex.stackexchange.com/a/357339/4427.

However, note that \xspace does nothing at all except spinning a few wheels in the case of macros with parameters so you probably don't need the suffix.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .