1

There is no day in which I do not look at StackOverflow code or at Github readmes, and I really like the way the code is displayed.

I would like to achieve a similar result in latex, i.e. a monospaced text inside a light grey box which can span multiple lines.

How can I achieve this?

So far: this solution is great for displaying inline text with a sugar syntax, but I have not been able to extend it to multiple lines without getting an ugly result, display in the Minimal Working Example here below.

% !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.

This is part of the code I used for displaying this document:
\begin{lstlisting}
\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}
\end{lstlisting}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% TeX-engine: luatex
%%% End:

Output:

enter image description here

  • 1
    Use a lstlisting environment istead of \lstinline command as proposed in this answer from the question you mentioned – BambOo Mar 30 '18 at 11:07
  • I tried to do it but the result is ugly: the distance between lines is too big and each line is contained in a grey rectangle, this means that there is not an uniform grey background but multiple grey rectangles separated by a thin white line – Nisba Mar 30 '18 at 11:12
  • I see what you mean, but this could be related to your pdf viewer. In any case, could you please update you question with some image showing the output as well as a MWE of you document ? – BambOo Mar 30 '18 at 11:19
  • 1
    That's just your PDF viewer. Zoom in or out and they (re)appear. – percusse Mar 30 '18 at 11:49
  • 1
    Following what @percusse said, I have the same kind of result, but it'really a matter of the PDF viewer you use. Another solution could be to use the tcblisting of tcolorbox as described here – BambOo Mar 30 '18 at 11:57

Your Answer

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

Browse other questions tagged or ask your own question.