5

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.

1
  • Please don't post fragments only -- any use willing to help you has to build a document around such fragments, which is tedious
    – user31729
    Feb 28, 2016 at 13:26

1 Answer 1

14

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

7
  • Thanks but I am looking for more general solution for multiple programming languages so I want to use "!" character as well. Feb 28, 2016 at 13:40
  • 1
    @user3376620: Well, I answered your question before you put into a different direction :-(
    – user31729
    Feb 28, 2016 at 13:41
  • Syntax: \lstinline[⟨key=value list⟩]⟨character⟩⟨source code⟩⟨same character⟩ Also, I found experimentally that some engines support a wider working range of working <character> delimiters. For example, I found ¿ to work with xelatex and not with pdflatex. Feb 18, 2019 at 6:43
  • @l --marc: Hm, how is this related to my answer? I used the correct syntax and usually users stick to the "!" character -- xelatex has never been an option to me. The fact that the user changed the question does not count
    – user31729
    Feb 18, 2019 at 9:38
  • 1
    @zyy: No, the !! style is a method to display verbatim content, i.e. text is displayed unexpanded, non-interpreted, e.g. _ is displayed as underscore and not indicating a subscript. You could use \lstinline+foo+ or \lstinline?foo? as well, but not \lstinline{foo} (as far as I remember)
    – user31729
    Mar 4, 2019 at 6:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .