如何在代码块中放置下划线和 &

如何在代码块中放置下划线和 &

您好,如何在代码块中放置 _ 和 &?

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{float}
\usepackage{amsmath}  
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}
\usepackage{graphicx}
\graphicspath{ {./images/} }


\colorlet{mygray}{black!30}
\colorlet{mygreen}{green!60!blue}
\colorlet{mymauve}{red!60!blue}
\parindent 0px
\lstset{
  mathescape,
  backgroundcolor=\color{gray!10},  
  basicstyle=\ttfamily,
  columns=fullflexible,
  breakatwhitespace=false,      
  breaklines=true,                
  captionpos=b,                    
  commentstyle=\color{mygreen}, 
  extendedchars=true,              
  frame=single,                   
  keepspaces=true,             
  keywordstyle=\color{blue},      
  language=c++,                 
  numbers=none,                
  numbersep=5pt,                   
  numberstyle=\tiny\color{blue}, 
  rulecolor=\color{mygray},        
  showspaces=false,               
  showstringspaces=false,
  showtabs=false,                 
  stepnumber=5,                  
  stringstyle=\color{mymauve}, 
% morecomment=[l][\color{red}]{\#},   
  tabsize=3,                      
  title=\lstname                
}

\begin{document}


\begin{lstlisting}
test\_test
test \& 
\end{lstlisting}


\end{document}

答案1

两件事情:

  1. px在 LaTeX 中不是有效单位。将 l.17 更改为

    \parindent 0pt
    
  2. 你不需要在lstlisting环境中转义特殊字符。你可以直接写,例如,

    \begin{lstlisting}
    test_test
    test & 
    \end{lstlisting}
    

相关内容