21

I am writing an AWK programming book and when I explain things, I refer to special AWK variables all the time, such as ORS. Is it possible to set LaTeX up in such a way that these words are always in bold in the text, so I don't have to write \bf{ORS} all the time?

doncherry
  • 54,637
  • 2
    if these words do not exist as normal words in your document, then use search and replace with an external editor. ORS=>\texttt{ORS}. It is common to write program sequences in typewriter format. –  Apr 30 '11 at 09:11
  • The question seems pretty similar to http://tex.stackexchange.com/questions/15626/highlight-language-keywords-in-a-paragraph (which is still unanswered, though) – diabonas Apr 30 '11 at 10:17
  • Related Question: Possible CSS type features?. Although it may not be useful for entire documents, or large blocks of text. – Peter Grill Oct 11 '12 at 19:48

5 Answers5

28

For a job like this I usually define a convenience macro:

\def\<#1>{\textbf{#1}}

Now you can say \<ORS> without much overhead and still don't need to define macros for all the terms that are special. In the context of a book you might want to think about how this macro can be also used to create an index. (This is a little trickier and might warrant a separate question.)

  • @Christian According to this question isn't it better to use a /newcommand instead? – pmav99 Apr 30 '11 at 09:45
  • 6
    @pmav99 I believe you cannot use \newcommand to define a macro that uses TeX's pattern matching capabilities that I've used here to make the macro easier to use than regular macros. – Christian Lindig Apr 30 '11 at 09:47
  • Oh I got it now. You are using def because it allows you to use different delimiters. EDIT we wrote together. – pmav99 Apr 30 '11 at 09:48
  • @Christian Why add two characters to the typing of the command, what is the advantage of typing \<OFS> to \OFS? – yannisl Apr 30 '11 at 11:12
  • @Yiannis Sure, but for that gain you have to define commands upfront (using whatever method). \<OFS> provides a tradeoff between defining command \OFS upfront and using \textbf{OFS} within the text. I am too lazy to maintain commands like \OFS but I can see why one would like it. – Christian Lindig Apr 30 '11 at 11:30
  • @Christian But then I would rather ...\def\*#1 {\textbf{#1}} closer to markdown. – yannisl Apr 30 '11 at 11:45
  • 4
    @Yiannis This would preclude writing variable \*OFS, \*NF, and ... because the space to terminate the argument would be missing. I'd say, this is a matter of taste. – Christian Lindig Apr 30 '11 at 12:01
  • 1
    @pmav99 the xparse package allows you to have the flexibility of arbitrary pattern matching in delimiters with the safety of \newcommand – Seamus May 28 '11 at 17:22
  • @Seamus Thank you. Would you care to provide an example? – pmav99 May 28 '11 at 17:30
12

In lieu of a ton of \newcommands, I would rather define a macro that stores a number of words for which you require them to be bold. This is simply a comma delimited list.

\documentclass{article}
\usepackage{xspace}
\begin{document}
\makeatletter
\def\boldcommandlist{\@elt FS,\@elt OFS,\@elt RS,\@elt ORS,\@elt NR,\@elt NF,\@elt FNR,}
\def\@elt#1,{%
 \expandafter\def\csname#1\endcsname{\textbf{#1}\xspace}
}
\boldcommandlist

Awk \OFS is an output equivalent of awk \FS variable. 
By default awk OFS is a single space character.
Following is an awk \OFS example. Use the command \OFS.

\makeatother
\end{document}

The \boldcommandlist holds the words to be typeset in bold:

 \boldcommandlist{FS,OFS,...}

You can always add another one if you wish at a later stage.

The \@elt is short for an element and is initially undefined. When we give it a definition, it picks up its argument as the next word (delimited by a comma) and creates a command for it. The xspace is a small package that correctly adjusts the spacing after a command catering correctly for such things as punctuation.

yannisl
  • 117,160
9

With LuaTeX, you can translate your input before it is passed on to TeX. Here is an example of doing the automatic translation in ConTeXt:

\usemodule[translate]

