9

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

  • 2
    Do you want all instances of \listinline to be colored automatically, or should the coloring be performed only for select instances? – Mico Mar 7 '17 at 11:41
  • @Mico: I'd prefer all instances of \lstinline be automatically colored. I just posted an update above. – guest_5758 Mar 7 '17 at 15:23
  • Which TeX engine do you use at present? If yo're not already using LuaLaTeX, would switching to LuaLaTeX be feasible? – Mico Mar 7 '17 at 16:05
  • @Mico I'm using PDFLatex right now. Never had enough experiences with another engines. I'd tried XeLaTeX for a few times and learned KOMA-Script (which I still don't really understand), but never use LuaLaTeX before. To be honest, I don't know exactly the differences among those engines, but I think switching-over would be just fine.. – guest_5758 Mar 7 '17 at 16:29
  • Glad to learn that you're willing to give LuaLaTeX a try :-), as I just revised my answer to show how to set up a preprocessor function, using Lua code, to automatically encase all instances of lstinline|...| in \Colorbox wrappers. – Mico Mar 7 '17 at 16:44
11

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
\xpretocmd\lstinline{\Colorbox{mygray}\bgroup\appto\lst@DeInit{\egroup}}{}{}
\makeatother

\begin{document}

\lstinline[language=TeX]|\my code|

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

\end{document}

enter image description here

  • The vertical size of the box depends on whether there are any descenders or ascenders in the text. Is there a more elegant way to make the box always have the same vertical height than what I did, which is to add the term \vphantom{ly} to the patch? Secondly, I'd like to make the extra padding of the box around the text a little smaller, is there a way to achieve this? Maybe closer to the vertical and horizontal padding of the lstlisting code section – dpritch Jan 4 '18 at 21:44
  • I've created a new question about reducing the padding of the Colorbox macro at tex.stackexchange.com/q/408878 – dpritch Jan 4 '18 at 22:46
8

(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}
  • This works great! That long codes look scary though. A bit more complicated compared to \Colorbox 's approach, but works great. Plus, since I'm new to LaTeX, I have to learn more about this engine and figure out how big are the differences compared to another engines. On the other hand, \Colorbox is simpler in this case, except that I have to type in every instances of \lstinline manually which could be possibly missing. I know both has it's own pros and cons. Nevertheless, I think I could just copy-paste the codes (instead of typing manually), right? – guest_5758 Mar 7 '17 at 17:25
  • 1
    @guest_5758 - Glad this works for you. :-) Indeed, you should fee free to simply copy-and-paste the lines from \usepackage{luacode} to \AtBeginDocument{\ColorLstinlineOn} into the preamble of your document and not have to worry about \Colorbox any longer. – Mico Mar 7 '17 at 17:42
  • 1
    @guest_5758 - I've just edited the Lua code so that it also treats instances of \verb and \Verb the \lstinline gets treated. (\Verb is a macro provided by the fancyvrb package.) – Mico Mar 7 '17 at 17:47
2

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 lstinline\endcsname!#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}
  • 1
    I may have misunderstood the OP's objective, but I thought it was to avoid using \Colorbox. (You may or may not agree with this objective...) If my understanding is correct, encasing \Colorbox inside a new macro (called \clist) probably won't help achieve the OP's objective. – Mico Mar 7 '17 at 8:40
  • Yes, that's exactly what I meant. I should have written avoid using \Colorbox... – guest_5758 Mar 7 '17 at 9:34
  • 1. Does this mean that If I use this method I should not use \Colorbox elsewhere with different arguments? 2. Is the same true for \colorbox and \lstinline? – vasili111 Sep 3 '18 at 14:00
  • @vasili111 (1) No, that's absolutely unrelated. (2) Without having researched it, I would say yes. – TeXnician Sep 3 '18 at 14:20
  • 1
    @vasili111 No, not really. The comment just meant that I did understand the OP differently (I thought he just did not want to write it every time). – TeXnician Sep 3 '18 at 14:43

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.