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}
\lstinlinecan not be used in the argument of another command – David Carlisle Sep 03 '18 at 11:46\lstinputform 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