# Defining a character as a macro and preserving its original function

I would like to redefine a character (say .) in such a way that it does its original function and also performs another task (for example runs certain command, say \mycommand).

I wrote the following, but it does not work. How I can correct this code.

\documentclass{article}
\newcommand\mycommand{OK}

\catcode\.=\active
\def.{.\mycommand}%

\begin{document}
Hello.
\end{document}


There are several ways to do it. One is

\begingroup\lccode~=. \lowercase{\endgroup\def~}{.\mycommand}
\catcode.=\active


The ~ will become an active period after \lowercase has performed its action, because it only changes character codes and ~ is by default active. The period in the replacement text still has its normal category code, which is permanently attached to it.

However you should not make the period active. Any subsequent command such as \hspace{1.5em} will raise puzzling errors.

• This approach is so fantastical, I am forced to try to remember it once again. I was even looking up your prior use of it, tex.stackexchange.com/questions/528480/…, when you posted this answer. Mar 4, 2020 at 11:35
• Would you mind if you could also write your original solution (which was as a comment to my question and was deleted afterward) either as a new answer. It could be potentially helpful to users.
– Name
Mar 4, 2020 at 13:02
• @Name I don't think so, sorry. Activating characters is bad practice in almost every case, unless done in a very controlled way (like babel does). Mar 4, 2020 at 13:07