% Notice the space before the words
\translateinput [ OFS][ \bold{OFS}]
\translateinput [ FS] [ \bold{FS}]

\enableinputtranslation

\starttext
Awk OFS is an output equivalent of awk FS variable. 
By default awk OFS is a single space character.
Following is an awk OFS example. Use the command OFS.

\stoptext
Aditya
  • 62,301
  • 1
    Does it work when OFS is at the beginning of a line in the source? – Andrey Vihrov Apr 30 '11 at 21:00
  • @Andrey. No :( The translate module translates all strings, so if I use \translate[OFS][...] even something like \startOFS will be translated. It should be possible to write a more intelligent pattern to check if the surrounding characters are non-letters and only do the substitution. – Aditya Apr 30 '11 at 21:49
  • @Aditya, I can't find any documentation for translate. (Actually, I always get confused navigating ConTeXt documentation in general, which I think says more about me than about ConTeXt.) Does it accept a regular expression to match, or only literal text? – LSpice Jun 15 '15 at 17:18
  • @LSpice: My understanding is that it was designed to show a proof of concept. It accepts only a literal match, but generalizing to regular expressions should not be difficult. It is just a 10 line function with some boilerplate code. – Aditya Jun 15 '15 at 21:06
8

First of all, try to avoid \bf{}, use \textbf{} instead. More info here, or search for the New Font Selection Scheme

you could use \newcommand{\ORS}{\textbf{ORS}\xspace} at the preamble, and invoke it in your document with \ORS. For \xspace usage look at the link in the comments

\documentclass{article}
\usepackage{xspace}
\newcommand{\ORS}{\textbf{ORS}\xspace}

\begin{document}
Compare \ORS with ORS.

Sentence ending in \ORS.
\end{document}

Update

Updated the answer according to lockstep's suggestion.

pmav99
  • 6,060
  • 3
    By including a trailing space in your definition of \ORS, this macro can't be used before punctuation marks. Better approaches are a) using the xspace package b) using "delimited macros" -- see here for details. – lockstep Apr 30 '11 at 09:36
  • @lockstep Cool! I didn't know about xspace, although I knew that there must be a way to do it! – pmav99 Apr 30 '11 at 09:40
7

Combining Christian's answer with Yiannis, you can get a single \<...> markup which expands to different things (bold, italics,...) depending on the argument. It might be useful in some cases where you have different types of words to highlight.

\documentclass{article}
\usepackage{hyperref}% used for an example.

\def\<#1>{\csname keyword@@#1\endcsname}

\begingroup
\makeatletter
\def\do#1{\expandafter\doaux\expandafter{\keyword@style{#1}}{#1}}
\def\doaux#1#2{\global\@namedef{keyword@@#2}{#1}}   
\def\keyword@style#1{\textbf{#1}}
\do{FS}
\do{OFS}
\do{RS}
\do{ORS}
\do{NR}
\do{NF}
\do{FNR}
\def\keyword@style#1{#1\footnote{From the \<python> language.}}
\do{try}
\do{raise}
\def\keyword@style#1{\href{http://www.#1.org/}{#1}}
\do{python}
\do{CTAN}
\endgroup

\begin{document}
I don't know what \<OFS> is, nor \<RS>, but they seem to be parts 
of the awk language. On the other hand, I know a little bit about 
\<try> and \<raise>. There are many packages about \TeX\ on the
\<CTAN> website.
\end{document}
  • That is really slick! +1 – Christian Lindig May 01 '11 at 05:52
  • 1
    @Christian. The number of extra characters to type could be reduced from 3 (\<, >) to 1 (an active character of your choice), but this would mean parsing the word one letter at a time until a non-letter is seens. Not too hard, but a little slower. – Bruno Le Floch May 01 '11 at 06:13
  • 2
    Knuth used |OFS| in the TeXbook by making | active and IMHO he does not rely on parsing letter by letter. Still two extra characters but I'd find the symmetry attractive. – Christian Lindig May 01 '11 at 06:36
  • @Christian. You are right, it is definitely simpler and faster to use two characters. – Bruno Le Floch May 01 '11 at 06:51