I use underscore inside \texttt{} quite often to represent python functions, and I get quite tired of escaping every single underscore with \_. I tried the following to redefine the \texttt{} command to allow unescaped underscores, based on these answers:
Can I redefine a command to contain itself?
To escape underscore without \verb and \texttt
For some reason, it doesn't work. If I write the three lines inside the command directly in my text, it works. Any ideas to why this doesn't work?
\usepackage[T1]{fontenc}
\let\oldtexttt\texttt #'Copy' command
\renewcommand{\texttt}[1]{
\catcode_ 12\relax \oldtexttt{#1} \catcode_ 8\relax}
\texttt{hello_world}
