0

I need to include some inline code in a text with a background color. I use \lstinline for including code in a text and \colorbox for making a background color for it.

\lstinline!$! produces s (without gray background) and \colorbox{Gainsboro!60!Lavender}{\lstinline!$!} produces s. \lstinline!\(! produces \( (without gray background). This is everything as I need.

But there is a problem:

When I use \colorbox{Gainsboro!60!Lavender}{\lstinline!\(!} it produces only ( instead of \(. I found the solution and use \colorbox{Gainsboro!60!Lavender}{\lstinline!\\(!} to produce \(. So, there is a need to use an escape character.


Question:

Is there a way to include some inline code with background color (I need to use at least svgnames or some with more collors) in a text without to worry about the need to use escape characters in inline code?


Complite code:

\documentclass{article}

\usepackage[svgnames]{xcolor}
\usepackage{listings}

\begin{document}

\lstinline!$!

\colorbox{Gainsboro!60!Lavender}{\lstinline!$!}

\lstinline!\(!

\colorbox{Gainsboro!60!Lavender}{\lstinline!\(!}

\colorbox{Gainsboro!60!Lavender}{\lstinline!\\(!}

\end{document}
vasili111
  • 377
  • 2
    like most verbatim commands \lstinline can not be used in the argument of another command – David Carlisle Sep 03 '18 at 11:46
  • @DavidCarlisle Is there some other way to include some inline code with background color (I need to use at least svgnames or some with more collors) in a text without to worry about the need to use escape characters in inline code? – vasili111 Sep 03 '18 at 11:49
  • you can not in general escape characters to make it work, it simply doesn't work. You can use the \lstinput form and input the code fragments from files, or you could use the lrbox environment to typeset the listing in a box and then colour the box, or I think the tcolorbox package as some options to make the interface a bit easier. – David Carlisle Sep 03 '18 at 11:54
  • also https://tex.stackexchange.com/search?q=background+color+listings – David Carlisle Sep 03 '18 at 11:57
  • Found interesting examples here: https://tex.stackexchange.com/questions/51038/different-background-colors-for-lstinline – vasili111 Sep 03 '18 at 12:57

0 Answers0