# Changing the catcode of _ in one command?

In the following code, I would like _ to be a simple letter. How can I achieve this ?

% Source : http://forum.mathematex.net/latex-f6/forcer-le-retour-a-la-ligne-dans-texttt-t13246.html#p127511

\documentclass{article}
\makeatletter
\newcommand\breakabletexttt[1]{\texttt{\breakable@texttt#1\@nil}}
\def\@gobble@fi#1\fi{\fi#1}
\def\breakable@texttt#1#2\@nil{%
#1\hspace{0pt plus 0.1pt minus 0.1pt}%
\ifx\relax#2\relax
%
\else
\@gobble@fi\breakable@texttt#2\@nil
\fi
}
\makeatother

\begin{document}

\breakabletexttt{rangerangerangerange_rangerangerange_rangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerange}

\end{document}

-

It is a general trick in @unbonpetit's solution:

% start a group and change the catcodes
\def\foo{%
\begingroup
\catcode\_=12\relax
\foowitharg}
% do the work and ends the group
\def\foowitharg#1{%
\bfseries
#1%
\endgroup}


But if you are not want to split the macro into two, you can use eTeX's extension \scantokens:

\def\foo#1{%
\textbf{\scantokens{\catcode\_=12\relax#1}}}


Either is OK.

That is to say, you can modify the definition as:

\documentclass{article}
\makeatletter
\newcommand\breakabletexttt[1]{\begingroup\ttfamily
\scantokens{\catcode\_12\makeatletter\breakable@texttt#1\@nil}%
\endgroup}
\def\@gobble@fi#1\fi{\fi#1}
\def\breakable@texttt#1#2\@nil{%
#1\hspace{0pt plus 0.1pt minus 0.1pt}%
\ifx\relax#2\relax
%
\else
\@gobble@fi\breakable@texttt#2\@nil
\fi
}
\makeatother
\begin{document}
\breakabletexttt{rangerangerangerange_rangerangerange_rangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerange}
\end{document}

-
Thanks for this shorter code. – projetmbc Nov 2 '11 at 19:37

You must change the catcode of _ before you read the argument of your macro (please, read carefuly the document I sent you).

\documentclass{article}
\makeatletter
\newcommand\breakabletexttt{\begingroup\catcode\_12 \breakabletexttt@i}
\newcommand\breakabletexttt@i[1]{\texttt{\breakable@texttt#1\@nil}\endgroup}
\def\@gobble@fi#1\fi{\fi#1}
\def\breakable@texttt#1#2\@nil{%
#1\hspace{0pt plus 0.1pt minus 0.1pt}%
\ifx\relax#2\relax
%
\else
\@gobble@fi\breakable@texttt#2\@nil
\fi
}
\makeatother
\begin{document}
\breakabletexttt{rangerangerangerange_rangerangerange_rangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerangerange}
\end{document}

-
Thanks, that's clearer. I'm reading your document but very slowly by lake of time... – projetmbc Nov 2 '11 at 16:07

You can change the catcode of a character by using

\catcode_=11


In this case 11 is there because that is the category code of letters. Normally _ is catcode 8 (which is the category for subscript). You can change it back with

\catcode_=8