Assign special semantics to a character

When looking for a Command that gobbles all following empty lines, I wondered if it is possible to define a shortcut for this command consisting of one single character.

Is ist possible to assign to, say, the character § the special semantics that a certain command (say, \fancystuff) is executed?

\documentclass{article}
\newcommand\fancystuff{\do\some\fancy\stuff\here}
% How to assign to § the semantics of \fancystuff ?

\begin{document}
This is text. But the following character §
gets fancy stuff done.
\end{document}

-
Search for "active character". –  Loop Space May 15 '12 at 12:12
You could make the character active and then \let it be \fancystuff, I guess. –  cgnieder May 15 '12 at 12:12
Done: tex.ac.uk/cgi-bin/texfaq2html?label=activechars . Thank you for the pointers. –  krlmlr May 15 '12 at 13:21

When the input encoding is Latin-1, the byte which is represented by § is already active, so all you need to do is to change its meaning.

% -*- coding: latin-1 -*-
\documentclass{article}
\usepackage[latin1]{inputenc}

\newcommand{\fancystuff}{\textit{Whatever}}
\protected\def§{\fancystuff}

\begin{document}
\section{The fancy § works also in section titles!}

Here's an example of §.

\end{document}


The \protected\def is to be sure the character is written as itself in the auxiliary files.

If you're using UTF-8 as encoding, the same result can be obtained with newunicodechar:

% -*- coding: utf-8 -*-
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{newunicodechar}
\DeclareRobustCommand{\fancystuff}{\textit{Whatever}}
\newunicodechar{§}{\fancystuff}

\begin{document}
\section{The fancy §{} works also in section titles!}

Here's an example of §.

\end{document}


In this case \protected\def§ would not work or, better, it would give wrong results. However the character will not be written as itself in the auxiliary files.

Whether using \DeclareRobustCommand or not depends on what § is supposed to do; if you don't plan to use it in "fragile" contexts, probably \newcommand is better.

-

Since § is not an ASCII character, you could use Unicode.

Here § has code U+00A7 in Latin-1 Supplement codepage of Unicode.

\documentclass{article}
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{00A7}{fancystuff}
\begin{document}
§
\end{document}


This should print fancystuff or the result of any command you supply as the second argument to the \DeclareUnicodeCharacter command.

-
Are there ways to achieve this with inputenc for, say, the @ character in the Latin-1 encoding? –  krlmlr May 15 '12 at 13:21

This is called "active character" in the TeX world. The procedure, as outlined in this TeX FAQ article, can be summarized as follows:

• Make the charcter active
• \def it to do whatever you want

The following MWE achieves this (using @ instead of § as special character, to avoid encoding problems):

\documentclass{article}
\pagestyle{empty}

\newcommand\fancystuff{\textbf{(pondering over fancy stuff)}}
\catcode\@=\active
\def@{\fancystuff}

\begin{document}
This is text. But the following character @
gets fancy stuff done.
\end{document}
`

-