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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 11 down vote accepted

There are usually two approaches taken to solve this problem.


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


\renewcommand{|}{hello\mypipe world}





Force the catcode of | to 12 with \string:






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:


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

share|improve this answer

Your Answer


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.