Short Styling Commands in LaTeX

In various LaTeX files I know that I’ve seen magic performed so that, for instance, code “typed |like| this” gets rendered as “typed like this”. It took me a lot of Googling, but I finally found a way to do this. The short version is:

\catcode`\|=\active
\protected\def|#1|{\textsf{#1}}

This example sets up the pipe symbol as the magic delimiter and uses the \textsf command to render pipe-enclosed text in a sans-serif font. Note that this will break vertical lines in table format specifications (e.g.), although pipes do seem to be the standard choice for the magic character. (Primary credit to StackExchange.)

If you want to do this to render the enclosed characters in “verbatim” format, the shortvrb package seems to take care of things.

According to this StackExchange answer, the above definition falls down in some corner cases (“will not work if you want verbatim text mixed with your emphasized text”) but they don’t sound like cases I care about.

Journals probably won’t be happy if you have this kind of stuff in your LaTeX files but I haven’t tried submitting anything containing commands like these.

Questions or comments? For better or worse this website isn’t interactive, so send me an email or, uh, Toot me.

To get notified of new posts, try subscribing to my lightweight newsletter or my RSS/Atom feed. No thirsty influencering — you get alerts about what I’m writing; I get warm fuzzies from knowing that someone’s reading!

Later: A Fancier Galactic Center Map

See a list of all posts.

View the revision history of this page.