Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
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. –  Herbert Apr 30 '11 at 9:11
    
The question seems pretty similar to tex.stackexchange.com/questions/15626/… (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
add comment

5 Answers 5

up vote 26 down vote accepted

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.)

share|improve this answer
    
@Christian According to this question isn't it better to use a /newcommand instead? –  pmav99 Apr 30 '11 at 9:45
5  
@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 9: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 9:48
    
@Christian Why add two characters to the typing of the command, what is the advantage of typing \<OFS> to \OFS? –  Yiannis Lazarides Apr 30 '11 at 11:12
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
show 4 more comments

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.

share|improve this answer
    
What does \makeatother do in your example? –  Peteris Krumins Apr 30 '11 at 23:35
    
\makeatother changes the @ back to a symbol (category code 'other'). –  Yiannis Lazarides May 1 '11 at 2:59
add comment

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.

share|improve this answer
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 9: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 9:40
add comment

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
share|improve this answer
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
add comment

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}
share|improve this answer
    
That is really slick! +1 –  Christian Lindig May 1 '11 at 5:52
    
@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 1 '11 at 6:13
1  
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 1 '11 at 6:36
    
@Christian. You are right, it is definitely simpler and faster to use two characters. –  Bruno Le Floch May 1 '11 at 6:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.