# one character macros

I would like to define a macro that has at most one token in length.

\w is the macro and anything after w is not treated as part of the macro name. \wTest is same as \w Test. I don't care if no other macros starting with w are messed up(at least in my document, since I don't use any). Obviously it shouldn't interfere with other packages.

Alternatively, if it is not possible to do so, I would like to be able to redefine some special character like ^ or | to do the job. I would, say, possibly like to use it without the .

So suppose I want | to type out something

\documentclass{article}

\def\|{hello\,}

\begin{document}
\|world

\end{document}


But I'd rather do |world. (my document is so simple with just two or three special macro's that it will make it much more logical to reduce these markups to the most simple representation possible. I, for example, will never use | or ^ in my document in any normal way so I can use them to represent some markup. I only have to deal with how tex interprets them) There is no risk in my own document since it's either plane text or very few tex/latex macros(I use \vspace and \hspace and a few simple environments like centering along with 3 of my own macros for marking up the plain text).

e.g.,

I might have

This is a |test paragraph^.
\begin{center} and this^ is |centered \end{center}


where | and ^ might be rather complex(long) markup that clutters up the original paragraph. in fact, it would be nice to even get rid of the envrionments:

This is a |test paragraph^.
<and this^ is |centered>


(notice how the second one is so much more readable than the first)

-
If your documents are simple enough, you might consider using a lightweight markup language like Markdown or reStructuredText, rather than LaTeX. –  G. Poore Aug 22 '12 at 20:44

I think your first suggestion (a macro called \w such that \wTest is treated the same as \w Test) would require a lot of hacking. For the second you just have to set the catcode of the relevant character to \active.

\documentclass{article}
\catcode|=\active
\def|{abc}
\begin{document}
|
\end{document}

-
Thanks. Works! I just hope I don't ever need to use | ;) –  AbstractDissonance Aug 22 '12 at 13:07

You should drop your idea of a single symbol command \w: You would have to change the \catcode of w and this would not only affect commands which start with w but all commands with w in their name and can have quite suprising side effects even if your document itself doesn't contain any w:

\documentclass{article}
\begin{document}
\catcode\w=12
\tableofcontents
\section{abc}
\end{document}


The same is true for your other ideas: It is not enough to be sure that you will never type a specific symbol. You also must be sure that no external file (like a toc, the aux, the lof, the bbl, ....) uses it.

-

If you are using LuaTeX, then it is possible to intercept the text file before it is seen by the typesetting engine. For example, in ConTeXt you could use the following:

\usemodule[translate]

\def\wTest{default}

\translateinput[\string\w][Something else ]

\starttext
\wTest

\enableinputtranslation
\wTest \crlf
\wAgain
\disableinputtranslation

\wTest
\stoptext


which gives

See porting the luatex/ConTeXt module "translate" to luaLaTeX for a port of the translate module to luaLaTeX.

-

Do you need to do this in LaTeX? Global catcode assignments and hacks to allow for one letter macros could very well break packages. Why not add a preprocessing stage that does the replacements for you. For example, you could use a SED or a Perl script to replace < with \begin{center} prior to processing with LaTeX.

-
Well, it only breaks the packages if I use them.. and I'm not using any(well 3 but hopefully they are not using those special characters). There is a way to use <> for centering without going outside latex. Preprocessors are nice but take time to work and sometimes are not robust enough(requires a full grammar to make work). For what I'm doing I don't need to go into all that. –  AbstractDissonance Aug 22 '12 at 13:06
@AbstractDissonance You said "Obviously it shouldn't interfere with other packages." –  StrongBad Aug 22 '12 at 13:20
exactly, thats why I said if I use them... which I won't. –  AbstractDissonance Aug 22 '12 at 13:22

it would be best to choose a non-alphabetic character, one that isn't already used for something else, or only in special situations.

| is used (in pairs) for verbatim shorthand by some packages, but if you don't need that, it's a reasonable candidate. +, < or > might be good candidates. if you never need starred latex commands or environments there's *.

i'd advise staying away from these: ~, #, \$, @, %, ^, _, and of course \.

plain tex is likely to be much more forgiving than latex, but of course then you don't have the advantage of existing packages.

-
I'm using ^ and | right now. I do not use ^ in mathmode, at least yet and hopefully won't. I think I can get around it by using raisebox if I ever do(or temporarily change it back). –  AbstractDissonance Aug 22 '12 at 13:23
^ and _ are also available as \sp and \sb. –  Heiko Oberdiek Aug 22 '12 at 13:46