8

I'm trying to highlight text using the \texttt font and a background color (pretty much like the highlighting on StackExchange [i.e., this]. I already tried it using a colorbox, it doesn't support line breaks though, resulting in:

enter image description here

Alternatively, I found a package called soul for highlighting, but in my opinion it's visually not as appealing as the colorbox because of the missing padding, it will look like this:

enter image description here

On the left is the colorbox, on the right is the soul package.

My coding:

\usepackage{color}
\usepackage{soul}

\definecolor{lightgrey}{rgb}{0.925, 0.925, 0.925}
\sethlcolor{lightgrey}

\newcommand{\codebox}[1]{\texttt{\colorbox{lightgrey}{#1}}}
\newcommand{\codebox2}[1]{\texttt{\hl{#1}}}

Any ideas how to overcome these caveats?

UPDATE: As mentioned in the comments below \texttt doesn't support line breaks as well which makes the problem even worse.

2
  • Since \texttt{} does not break anyway, this is twice worsed idea to use \colorbox and \texttt
    – user31729
    Commented Mar 4, 2016 at 11:57
  • @ChristianHupfer Thank you for this hint. But, so what do you recommend if I want that font, a background color with nice padding/margin and correct line breaking? Commented Mar 4, 2016 at 14:25

1 Answer 1

4

Soul doesn't support adjusting the sizes that determine the placement of its highlight box.

By looking at its code, I redefined one of its internal macros. I also used the trick from this answer.

\documentclass{article}
\usepackage{color}
\usepackage{soul}
\usepackage{tikz} % only to get \foreach

\definecolor{lightgrey}{rgb}{0.925, 0.925, 0.925}
\sethlcolor{lightgrey}

\makeatletter
\def\SOUL@hlpreamble{%
    \setul{}{3.5ex}% increased by 1ex
    \let\SOUL@stcolor\SOUL@hlcolor
    \dimen@\SOUL@ulthickness
    \dimen@i=-.75ex % increased by -0.25ex
    \advance\[email protected]\dimen@
    \edef\SOUL@uldepth{\the\dimen@i}%
    \let\SOUL@ulcolor\SOUL@stcolor
    \SOUL@ulpreamble
}
\makeatother

\newcommand*{\codebox}[1]{{\ttfamily\hyphenchar\font=45\relax\hl{~#1~}}}

\begin{document}
    \foreach \i in {1,...,10} {
        text text text text text \codebox{extracalifragilisticexpealidocious}
    }
\end{document}

output

Note that this may break if the authors of soul change their packet's interna and that the large boxes change the line height.

2
  • Wow, what a hack. :) But it changes the font family of the text that is followed by the codebox to the ttfamily. If you try something like this: \codebox{extracalifragilisticexpealidocious} test test test test test test test Commented Mar 8, 2016 at 9:04
  • @ScientiaEtVeritas: You're right, I didn't properly test my code. I've added a TeX group in the \codebox command to fix this issue. Also note that this command enables hyphenation in all \ttfamily text. The linked question has a solution that fixes the issue.
    – wrtlprnft
    Commented Mar 8, 2016 at 9:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .