Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

3 Answers

up vote 9 down vote accepted

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}
share|improve this answer
    
Thanks for this shorter code. –  projetmbc Nov 2 '11 at 19:37
add comment

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

share|improve this answer
add comment

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}
share|improve this answer
    
Thanks, that's clearer. I'm reading your document but very slowly by lake of time... –  projetmbc Nov 2 '11 at 16:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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