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}
-
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?
Best Answer
Here's an option using
tcolorbox
andsoulpos
, 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\}
.Correct output will require two compilations.