# lstinline loses braces when used as an argument to another command

I'm using the listings package, for the Java language (which, as all C-like languages, uses braces as a block delimiter). I intend to use \lstinline{...} as the second argument to a command for typesetting Hoare triples.

While \lstinline|while (...) { ... } | typesets the braces correctly, they are removed if \lstinline|...| is passed as argument to any macro. E.g.:

\documentclass{article}
\usepackage{listings}
\lstset{language=Java}

\newcommand{\hoare}[3]{\{ $#1$ \} #2 \{ $#3$ \} }
\newcommand{\true}{\mathbf{true}}
\newcommand{\false}{\mathbf{false}}

\begin{document}
\hoare{\true}{\lstinline|while(true) { }|}{\false}
\end{document}


How do I get my braces back?

-
\lstinline is a verbatim-like command: it tries to not interpret any special character such as %, #, {, }, etc. Unfortunately, when it is passed as an argument of another command, it doesn't get to do the magic to tell TeX not to interpret those characters: your braces already acquired a special meaning. Try \hoare{\true}{\scantokens{\lstinline|while(true) { }|}}{\false}. Not general, but should work in this particular case. –  Bruno Le Floch Jan 25 '12 at 16:27
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 2 at 11:29

\hoare{\true}{\lstinline|while(true) \{ \}|}{\false}

When directly called, \lstinline|while(true) \{ \}| would also print the backslash, but not here in the macro argument, because the outer macro already deals with the quoted braces.
then \lstinline makes no sense –  Herbert Jan 25 '12 at 16:39
@Herbert \lstinline is still useful for pretty-printing the code snippets, such as regarding listings font settings, language style such as syntax emphasizing etc. Even if you have to quote. –  Stefan Kottwitz Jan 25 '12 at 16:48