Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 $.
pdflatex> <recently read> \egroup 

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+-*
            1.1headlines,   
            appendixprefix  = false,
            chapterprefix   = false,    
            %titlepage,
            notitlepage,                    % no cleardoublepage 
            headinclude,
            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},     
           commentstyle     = \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?

share|improve this question
1  
\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
    
Welcome to TeX.SE. –  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

1 Answer 1

up vote 1 down vote accepted

Escaping the underscore works just fine:

enter image description here

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+-*
            1.1headlines,   
            appendixprefix  = false,
            chapterprefix   = false,    
            %titlepage,
            notitlepage,                    % no cleardoublepage 
            headinclude,
            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},     
           commentstyle     = \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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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