Books in black and white
 Books Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

# A Gentle Intro duction to TEX - Doob M.

Doob M. A Gentle Intro duction to TEX - MDOOB, 1994. - 96 p.
Previous << 1 .. 16 17 18 19 20 21 < 22 > 23 24 25 26 27 28 .. 30 >> Next

7.2 Filling in with parameters

It’s possible to use macros in much greater generality by allowing parameters to be passed. The idea is somewhat similar to the template line in the \halign environment. First, let’s look at the case where there is one parameter. In this case a control sequence is defined by \def \newword# 1{...}. The symbol #1 may appear between the braces (several times) in the definition of \newword. The material between the braces acts like a template. When \newword{...} appears in the text, it will use the definition of \newword with the

57

material between the braces inserted into the template at every occurrence of #1 in the original definition. The spacing in the original definition is crucial here; there must be no spaces before the opening brace.

As an example, we could use the form letter of the last section in the following way:

\def\letter#l{

\par \noindent Dear #1,

This is a little note to let you know that your name is #1.

\hskip 2 in Sincerely yours,

\vskip 2\baselineskip \hskip 2 in The NameNoter \smallskip \hrule }

Now we can use

\letter{Michael Bishop}

\letter{Michelle L\’ev\~eque}

to get

Dear Michael Bishop,

This is a little note to let you know that your name is Michael Bishop.

Sincerely yours,

The NameNoter

58
Dear Michelle Leveque,
This is a little note to let you know that your name is Michelle Leveque.
Sincerely yours,
The NameNoter

Now let’s define \def\displaytext#l{$$\vbox{\hsize = 12 cm #1}$$}

as a new macro to display text. Then \displaytext{.. J will cause the material between the braces to be put in a paragraph with width 12 centimetres and then centred with some space added above and below as is appropriate for a display. This paragraph was set using this \displaytext macro.

The parameter of a macro can be no more than one paragraph long. If a new paragraph is encountered as part of a parameter, an error will be generated. This is a safety feature, for otherwise the accidental omission of a closing brace would cause TJjX to eat up the rest of the file as the parameter.

D> Exercise 7.4 Define a macro \yourgrade so that \yourgrade{89} will cause the following sentence to be typeset: The grade you received is 89%. It should be able to work with any other percentage, of course.

It’s not really any harder to use more than one parameter. The form used to define a new control word with two parameters is \def \newword#1#2{...}. The definition between the braces may have #1 and #2 occurring in it, perhaps several times. When \newword{...}{...} appears in the text, the material between the first set of braces replaces #1 in the definition and the material between the second set of braces replaces #2 in the definition. Here is an example followed by its result:

\def\talks#l#2{#l talks to #2.}

\talks{ JohnM Jane}

\talks{Jane}{John}

\talks{John}{me}

\talks{She}{Jane}

59

John talks to Jane. Jane talks to John. John talks to me. She talks to Jane.

D> Exercise 7.5 In a manner similar to the previous exercise, define a macro \yourgrade so that \yourgrade{89}{85} causes the following sentence to be typeset: You received a grade of 89% on your first exam and a grade of 85% on your second exam.

D> Exercise 7.6 Write a macro \frac so that \frac{a}{b} will typeset the fraction |.

It’s important not to put any spaces before the first brace in the definition. If you do, TeX will interpret the definition differently from the way described here. For more than two parameters, the method of definition is similar. To define a control word with three parameters, start with \def \newword#l#2#3{...}. Then #1, #2 and #3 may occur between the braces. When \newword{...}{...}{...} appears in the text, the material between each set of braces replaces its corresponding symbol in the definition of the control word. The parameters may go up to #9.

7.3 By any other name

Sometimes it’s convenient to be able to give a control word an alternative name. For example, if you prefer a different spelling, you might want to call the control word \centerline by the name of \centreline. This can be done by using the \let control word. The use of \let \centreline = \centerline now makes a new (as well as the old) control word available. This can also be used with mathematical names as with \let \tensor = \otimes.

It is then possible to use ÒâÕÜîîê: 206-207

$$(A \tensor Â) (C \tensor D) = AC \tensor BD.$$

to get

(A ® B)(C ® D) = AC® BD.

d> Exercise 7.7 Define control sequences \11, \cl, and \rl that are equivalent to \lef tline, \centerline, and \rightline.

60