Like this question
But because many of my keywords has underscore, I created a macro called \keyword with \detokenize to make things easy. However, it makes underscore become longer again.
\documentclass{article}
\usepackage{lmodern}
\usepackage{relsize}
\usepackage[T1]{fontenc}
\NewDocumentCommand\keyword{sm}{\emph{\detokenize{#2}}}
\renewcommand{_}{\textscale{.6}{\textunderscore}}
\begin{document}
This method works for:\
\verb|\emph{__all__}|\quad\textrightarrow\quad \emph{__all__}\
\par
But not works for:\
\verb|\keyword{all}|\quad\textrightarrow\quad \keyword{all}\
Maybe the reason is the same as this one:\
\verb|\char_\charall\char`\char_|\quad\textrightarrow\quad \char\char_all\char\char`_\
\end{document}



\verbor\lstinlineand adjust the style (font, color, etc.) instead of implementing the commands yourself – DG' Nov 23 '20 at 11:40\emphinstead of typewrite font – Qiang Nov 23 '20 at 11:52\lstinline[language=,basicstyle=\itshape]{__all__}also has a lone underline – Qiang Nov 23 '20 at 12:14\_and define it to be a rule, you can ask tex to draw any length you wish. If you use detokenize (or\verb\char(or the latex version\symbol) etc then you are typesetting the character from the font, and asking for a different length is like asking for a narrowerMnot necessarily impossible but a question of font design, not TeX. – David Carlisle Nov 23 '20 at 12:21