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.

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
add comment

1 Answer 1

up vote 11 down vote accepted

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.

share|improve this answer
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.