home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / riscbsd / 1_1_contri / usd / 25_trofftu / tt08 < prev    next >
Encoding:
Text File  |  1986-05-23  |  2.6 KB  |  159 lines

  1. .\"    @(#)tt08    6.1 (Berkeley) 5/23/86
  2. .\"
  3. .NH
  4. Introduction to Macros
  5. .PP
  6. Before we can go much further in
  7. .UL troff ,
  8. we need to learn a bit about the
  9. macro
  10. facility.
  11. In its simplest form, a macro is just a shorthand notation
  12. quite similar to a string.
  13. Suppose we want every paragraph to start
  14. in exactly the same way _
  15. with a space and a temporary indent of two ems:
  16. .P1
  17. ^sp
  18. ^ti +2m
  19. .P2
  20. Then to save typing, we would like to collapse these into
  21. one shorthand line,
  22. a
  23. .UL troff
  24. `command' like
  25. .P1
  26. ^PP
  27. .P2
  28. that would be treated by
  29. .UL troff
  30. exactly as
  31. .P1
  32. ^sp
  33. ^ti +2m
  34. .P2
  35. .BD .PP
  36. is called a
  37. .ul
  38. macro.
  39. The way we tell
  40. .UL troff
  41. what
  42. .BD .PP
  43. means is to
  44. .ul
  45. define
  46. it with the
  47. .BD .de
  48. command:
  49. .P1
  50. ^de PP
  51. ^sp
  52. ^ti +2m
  53. ^^
  54. .P2
  55. The first line names the macro
  56. (we used
  57. .BD .PP ' `
  58. for `paragraph',
  59. and upper case so it wouldn't conflict with
  60. any name that
  61. .UL troff
  62. might
  63. already know about).
  64. The last line
  65. .BD ..
  66. marks the end of the definition.
  67. In between is the text,
  68. which is simply inserted whenever
  69. .UL troff
  70. sees the `command'
  71. or macro call
  72. .P1
  73. ^PP
  74. .P2
  75. A macro
  76. can contain any mixture of text and formatting commands.
  77. .PP
  78. The definition of
  79. .BD .PP
  80. has to precede its first use;
  81. undefined macros are simply ignored.
  82. Names are restricted to one or two characters.
  83. .PP
  84. Using macros for commonly occurring sequences of commands
  85. is critically important.
  86. Not only does it save typing,
  87. but it makes later changes much easier.
  88. Suppose we decide that the paragraph indent is too small,
  89. the vertical space is much too big,
  90. and roman font should be forced.
  91. Instead of changing the whole document,
  92. we need only change the definition of
  93. .BD .PP
  94. to
  95. something like
  96. .P1
  97. ^de PP    \e" paragraph macro
  98. ^sp 2p
  99. ^ti +3m
  100. ^ft R
  101. ^^
  102. .P2
  103. and the change takes
  104. effect everywhere we used
  105. .BD .PP .
  106. .PP
  107. .BD \e"
  108. is a
  109. .UL troff
  110. command that causes the rest of the line to be ignored.
  111. We use it here to add comments to the macro
  112. definition
  113. (a wise idea once definitions get complicated).
  114. .PP
  115. As another example of macros,
  116. consider these two which start and end a block of offset,
  117. unfilled text, like most of the examples in this paper:
  118. .P1
  119. ^de BS    \e" start indented block
  120. ^sp
  121. ^nf
  122. ^in +0.3i
  123. ^^
  124. ^de BE    \e" end indented block
  125. ^sp
  126. ^fi
  127. ^in \(mi0.3i
  128. ^^
  129. .P2
  130. Now we can surround text like
  131. .P1
  132. Copy to
  133. John Doe
  134. Richard Roberts
  135. Stanley Smith
  136. .P2
  137. by the commands
  138. .BD .BS
  139. and
  140. .BD .BE ,
  141. and it will come out as it did above.
  142. Notice that we indented by
  143. .BD .in\ +0.3i
  144. instead of
  145. .BD .in\ 0.3i .
  146. This way we can nest our uses of
  147. .BD .BS
  148. and
  149. .BD BE
  150. to get blocks within blocks.
  151. .PP
  152. If later on we decide that the indent
  153. should be 0.5i, then it is only necessary to
  154. change the definitions of
  155. .BD .BS
  156. and
  157. .BD .BE ,
  158. not the whole paper.
  159.