[Tex/LaTex] Verbatim text can’t be shown correctly in pdf

fontsmacrosverbatim

I try to change the font of verbatim text as below

% Used by @verbatim ... @endverbatim
\newenvironment{DoxyVerb}{%
  \verbatim%
  \fontencoding{OT1}\fontfamily{ptm}\fontseries{m}\fontshape{n}%
  \fontsize{10pt}{10}\selectfont%
}{%
  \endverbatim%
  \normalsize%
}

But I don't know why some characters cannot be shown in pdf

enter image description here

The example call is the verbatim text in the above figure. The variable name should be parameter_table_build_number, but pdf shows the underscore as an upper dot. I copied that dot and paste into notepad, it shows me the underscore. I searched the underscore in pdf, and it also indicates that dot is the underscore.

However, if I change the font to \footnotesize, it show me the correct character but the verbatim text will exceeds the paper's boundary.

enter image description here

Could anybody tell me how to fix this problem? Thanks a lot.

Best Answer

You can't use \verbatim ... \endverbatim inside a macro. A detailed explanation you can find here: TeX Frequently Asked Questions - Why doesn’t verbatim work within …?

You can use packages like listings, fancyvrb ...

Here an example with fancyvrb:

\documentclass[]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{fancyvrb}

\DefineVerbatimEnvironment{DoxyVerb}{Verbatim}%
   {fontfamily=ptm,fontseries=m,fontshape=n}

\begin{document}
\begin{DoxyVerb}
get_asd_asd
\end{DoxyVerb}
\end{document}

EDIT: Using listings:

\documentclass[]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{listings}
\lstnewenvironment{lstDoxyVerb}%
    {\lstset{basicstyle=\fontsize{10pt}{10}\usefont{OT1}{ptm}{m}{n}}}%
    {}
\begin{document}


\begin{lstDoxyVerb}
get_asd_asd
\end{lstDoxyVerb}
\end{document}