[Tex/LaTex] Can’t escape curly braces in \lstinline

braceserrorslistings

Upon trying to typeset the following TeX I get the error:

Too many }'s. …handleClick } >Button component}

TeX:

\lstinline{<Button onClick=\{this.handleClick\}>Button component</Button>}

What am I doing wrong?

Best Answer

Using curly braces for the source code argument of \lstinline is marked as experimental in the documentation of package listings. The normal form with a character to delimit the argument works without the need to escape braces inside the code:

\documentclass{article}
\usepackage{listings}

\begin{document}
\lstinline|<Button onClick={this.handleClick}>Button component</Button>|
\end{document}

Result


The expression

\lstinline{<Button onClick=\{this.handleClick\}>Button component</Button>}

with or without the backslash does not work, because the source code argument is scanned in a special way as verbatim with changed category codes. The first } ends the argument: <Button onClick=\{this.handleClick\ the remaining part >Button component</Button>} is set outside of \lstinline and the closing curly brace without counterpart causes the error.

Related Question