# My spaces are swallowed

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?

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


• I think \hl can't cope with \texttt inside it. Try changing their order: \texttt{\hl{#1}}. – Phelype Oleinik Jun 19 at 14:54
• Can confirm Phelypes suggestion works just fine. – daleif Jun 19 at 14:56
• See tex.stackexchange.com/a/436849/2388, that's the same problem and the change in ragged2e works too. – Ulrike Fischer Jun 19 at 15:07

Using \ttfamily outside of the soul macro works:

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