# How do you define macros to toggle a character active and define a macro for it?

I'm experimenting with core tex in order to learn some basics.

My goal is to define a shorthand environment which makes use of active characters. The general idea is to activate the used characters and defining macros upon entering the environment while reverting the character codes upon leaving it.

As simple as that sounds in theory, I'm running into a

"Missing control sequence inserted" ("\inaccessible")


error with the following imo relatively straight forward example (for a {\bfseries ...} shorthand)

\documentclass{minimal}

\def\beginshorthand{%

\makeatletter

\def\shorthand@catcode@plus{\the\catcode+}

\catcode\+=\active
\def+##1+{{\bfseries ##1}}

\makeatother

}
\def\endshorthand{%

\makeatletter

\catcode\+=\shorthand@catcode@plus

\makeatother

}

\begin{document}

\beginshorthand
+ bold span +
\endshorthand

+ plain span +

\end{document}


(in TeXShop 3.11, Mac OS X 10.9.3)

Is it possible? If so, how do i make it work?

• Note: this is not plain. You are doing low-level TeX programming, but still using LaTeX. – Joseph Wright Jun 11 '14 at 20:53
• This answer tex.stackexchange.com/questions/176739/… shows my method for building toggles to turn on and off active characters. Here is another example of it: tex.stackexchange.com/questions/172826/… – Steven B. Segletes Jun 12 '14 at 0:19
• @FK82 Well they are primitives or low-level TeX programming, but that's not the same as plain TeX, which is specifically the format defined by Knuth and not the same as LaTeX. – Joseph Wright Jun 12 '14 at 9:56
• Actually, this answer tex.stackexchange.com/questions/169093/… is much closer to your apparent application than the other two I cited. Why don't you look at that, and see if you still want the verbatim focus you mentioned in your last comment – Steven B. Segletes Jun 12 '14 at 12:46
• unless redefined (which isn't trivial), a blank line is equivalent to \par, and only \long macros are able to incorporate \par. this is built into the tex machinery, and your question is tagged "tex core". if you want the visual effect of an empty line for improved readability, then entering a % sign on a line (that contains nothing else but spaces) will usually suffice; this does not work if \obeyspaces or \obeylines is in effect, as within a verbatim context. (for learning about core basics, victor eijkhout's "tex by topic" is recommended.) – barbara beeton Jun 12 '14 at 14:35

There are several errors in your code.

1. When the replacement text for a macro is absorbed, category codes are fixed once and for all, so

\shorthand@catcode@plus


in the definition of \beginshorthand is several tokens and not one:

\shorthand • @ • c • a • t • c • o • d • e • @ • p • l • u • s


so when \beginshorthand is expanded, TeX defines \shorthand to be followed by @catcode@plus as parameter text

2. You should use

\edef\shorthand@catcode@plus{\the\catcode+ }


in order to access the current value of the category code; note the space after +, which is almost mandatory, or when \shorthand@catcode@plus is expanded, TeX won't try to expand prematurely the next token.

3. In any case, + has category code 12 in all the replacement text, so \def+ is illegal.

The first problem is easily solved by placing \makeatletter and \makeatother outside the definition that needs them.

The fixed category code problem is usually solved by a \lowercase trick (see https://tex.stackexchange.com/a/19750/4427)

\documentclass{article}

\makeatletter
\def\beginshorthand{%
\edef\shorthand@catcode@plus{\the\catcode+ }%
\begingroup\lccode~=+
\lowercase{\endgroup\def~##1~{\textbf{##1}}}%
\catcode+=\active
}
\def\endshorthand{%
\catcode\+=\shorthand@catcode@plus
}
\makeatother

\begin{document}

\beginshorthand
+ bold span +
\endshorthand

+ plain span +

\end{document}


However, I'd prefer to use grouping in order to limit the category code change:

\documentclass{article}

\makeatletter
\def\beginshorthand{%
\begingroup
\begingroup\lccode~=+
\lowercase{\endgroup\def~##1~{\textbf{##1}}}%
\catcode+=\active
}
\def\endshorthand{%
\endgroup
}
\makeatother

\begin{document}

\beginshorthand
+ bold span +
\endshorthand

+ plain span +

\end{document}


With grouping you don't need to restore the old category code manually, because TeX will do it by itself.

A different approach, without the \lowercase trick, but which has the defect that it defeats all patching commands of etoolbox, xpatch or regexpatch is activating + beforehand in a group and doing a global definition:

\documentclass{article}

\makeatletter
\begingroup
\catcode+=\active
\gdef\beginshorthand{%
\begingroup
\def+##1+{\textbf{##1}}%
\catcode+=\active
}
\endgroup
\def\endshorthand{%
\endgroup
}
\makeatother

\begin{document}

\beginshorthand
+ bold span +
\endshorthand

+ plain span +

\end{document}

• Ok, didn't know about (1.). That explains a lot. I'm not a big fan of the lowercase trick tbh. Is it the only real option here? – FK82 Jun 12 '14 at 10:05
• @FK82 Not necessarily, but it's a good trick – egreg Jun 12 '14 at 10:42
• @FK82 why object to the use of \lowercase? it's the main use of \lowercase in tex (in the latex.ltx source of latex for example \lowercase appears 7 times and only 1 of them is related to lowercasing text, the others are all to do with generating tokens with specific catcodes. – David Carlisle Jun 12 '14 at 12:37
• @DavidCarlisle Well, I'm not making a case against the lowercase trick. I know way too little about TeX (or LaTeX for that matter) for that to happen. But in general coding best practice I think it's not a good idea to base the working of any code on a hack/trick (unless there is no other way). – FK82 Jun 12 '14 at 17:04
• @FK82 Quite a number of these 'tricks' are 'standard practice' for TeX programmers: read Appendix D of The TeXbook to get a feel for this. – Joseph Wright Jun 22 '14 at 15:35