[Tex/LaTex] lstinline – source code

listings

I want to use \lstinline for displaying inline source code in text. This code can contain various characters from different programming languages. For example

while{$a || $b}

This text doesn't work in any of these forms because it contains start/end character of lstinline.

\documentclass{article}

\usepackage{listings}

\begin{document}
    % works
    \lstinline|while{$a && $b}|

    % doesn't work
    \lstlinline{while{$a || $b}}
    \lstlinline|while{$a || $b}|
    \lstlinline$while{$a || $b}$
\end{document}

I am looking for universal solution for multiple languages so there are many characters I can't use in this case (!, ^, ., {, |, etc…).

Is that possible with lstinline? If not, is there any good alternative? Thanks.

Best Answer

The command is \lstinline, not \lstlinline. If { and } pairs occur in the code, it's better to use the \lstinline[...]!some code! way.

The language etc. and other settings can be done in the optional argument. (the language=C setting is wrong for the given programming language -- it's not C, of course)

\documentclass{article}

\usepackage{listings}


\begin{document}

\lstinline[language=C]!while{$a || $b}!
\end{document}

enter image description here

Related Question