0

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`_\char`_all\char`_\char`_|\quad\textrightarrow\quad \char`_\char`_all\char`_\char`_\\

\end{document}

output

5
  • 1
    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
  • 2
    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
1

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}

  \verb|\emph{\_\_all\_\_}|\quad\textrightarrow\quad \emph{\_\_all\_\_}

  \verb|\lstinline{__all__}|\quad\textrightarrow\quad \lstinline{__all__}

\end{document}

enter image description here

2
  • 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
2

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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.