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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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

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

How do I get my braces back?

share|improve this question
\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 '14 at 11:29

You could quote the braces to make them visible:

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

lstinline with braces

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.

share|improve this answer
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
in general yes, but not in this special case – Herbert Jan 25 '12 at 17:03

Your Answer


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.