1

Does a LaTeX3 version of the following code could be simpler? I understand the logic, but I dislike the code...

\documentclass[12pt]{article}

\usepackage{color}

% Source: https://tex.stackexchange.com/a/33478/6880 \def\test{% \begingroup \catcode`_=12\relax \ttest} \def\ttest#1{% \color{red}\fbox{#1}% \endgroup}

\begin{document}

The smallest \test{txt_example} in the world.

\end{document}

Another question. Is there a way to see an underscore because I have the following output?

enter image description here

projetmbc
  • 13,315

1 Answers1

1

In OT1 encoded fonts, the slot for _ is taken by the dot accent, unless you use \ttfamily. Use T1.

Is there a simpler version? Yes, there is.

\documentclass[12pt]{article}

\usepackage{color}

\NewDocumentCommand{\test}{v}{% \fbox{\fontencoding{T1}\selectfont\textcolor{red}{#1}}% }

\begin{document}

The smallest \test{txt_example} in the world.

\end{document}

The v argument specifier means “collect the argument verbatim”.

enter image description here

With \ttfamily instead of \fontencoding{T1}\selectfont you'd get

enter image description here

Of course you don't need to select the encoding if you have \usepackage[T1]{fontenc} in your document preamble, which is essentially mandatory for documents in (continental) European languages, including French.

egreg
  • 1,121,712