在列表/小页面中使用带下划线“_”的颜色框

在列表/小页面中使用带下划线“_”的颜色框

\colorbox{}{}我在与下划线结合使用时遇到一些问题。

清单将顺利编译并正确突出显示文本,但只要我在要突出显示的文本中出现下划线,我就会收到 100 次相同的错误。

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

我尝试用斜线转义“_”,但没有帮助。

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

上面的代码按应有的方式呈现,但只要我添加“_”,它就不起作用。

有人知道我如何在示例中的带下划线的单词上使用颜色框吗?

答案1

转义下划线就可以了:

在此处输入图片描述

笔记:

  • 您有escapechar = \!定义要转义为 LaTeX,因此下划线是需要转义的特殊字符之一。否则它将被解释为下标。
  • 此外,在撰写平均能量损失,您应该尝试消除重现问题不需要的不必要的选项和包。

代码:

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

相关内容