TransWikia.com

Adding background color to verb or lstinline command without Colorbox

TeX - LaTeX Asked by guest_5758 on February 9, 2021

Update:

In addition to my previous post, here’s a screenshot to give you a better visual example. This was created with HTML and CSS for demo only.

In CSS, the code part is made using font-family: monospace, sans-serif;, background: #EFF0F1;, display: inline-block;, and padding: 2px 5px;.

I wonder if there’s a simple way to mimic this style in LaTeX using lstinline (or better options) without Colorbox (if possible). All instances of lstinline would have the same style.

What I mean by simple is that I thought I could just write something (once) inside lstset{} in the preamble–which is simple–rather than typing Colorbox in every instances of lstinline–which is long and awful for a novice like me. Otherwise, using Colorbox would be just fine.

sample image

Original question:

I need to create inline codes with gray background using either verb or lstinline. I’ve read here which use Colorbox which works great!

My question: Could we achieve the same effect just using lstinline alone, without Colorbox so it would be more simple?

documentclass{article}
usepackage{xcolor}
usepackage{listings}
definecolor{mygray}{rgb}{0.8,0.8,0.8}
lstset{%
basicstyle=ttfamily,
breaklines = true,
backgroundcolor=color{mygray},
}
usepackage{realboxes}
begin{document}

% demo using lstinline only
This is lstinline|my code|

% demo using lstinline and Colorbox
This is Colorbox{mygray}{lstinline|my code|}

end{document}

enter image description here

4 Answers

The following defines a new macro clist (needs package xparse) which automatically approaches the colorbox.

documentclass{article}
usepackage{xcolor,xparse}
usepackage{listings}
definecolor{mygray}{rgb}{0.8,0.8,0.8}
lstset{%
basicstyle=ttfamily,
breaklines = true,
backgroundcolor=color{mygray},
}
usepackage{realboxes}

DeclareDocumentCommand{clist}{v}{%
    Colorbox{mygray}{csname lstinlineendcsname!#1!}%
}

begin{document}

% demo using lstinline only
This is lstinline|my code|

% demo using lstinline and Colorbox
This is Colorbox{mygray}{lstinline|my code|}
clist{my code}

end{document}

Answered by TeXnician on February 9, 2021

(I rewrote this answer after the OP indicated that he/she was mainly interested in having all instances of lstinline colored automatically, i.e., without having to encase the instances in explicit Colorbox{<color-of-choice>}{lstinline...} "wrappers".)

To the best of my knowledge, the listings package currently does not offer an option or setting for rendering all instances of lstinline... against a colored background. If you are willing and able to use LuaLaTeX -- and, who knows, maybe you're already doing so -- it is straightforward to set up a Lua function that acts as a pre-processor, i.e., scans all input lines at a very early stage of processing and automatically encases all instances of lstinline|...|, verb|...|, and Verb|...| in Colorbox{mygray}{...} wrappers.

In the example code below, it is assumed that the 'arguments' of lstinline, verb, and Verb are always delimited by | ("pipe") symbols. To suspend the action of the Lua function, either use different delimiter symbols or execute the macro ColorLstinlineOff.

enter image description here

% !TeX program = lualatex
documentclass{article}
usepackage{xcolor,listings,realboxes,fancyvrb} % fancyvrb for 'Verb' macro
definecolor{mygray}{rgb}{0.8,0.8,0.8}
lstset{basicstyle=ttfamily, breaklines = true, backgroundcolor=color{mygray}}
usepackage[doublespacing]{setspace} % just for this example

usepackage{luacode} % for 'luacode' environment
begin{luacode}
-- the following code employs Lua's powerful "string.gsub" function
function color_lstinline ( s )
   s = string.gsub ( s , "lstinline%b||", "Colorbox{mygray}{%0}" ) 
   s = string.gsub ( s , "[vV]erb%b||", "Colorbox{mygray}{%0}" ) 
   return s
end
end{luacode}
%% Define 2 LaTeX macros to switch operation of Lua function on and off
newcommand{ColorLstinlineOn}{directlua{
   luatexbase.add_to_callback ( "process_input_buffer" , 
   color_lstinline, "color_lstinline" )}}
newcommand{ColorLstinlineOff}{directlua{
   luatexbase.remove_from_callback ( "process_input_buffer" , 
   "color_lstinline" )}}
AtBeginDocument{ColorLstinlineOn} % Default: activate the Lua function 

begin{document}
obeylines % just for this example
This is my lstinline|amazing| code.
This is my verb|@#$%^&*()%| code.
This is my Verb!amazing! code. 
This is my Colorbox{mygray}{lstinline!amazing!} code.

end{document}

Answered by Mico on February 9, 2021

You can patch lstinline to use Colorbox; of course you lose the possibility to break lines in lstinline.

documentclass{article}
usepackage{xpatch}
usepackage{xcolor}
usepackage{listings}
usepackage{realboxes}

definecolor{mygray}{rgb}{0.8,0.8,0.8}

lstset{
  basicstyle=ttfamily,
  backgroundcolor=color{mygray},
}

makeatletter
xpretocmdlstinline{Colorbox{mygray}bgroupapptolst@DeInit{egroup}}{}{}
makeatother

begin{document}

lstinline[language=TeX]|my code|

begin{lstlisting}[language=TeX]
my code
end{lstlisting}

end{document}

enter image description here

Answered by egreg on February 9, 2021

A simple solution would be to create an alias for the grey background when you want it

newcommand{code}[1]{Colorbox{mygray}{lstinline|#1|}}

Answered by someone on February 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP