# Reduce the length of underscore inside detokenize

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:\\
\par
But not works for:\\

Maybe the reason is the same as this one:\\
\verb|\char_\char_all\char_\char_|\quad\textrightarrow\quad \char_\char_all\char_\char_\\

\end{document}


• I am wondering, why you are trying to reinvent the wheel. If you want to typeset code use \verb or \lstinline and adjust the style (font, color, etc.) instead of implementing the commands yourself – DG' Nov 23 '20 at 11:40
• This is another style and I prefer it using \emph instead of typewrite font – Qiang Nov 23 '20 at 11:52
• And \lstinline[language=,basicstyle=\itshape]{__all__} also has a lone underline – Qiang Nov 23 '20 at 12:14
• if you use \_ 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 narrower M not necessarily impossible but a question of font design, not TeX. – David Carlisle Nov 23 '20 at 12:21
• I see, really impossible. – Qiang Nov 23 '20 at 12:25

You can setup listings to format the code the way you want. The option literate allows you to replace characters on the fly:

\documentclass{article}
\usepackage{lmodern}
\usepackage{relsize}

\usepackage{listings}
\lstset{
basicstyle=\itshape,
literate={_}{\textscale{.6}{\textunderscore}}1
}

\renewcommand{\_}{\textscale{.6}{\textunderscore}}

\begin{document}

\end{document}



• fontenc missing? Furthermore I would maybe add a small space around the literate substitution of _ as to make clear that there are two underscores. – campa Nov 23 '20 at 12:35
• @campa I seperate them by adding a small space \kern0.1em – Qiang Nov 23 '20 at 13:35

listings processes the input, and you can redefine the output you get for an input. Replace the xx/yy with whatever you want:

\documentclass{article}
\usepackage{lmodern}
\usepackage{relsize}

\usepackage{listings}
\lstset{
basicstyle=\itshape,
}

\makeatletter
\lst@CCPutMacro
\lst@ProcessOther {"5F}{%
\lst@ttfamily
{xx}% used with ttfamily
{yy}}% used with other fonts
\@empty\z@\@empty
\makeatother

\begin{document}

\lstinline{__all__}
\lstinline[basicstyle=\ttfamily]{__all__}

\end{document}