2

Hello how do I put _ and & in codeblock?

\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 Answer 1

4

Two things:

  1. px is not a valid unit in LaTeX. Change l.17 to

    \parindent 0pt
    
  2. You don't need to escape special characters inside a lstlisting environment. You can just write, e.g.,

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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