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.

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}
share|improve this question
4  
Search for "active character". –  Andrew Stacey May 15 '12 at 12:12
1  
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
add comment

3 Answers

up vote 5 down vote accepted

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.

share|improve this answer
add comment

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.

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

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}

Result of compilation

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.