# Using colorbox in a Listing/minipage with underscore “_”

I am having some issues with using \colorbox{}{} in combination with an underscore.

The Listing will compile without issues and highlighting the text properly, but as soon as I have an underscore in the text to be highlighted, I get the same error like 100 times.

pdflatex> l.215 ...red_3      \!\colorbox{lightred}{Man_3}!
pdflatex>                                                    . )
pdflatex> ! Extra }, or forgotten \$.


I've tried escaping that "_" with slash, but that does not help.

        \documentclass[ a4paper,                        % DinA4 Paper
% abstract      = on,           % enable abstract (not in scrbook})
draft           = false,        % status: draft, final
twoside         = false,        % set true for 2-sided
pdftex,                         % PDF
pagesize,
BCOR            = 10mm,
open            = any,          % right, left
headings        = normal,       % alt: big, normal
fontsize        = 12pt,         % font size
parskip         = full+,        % alt: half+-*
appendixprefix  = false,
chapterprefix   = false,
%titlepage,
notitlepage,                    % no cleardoublepage
numbers         = noenddot,     % no end dot in headings
% listof        = totoc,        % add LoF to ToC
% index         = totoc,        % Index
bibliography    = totoc         % Bib to ToC
]{scrbook}  % scrbook,scrreprt
\usepackage[english]{babel} % hyphenation etc.
\usepackage{color}
\definecolor{lightred}{rgb}{1.0,0.4,0.4}   % Light Redish Color
\usepackage{listings}      % llistings
\lstset{
float             = htpb,     % listings are floats, no page breaks
language         = C,
basicstyle       = \ttfamily\scriptsize,
backgroundcolor  = \color{white},
numbers          = none,
numberstyle      = \tiny\color{gray},
stepnumber       = 2,
numbersep            = 5pt,
showspaces       = false,
showstringspaces = false,
showtabs         = false,
frame                = single,
rulecolor            = \color{black},
tabsize          = 2,
captionpos       = b,
breaklines       = true,
breakatwhitespace    = false,
title                = \lstname,
keywordstyle     = \bfseries\color{black},
stringstyle      = \color{black},
escapechar       = \! ,
%escapeinside        = {\%*}{*)},
morekeywords     = {*,SEQ,...}
}

\begin{document}
\noindent\begin{minipage}{\textwidth}
\lstset{caption = {Caption},label = {label}}
\begin{lstlisting}
Hello
\!\colorbox{lightred}{Highlighted}!
\end{lstlisting}
\end{minipage}
\end{document}


The code above gets rendered as it should, but as soon as I add an "_" it does not work.

Does anybody have a clue, how I can use the colorbox on a underscored word in the example?

-
\usepackage{underscore} or \string_. Also you should better make complete example instead of letting us assemble your snippets. – Ulrike Fischer Aug 1 '12 at 17:06
I recommend you to have a look to the tcolorbox package. – Claudio Fiandrino Aug 1 '12 at 17:07
– Peter Grill Aug 1 '12 at 17:51
The <code>\usepackage{underscore}</code> works in my small example, but as I have escaped all underscores in my 100+ pages, this breaks my document... Or is there some easy way to "migrate" my document to use the underscore package? – FloatingPointZilch Aug 1 '12 at 17:55

Escaping the underscore works just fine:

## Notes:

• You have escapechar = \! which defines that you are escaping to LaTeX, hence the underscore is one of those special characters that need to be escaped. Otherwise it is interpreted as a subscript.
• Also, when composing a MWE, you should try to eliminate an unnecessary options and packages that are not required to reproduce the problem.

## Code:

\documentclass[ a4paper,                        % DinA4 Paper
% abstract      = on,           % enable abstract (not in scrbook})
draft           = false,        % status: draft, final
twoside         = false,        % set true for 2-sided
pdftex,                         % PDF
pagesize,
BCOR            = 10mm,
open            = any,          % right, left
headings        = normal,       % alt: big, normal
fontsize        = 12pt,         % font size
parskip         = full+,        % alt: half+-*
appendixprefix  = false,
chapterprefix   = false,
%titlepage,
notitlepage,                    % no cleardoublepage
numbers         = noenddot,     % no end dot in headings
% listof        = totoc,        % add LoF to ToC
% index         = totoc,        % Index
bibliography    = totoc         % Bib to ToC
]{scrbook}  % scrbook,scrreprt
\usepackage[english]{babel} % hyphenation etc.
\usepackage{color}
\definecolor{lightred}{rgb}{1.0,0.4,0.4}   % Light Redish Color
\usepackage{listings}      % llistings
\lstset{
float             = htpb,     % listings are floats, no page breaks
language         = C,
basicstyle       = \ttfamily\scriptsize,
backgroundcolor  = \color{white},
numbers          = none,
numberstyle      = \tiny\color{gray},
stepnumber       = 2,
numbersep            = 5pt,
showspaces       = false,
showstringspaces = false,
showtabs         = false,
frame                = single,
rulecolor            = \color{black},
tabsize          = 2,
captionpos       = b,
breaklines       = true,
breakatwhitespace    = false,
title                = \lstname,
keywordstyle     = \bfseries\color{black},
stringstyle      = \color{black},
escapechar       = \! ,
%escapeinside        = {\%*}{*)},
morekeywords     = {*,SEQ,...}
}

\begin{document}
\noindent\begin{minipage}{\textwidth}
\lstset{caption = {Caption},label = {label}}
\begin{lstlisting}
Hello
High_lighted
\!\colorbox{lightred}{Highlighted}!
\!\colorbox{lightred}{High\_lighted}!
\end{lstlisting}
\end{minipage}
\end{document}

-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.8596867918968201, "perplexity": 22789.983197792746}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2016-18/segments/1461860115836.8/warc/CC-MAIN-20160428161515-00040-ip-10-239-7-51.ec2.internal.warc.gz"}