I understand that underscore is a special character in LaTeX, but how can I input underscores within lstlisting
blocks without changing the code block itself?
Here is a short example:
Settings:
\lstset{
frame=l,
language=C++,
basicstyle=\fontsize{11}{11}\selectfont\ttfamily,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=fixed,
numbers=left,
numberstyle=\tiny\color{gray},
keywordstyle=\color{blue},
commentstyle=\color{dkgreen},
stringstyle=\color{mauve},
breaklines=true,
breakatwhitespace=true,
tabsize=4,
texcl=true
}
Code snippet:
\begin{lstlisting}
shared_ptr<vector> p; // this is a shared_ptr
\end{lstlisting}
If you could provide an answer works for both lstlisting
and lstinline
could be the best.
Best Answer
Your problem stems from the fact that you have
as part of your
listings
settings. Let's see what thelistings
documentation mentions abouttexcl
:Of course, if you have
texcl
set and_
in your comments, then you'll be forced to adhere to LaTeX's rules for underscores in text. My suggestion would be to remove thetexcl
option:Alternatively, you can use
texcl
but should be aware of special processing of LaTeX-related uses in the comments: