# Short version of \emph and non standard code

The question regarding short version of \emph yielded brand new options for me and I should add impressive ones. However, the method suggested there results with non-standard code on the one hand but more readable on the other. I have two follow up questions:

1. What is the most dangerous things one should have in mind when practicing something like this?
2. More generally, what are the pros and cons of such code fragments?
-

That's quite often used in TeX/LaTeX, in fact. shorvrb, fancyvrb, listings packages use | for short verbatim text, for example. Knuth himself also use | for short verbatim in TeXbook, and use " and | for index items and labels in Concrete Math. Of course you can define your own short commands.

First of all, you should keep in mind that where they fail. In (La)TeX, only very few characters are rarely used for normal text typesetting, and then can be used for short commands. The most common two commands are | and ". They both have no use in normal text, and | can be used as \vert in math mode. Be careful that | is used in tabular column specifiers for vertical lines, and " denotes the hexadecimal numbers in TeX. And babel also redefines the meaning of " for some languages. What's more, in verbatim and \verb, the changed characters may also be problematic. Is that all? No! the characater | also has special meaning in argument of \index. I'm not sure these are all cases, you may meet more.

What's more, you should have ablility to disable the short commands by restore the catcodes. For example, shortvrb provides \DeleteShortVerb, you can disables the short verbatim easily.

And the third thing is that you must be careful about the conflictions between different packages and your own code. Since you can use shortvrb to change the meaning of |, you cannot use | for \emph at the same time. If the code is not carefully wrote, there might be more problems.

You can read the documented source code of shortvrb to know how to safely define such a short command, and think over the problems you may meet.

Some of the problems can be avoided by carefully coding. For example, directly define a short command like this:

\documentclass{article}
\begin{document}
\catcode|=\active
\def|#1|{\emph{#1}}
\begin{verbatim}
a|b|c|d
\end{verbatim}
\end{document}


you will get a ! File ended while scanning use of |. error. How to solve it? shortvrb package uses a \add@special to add the character to \dospecials list, then the verbatim will work well:

\def\add@special#1{%
\rem@special{#1}%
\expandafter\gdef\expandafter\dospecials\expandafter
{\dospecials \do #1}%
\expandafter\gdef\expandafter\@sanitize\expandafter
{\@sanitize \@makeother #1}}
\def\rem@special#1{%
\def\do##1{%
\ifnum#1=##1 \else \noexpand\do\noexpand##1\fi}%
\xdef\dospecials{\dospecials}%
\begingroup
\def\@makeother##1{%
\ifnum#1=##1 \else \noexpand\@makeother\noexpand##1\fi}%
\xdef\@sanitize{\@sanitize}%
\endgroup}


It's rather complex, indeed. You can use \add@special for your own code when needed. The code can be simpler if you just redefine one particular character like | or ". Herbert has shown another example to make sure that | work well in tabular environments, and don't forget patching array environment!

If you meet strage things, just use a command like \DeleteShortVerb. The simplest implement is to change the catcode back to 12. However, it is also difficult to define a safe \DeleteShortVerb to restore the original meaning of the character.

To prevent possible problems in other packages, you should change the catcodes as late as possible. You can use \AtBeginDocument to ensure that the catcodes are changed after preamble.

-

It's a question of personal judgment what's more readable between

some text with a \emph{word} emphasized
some text with a |word| emphasized


I still think that the former is more readable (but I use Emacs that colors and slants the argument of \emph).

If you plan to use such a shortcut, you have to take care of the usual meanings of |. You can use \vert or amsmath's \lvert and \rvert instead of | for the absolute value, but there's still the problem with tabular.

A solution, perhaps still partial, might be

\usepackage{array}
\catcode|=\active
\protected\def|{\ifmmode\vert\else\expandafter\textvert\fi}
\def\textvert#1|{\emph{#1}}


In this way, also inputs such as $|x|$ or $\left|\frac{1}{2}\right|$ will work.

Note 1. Usage of | in tabular preambles is safe, provided one loads the array package. On the other hand vertical rules in tabular environments should never (well, hardly ever) be used.

Note 2. With this setting, | won't work in verbatim mode.

-

It may always be a problem if you change the default catcode of characters. It depends to your used packages if you have to reset the catcode for some macros/environments or to use other macros:

\documentclass{article}
\catcode\|=13
\def|#1|{\emph{#1}}
\let\Tabular\tabular
\let\endTabular\endtabular
\def\tabular{\begingroup\catcode\|=12\Tabular}
\def\endtabular{\endTabular\endgroup}
\begin{document}
This is an \emph{important} test.
This is an |important| test.

\begin{tabular}[b]{|c|}
foo
\end{tabular}

This is an |important| test.

$y=\vert x\vert$

\end{document}


One should use a rarely used character für such shortcuts. | is not a good choice, / maybe better.

-
It's important to note that this method won't work if the tabular environment is used in the argument of another command; this is the usual problem with category code changes. –  egreg Jan 9 '12 at 15:49

The greatest problem I see with it is that other packages might do something similar. If I recall correctly in .dtx | is defined as a shortvrb, for instance. Other packages might do this as well and redefining | can cause problems there. The greatest issues would arrise if some package uses | as a delimiter rather than a macro. This will simply generate errors.

A smaller issue is that you don't have a direct method of writing absolute values anymore (although you could change the category code of | back in mathmode).

I don't see any real problems with this method though, as long as you take care with other packages.

Edit: As Leo says in the comments, this will cause problems when using vertical lines in a tabular. I would generally advice against using vertical lines in a tabular anyway, but it is an excellent point. Basically any place you use a | as a normal character you can't anymore, because it is now active. You can either choose some character you hardly use to minimize problems, or you can temporarily make | a letter when using it in (for example) a tabular.

-
tabular lines may have problems. for example, \begin{tabular}{|c|c|} –  Leo Liu Jan 9 '12 at 14:38