我的空间被吞噬

我的空间被吞噬

我希望我的代码示例不仅显示\code{\textbackslash texttt}灰色框,而且还显示灰色框。您可能熟悉某些著名网页上有关 LaTeX 问题和答案的外观 :)。但是,当我的代码包含空格时,例如\code{some other code}空格似乎被吞没了。这似乎与加载的包有关。我该怎么办?

\documentclass[oneside,article]{memoir}
\usepackage{soul}
\usepackage{pgfplots}
\usepackage{ragged2e}

\newcommand\code[1]{\hl{\texttt{#1}}}
\definecolor{mygrey}{cmyk}{0,0,0,0.05}
\sethlcolor{mygrey}
\begin{document}
\noindent
I want to have my code examples show up with not only \code{\textbackslash texttt} but also a
grey box. You might be familiar with the looks from some famous web page with
questions and answers on \LaTeX\ somewhere :). However when my code contains spaces
like for example \code{some other code} the spaces seem to be swallowed. This
seem to be related to the loaded packages somehow. What can I do about it?
\end{document}

在此处输入图片描述

答案1

使用\ttfamily灵魂宏之外的工作原理:

\documentclass[oneside,article]{memoir}
\usepackage{soul}
\usepackage{pgfplots}
\usepackage{ragged2e}

\newcommand\code[1]{{\ttfamily \hl{#1}}}
\definecolor{mygrey}{cmyk}{0,0,0,0.05}
\sethlcolor{mygrey}
\begin{document}
\noindent
I want to have my code examples show up with not only \code{\textbackslash texttt} but also a
grey box. You might be familiar with the looks from some famous web page with
questions and answers on \LaTeX\ somewhere :). However when my code contains spaces
like for example \code{some other code} the spaces seem to be swallowed. This
seem to be related to the loaded packages somehow. What can I do about it?
\end{document}

在此处输入图片描述

相关内容