Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 !!.

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

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

share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

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.

share|improve this answer
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 '12 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 '12 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. –  tohecz Mar 17 '12 at 16:30
add comment

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:

\documentclass{article}

\usepackage{listings}
\usepackage{amsmath}

\begin{document}
With ``!!'': \lstinline[mathescape]!a$\beta$c! in math mode gives
\begin{align}
    \lstinline[mathescape]!a$\beta$c!
\end{align}

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

\end{document}

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 !!.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.