I wonder what is the difference or correct usage of \lstinline!...! and \lstinline{...}?

I like better {} because I feel there it's more consistent with other commands but for example in below example brackets don't work so I have to use !!.

This line is fine I can use \lstinline{inline listings with brackets}.
  \begin{tabular}{ c l }
    Example & Explanation \\ \hline
    \lstinline!$#! & This works\\
    \lstinline{$*} & But how come this doesn't? \\

Why is that? What would be the rule of thumb for !! and {} ?

2 Answers 2


The reason they exist next to each other is that

  • inside {...}, the LaTeX "special characters" are treated differently, so you cannot use $, because it is a character for math mode.
  • inside !...!, you can use anything but !.

As far as what is "good habit": whatever you want. I consider {} less confusing and would use this as default, and switch to !! only when necessary. On the other hand, if most of your entries need !!, then probably make this your default.

  • 1
    I still don't get it. If I write \lstinline{$?} and \lstinline!$!! outside of a tabular I get printed out correctly: $? $!, everything works. I am being able to use ! inside \lstinline!...! and $ inside of \lstinline{...} too (?)
    – nacho4d
    Mar 17, 2012 at 15:33
  • The Listings Package document in section 4.2 and 5.1 says {} is experimental. Now, no wonder why, even-though that document is from 2007
    – nacho4d
    Mar 17, 2012 at 15:53
  • Well, I recommend to use !...! for everything that contains special characters. Of course, you have to put e.g. :hello!: to get ! inside the argument.
    – yo'
    Mar 17, 2012 at 16:30

I found a slight difference between {} and using an arbitrary symbol as delimiter:

When trying to use mathescape inside math mode, {} behaves not as expected, !! works:



With ``!!'': \lstinline[mathescape]!a$\beta$c! in math mode gives

With ``\{\}'': \lstinline[mathescape]{a$\beta$c} in math mode gives


gives (to the amount I can represent that here)

With “!!”: aβc in math mode gives

aβc (1)

With “{}”: aβc in math mode gives

βac (2)

and the first align block compiles without error, while the second one does not. (Improper alphabetic constant and Undefined control sequence in line 15.) So, I don't see that {} handles $$ differently, as stated in the previous answer, but braces and other symbols do obviously behave differently (and it seems that there is indeed still something wrong with the braces or possibly with amsmath-align. This would need further experimentation, reading or somebody who understands the source).

Otherwise, I mostly use them as convenient, my usual way is to use {} unless my code contains braces, in which case I use !!.


You must log in to answer this question.

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