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'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?

share|improve this question
3  
\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

1 Answer 1

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
2  
@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

 
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.