# Printing a character that has been made active in text and/or math mode?

I am not very familiar with TeX's category codes. If I use

\catcode\|=13 \renewcommand{|}{hello world}


then every use of | in my document will be replaced by hello world. However, how would I be able to still print the | sign? Of course, the circular reference

\catcode\|=13 \renewcommand{|}{hello|world}


does not work (exceeding TeX's majestic capacity), although I would like all instances of | to print as hello|world. And, if I'm correct, the above would just hold for regular text mode. How would the above code change if | was to be used in math mode?

-

There are usually two approaches taken to solve this problem.

## A

Save the character with original catcode in a macro before its catcode is changed. This works because catcodes are assigned at input time.

\documentclass{article}

\newcommand{\mypipe}{|}
\catcode\|=\active
\renewcommand{|}{hello\mypipe world}

\begin{document}

|

\end{document}


## B

Force the catcode of | to 12 with \string:

\documentclass{article}

\catcode\|=\active
\renewcommand{|}{hello\string|world}

\begin{document}

|

\end{document}


## Math mode

A character can also be only active in math mode, that is, its mathcode can be "8000. To get the original meaning in this case, one can define a control sequence to be a synonym for the previous mathcode of the character:

\mathchardef\mymathpipe=\mathcode\|


Look at the implementation of the icomma` package for an example.

-