如何在 LaTeX 中模仿 StackExchange 风格的内联代码

如何在 LaTeX 中模仿 StackExchange 风格的内联代码

我喜欢 StackExchange 的方式formats inline code,它同时使用fixed width fonta gray background

我怎样才能在 LaTeX 中模仿上述句子?

一种可能是使用listingshelvet, 和inconsolata包(除了geometry包)。下面是一个尝试:

\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}

输出渲染

  • 但是,我不清楚如何获取灰色背景。我该怎么做?

  • 我还可以通过哪些其他方式改进尝试来模仿 StackExchange 风格的内联代码?

答案1

这是一个使用tcolorbox和的选项soulpos,改编自我的回答这里。它支持换行和 tcolorbox 命令可用的任何自定义。该命令可以像或\mycode一样使用,例如、、 ... 后一种语法允许包含和。\mycode{<code>}\mycode<char><code><char>\mycode|bla|\mycode!bla!<code>\{\}

\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}

代码

正确的输出需要两次编译。

答案2

需要使用 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}

上述代码的输出

答案3

有以下soul包:

在此处输入图片描述

\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}

答案4

在这里,我调整了该censor包,以介绍\seh(stack-exchange 突出显示)。结果处理换行。我注意到,虽然(就我而言)TeXworks 查看器以有些不连贯的方式显示结果,但使用 Adob​​e 等 PDF 查看器查看 PDF(如下面的结果所示)可以按预期显示。

这种方法的一个好处censor是大多数内联数学也可以直接处理。

\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}

在此处输入图片描述

相关内容