Search code examples
escapinglatextabular

Escaping _ efficiently in Tabular environment, LaTeX


How can you escape underscores only in the tabular environment without the use of \_?

This thread discusses about underscore in general. I cannot use the environment verbatim nor the package underscore.

Sample data

\begin{tabular}{| l | l | p{5cm} |} 
\hline 
delete_a_question.php&poistaa kysymyksen&setterit \\ \hline 
edit_question.php&muokkaa kysymyst\"{a}&getterit, HTML koodin generointia \\ \hline 
--cut--
\end{tabular}

Solution

  • \bgroup
      \catcode`\_=13%
      \def_{\textunderscore}%
      \begin{tabular}{|l|l|p{4.5cm}}
        test_444 & 555 & 4_4\\\hline
      \end{tabular}
    \egroup
    
    And now for some normal maths: $a_i=3$.
    

    Here, I change the category code of the underscore character so that it is active (this means that I can give the underscore a macro definition). I define the underscore character to output an actual underscore character.

    The \bgroup and \egroup limit the effects of redefining the underscore character.