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.



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

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

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.

  • 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 '16 at 13:26

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)




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

enter image description here

  • Thanks but I am looking for more general solution for multiple programming languages so I want to use "!" character as well. – user3376620 Feb 28 '16 at 13:40
  • 1
    @user3376620: Well, I answered your question before you put into a different direction :-( – user31729 Feb 28 '16 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. – l --marc l Feb 18 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 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 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.