[Tex/LaTex] email symbol while using package listings

listssymbols

I use \usepackage{marvosym} and the command \MVAt whenever I need to write the 'at' symbol (@)

But surprisingly it doesn't work in a listings environment

For example:

\begin{lstlisting}[framexleftmargin=0mm,basicstyle=\ttfamily\small,breaklines,columns=fullflexible]
' having 1=1--
' or 1 in (select @@version)--
' union all select @@version--
' OR 'unusual' = 'unusual'
' OR 'something' = 'some'+'thing'
\end{lstlisting}

Also when it comes to ttfamily\verb I have the same problem:

\begin{ttfamily}\verb|declare @s varchar(8000) select @s = db_name() if (ascii(substring(@s,|\end{ttfamily}

Is there a solution to the problem?

Best Answer

You have to tell listings and \verb to change the @ into \MVAt:

\documentclass{article}
\usepackage{marvosym,listings,etoolbox}

\lstset{literate={@}{\MVAt}1}

\patchcmd{\verb}{\dospecials}{\dospecials\atspecial}{}{}
\def\atspecial{\begingroup\lccode`~=`@
  \lowercase{\endgroup\let~}\MVAt
  \catcode`@=\active}

\begin{document}
\begin{lstlisting}[framexleftmargin=0mm,
                   basicstyle=\ttfamily\small,
                   breaklines,
                   columns=fullflexible]
' having 1=1--
' or 1 in (select @@version)--
' union all select @@version--
' OR 'unusual' = 'unusual'
' OR 'something' = 'some'+'thing'
\end{lstlisting}

\verb|declare @s varchar(8000)|
\end{document}

There's no need to use \ttfamily around \verb.

enter image description here