11

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}
12

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}
9

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
1

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

You can read more about this in TeX by Topic

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.