5

I love the way StackExchange formats inline code, using both a fixed width font and a gray background.

How can I mimic the above sentence in LaTeX?

One possibility might be using a combination of the listings, helvet, and inconsolata packages (in addition to the geometry package). Here is an attempt:

\documentclass{article}

% Implement 1-inch margins using 'geometry' package
% ('geometry' package: https://ctan.org/pkg/geometry)
\usepackage[margin=1in]{geometry}

% Implement inline code using 'listings' package
% ('listings' package: https://ctan.org/pkg/listings)
\usepackage{listings}

% Implement Helvetica using 'helvet' package
% ('helvet' package: https://ctan.org/pkg/helvet)
% (use code snippet from https://tex.stackexchange.com/a/121063/15622/)
\usepackage[scaled]{helvet}
\renewcommand\familydefault{\sfdefault}
\usepackage[T1]{fontenc}

% Implement Inconsolata, a Consolas-like font, using 'inconsolata' package
% ('inconsolata' package: https://ctan.org/pkg/inconsolata)
\usepackage{inconsolata}
\lstset{basicstyle=\ttfamily}

\begin{document}

\section*{Attempt to mimic StackExchange inline code}
\noindent I love the way StackExchange \lstinline|formats inline code|,
using both a \lstinline|fixed width font| and \lstinline|a gray background|.

\end{document}

output rendering

  • However, I'm not sure how to obtain the gray background. How can I do this?

  • In what other ways can my attempt be improved to mimic StackExchange-style inline code?

3

6 Answers 6

4
+150

Here's an option using tcolorbox and soulpos, adapted from my answer here. It supports line breaking and any customization available to tcolorbox commands. The command \mycode can be used like \mycode{<code>} or \mycode<char><code><char>, e.g. \mycode|bla|, \mycode!bla!, ... The latter syntax allows <code> to contain \{ and \}.

\documentclass{article}

\usepackage{soulpos}
\usepackage{tcolorbox}

\newtcbox{\mybox}[1][]{
  on line,
  arc=1pt, outer arc=2pt,
  colback=lightgray!50!white, colframe=lightgray!50!white,
  boxsep=0pt, left=0pt, right=-1pt, top=2pt, bottom=0pt,
  boxrule=0pt, #1
}

\ulposdef\codeulinner[xoffset-start=1pt]{%
    \ifulstarttype{0}%
        {\tcbset{ULsiderule/.append style={leftrule=1pt}}}%
        {\tcbset{ULsiderule/.append style={leftrule=0pt,sharp corners=west}}}%
    \ifulendtype{0}%
        {\tcbset{ULsiderule/.append style={rightrule=1pt}}}%
        {\tcbset{ULsiderule/.append style={rightrule=0pt,sharp corners=east}}}%
    \mybox[ULsiderule]{\vphantom{Ap}\rule{\ulwidth}{0pt}}%
  }

\NewDocumentCommand{\mycode}{v}{%
  \begingroup\ttfamily
  \codeulinner{#1}%
  \endgroup%
  }

\begin{document}

Some code: \mycode{\command _^%&}

Some text. \mycode{This is very very very very very very very very very very very very very very very long inline code.} Some more text.
\end{document}

code

Correct output will require two compilations.

4
  • Unfortunately, I get "Undefined control sequence" from \NewDocumentCommand. Do you have any idea why this would be?
    – Andrew
    Commented Nov 14, 2023 at 22:12
  • @Andrew Your tex distribution is probably out of date. Try updating
    – mbert
    Commented Nov 14, 2023 at 22:16
  • I learned that including \usepackage{xparse} in the preamble allowed me to use \NewDocumentCommand.
    – Andrew
    Commented Nov 17, 2023 at 17:29
  • 1
    @Andrew Yes that works, however the xparse commands have been available in the kernel since October 2020 so you should really consider updating your distribution
    – mbert
    Commented Nov 17, 2023 at 18:27
10

A solution which requires the use of LuaLaTeX.

\documentclass{article}
\usepackage{xcolor}
\usepackage{luacolor}
\usepackage{lua-ul}

\NewDocumentCommand{\Code}{v}{\highLight[gray!20]{\texttt{#1}}}

\begin{document}

I love the way StackExchange \Code!formats inline code!, using both a \Code!fixed width font! and 
\Code!a gray background!. 

\end{document}

Output of the above code

4

There is the soul package:

enter image description here

\documentclass{article}
\usepackage{soul, xcolor}
\colorlet{tsecolor}{gray!20}
\sethlcolor{tsecolor}

\newcommand{\tse}[1]{\hl{\texttt{#1}}}

\begin{document}

\noindent I love the way StackExchange \tse{formats inline code},
using both a \tse{fixed width font} and \tse{a gray background}.

\end{document}
3

You can use soul and verbatim mode.

\documentclass{article}
\usepackage{soul,xcolor}

\makeatletter
\def\SOUL@hlpreamble{%
    \setul{2ex}{3.5ex}%
    \let\SOUL@stcolor\SOUL@hlcolor
    \SOUL@stpreamble
}
\makeatother

\NewDocumentCommand{\Code}{v}{%
  \begingroup\ttfamily
  \sethlcolor{black!15}%
  \hl{\,\,#1\,\,}%
  \endgroup
}

\begin{document}

\Code!\LaTeX{\{| y{!

\end{document}

enter image description here

3

Here, I adapt the censor package, to introduce \seh (stack-exchange highlight). The result handles line breaking. I note that while (in my case) the TeXworks viewer shows the result in a somewhat choppy fashion, looking at the PDF with a PDF viewer such as Adobe (shown in the result below) presents as intended.

A bonus of the censor approach is that most inline math can also be handled directly.

\documentclass{article}
\usepackage{censor,xcolor}
\makeatletter
\def\censorcolor{gray!30}
\censorruledepth=-.9\dp\strutbox% -0.3ex DEFAULT
\censorruleheight=.9\dimexpr\dp\strutbox+\ht\strutbox%  2.1ex DEFAULT
\newlength\wo@pb
\renewcommand\@cenword[1]{%
  \setlength\wo@pb{\widthofpbox{\ttfamily#1}}%
  \censorrule{\wo@pb}\kern-\wo@pb\texttt{#1}}
\def\censpace{{\color{\censorcolor}%
  \leaders\hrule height \dimexpr\censorruleheight+\censorruledepth\relax
    depth -\censorruledepth
  \hskip\fontdimen2\font plus \fontdimen3\font minus \fontdimen4\font
  \mbox{}}}
\let\sv@censpace\censpace
\let\svcensorrule\censorrule
\renewcommand\censorrule[1]{\textcolor{\censorcolor}{\svcensorrule{#1}}}
\makeatother
\let\seh\xblackout
\begin{document}
I love the way StackExchange \seh{formats inline code}, using both a 
\seh{fixed width font} and \seh{a gray background}.

One can even do this with \seh{inline math: $y=\alpha x^{2\beta} + 1$}.
\end{document}

enter image description here

1

One possibility might be to eliminate use of the listings package altogether, and use tikz to encode gray background, as in this answer to this question.

  • A disadvantage is that I don't think it's possible to typeset special characters like braces {} and brackets [] in this way.
\documentclass{article}

% Implement 1-inch margins using 'geometry' package
% ('geometry' package: https://ctan.org/pkg/geometry)
\usepackage[margin=1in]{geometry}

% Implement Helvetica using 'helvet' package
% ('helvet' package: https://ctan.org/pkg/helvet)
% (use code snippet from https://tex.stackexchange.com/a/121063/15622/)
\usepackage[scaled]{helvet}
\renewcommand\familydefault{\sfdefault}
\usepackage[T1]{fontenc}

% Implement Inconsolata, a Consolas-like font, using 'inconsolata' package
% ('inconsolata' package: https://ctan.org/pkg/inconsolata)
\usepackage{inconsolata}
%\lstset{basicstyle=\ttfamily}

% Implement gray background using \mbox and the 'tikz' package 
% ('tikz' package: https://tikz.dev/)
% (use code snippet from https://tex.stackexchange.com/a/663945/15622)
\usepackage{tikz}
\tikzset{%
  baseline,
  inner sep=2pt,
  minimum height=12pt,
  rounded corners=2pt
}
\newcommand{\code}[1]{\mbox{%
  \ttfamily
  \tikz \node[anchor=base,fill=black!12]{#1};%
}}

\begin{document}

\section*{Attempt to mimic StackExchange inline code}
\noindent I love the way StackExchange \code{formats inline code},
using both a \code{fixed width font} and \code{a gray background}.

\end{document}

output example using tikz

1
  • Unfortunately this solution does not support line breaking.
    – Sandy G
    Commented Nov 14, 2023 at 4:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .