3

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}

enter image description here

5

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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.