home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / texpp.zoo / texpp.1 / README < prev    next >
Text File  |  1990-04-04  |  14KB  |  330 lines

  1.  ***************************************************************************
  2.  *                                       *
  3.  *           texpp TeX preprocessor, Version 1.2.               *
  4.  *              Laci Csirmaz, DIMACS - Rutgers               *
  5.  *                   Feb. 25, 1990                   *
  6.  *                                       *
  7.  ***************************************************************************
  8.  *    You are granted to use, modify, copy, redistribute this program any  *
  9.  * way you want. However no warranties are made for this program or the    *
  10.  * accopanying documentation.                           *
  11.  *                                       *
  12.  *    Please send your comments, suggestions, etc. to:               *
  13.  *        csirmaz@cs.rutgers.edu                       *
  14.  *                                       *
  15.  ***************************************************************************
  16.  
  17.  
  18.                        Tex Preprocessor  M A N U A L
  19.  
  20.     0. Introduction
  21.  
  22.     TeX, undoubtedly a very LOGICAL language, does not follow closely enough
  23. the illogical nature of human beings. Using backslashes everywhere to signify
  24. a special treatment of a word is logical, but, of course, tedious and a bit
  25. boring. Whenever the word "alpha" appears in a text, with higher probability
  26. than 0.9999 the author wishes the corresponding greek letter to appear, and
  27. not this word. In the latter unlikely case, s/he is more willing to do some
  28. extra word to make the (now) exception the rule, and save a lot of work.
  29.     Another drawback is the (also very logical, but unnatural) handling of
  30. macros. If an operator is applied to a word (or a single letter), TeX
  31. requires the operator to precede its argument. While in some cases this
  32. is the natural approach, in some cases it is not. As in using a dot, tilde, 
  33. or hat on a letter, the natural form is "x tilde", and not what is the
  34. obligatory form, "\tilde x".
  35.     TeX forbids almost all key combinations to use as macro names, only
  36. sequences of letters are allowed. Why cannot I write "|==" instead of
  37. "\model", "/0" for "\empty", or, horribile dictu, "/\" instead of "\wedge"?
  38. Formulas written in this way are more readable, and easier to type.
  39.     Finally, while TeX (and LaTeX) allows to redefine its macros, the user
  40. is usually not aware of its danger. Both plain TeX and LaTeX have a lot of
  41. built-in macros which are absolutely necessary to run them. Redefining any
  42. of them the program simply goes crazy. And, in most of the cases, the user
  43. does not even know why the construct is wrong. What happens, for example, 
  44. if "\and" is redefined in LaTeX so that it would produce the sign /\ -- a 
  45. quite reasonable assumption. To avoid all the problems arising this way, TeX 
  46. should do these definitions locally, which it doesn't.
  47.  
  48.     All of the above problems lead to the development of the "texpp" TeX
  49. preprocessor. Since TeX is quite ambivalent about handling the input layout
  50. (often it says to be independent of whitespaces, but two consecutive newlines
  51. signify a new paragraph, and comments are closed by newlines), I decided to
  52. use layout a bit more significantly than TeX does. All macro definitions
  53. should start at the first column. The preprocessor is not intended to be as
  54. general as possible. It has a lot of wired in features (for example, the $ 
  55. and $$ to start and end math mode), but the C code is available, and (at 
  56. least I hope) can be modified easily. Also, the preprocessor is absolutely 
  57. NOT for TeX wizards. It can be fooled quite easily e.g. by redefining the
  58. escape character, but as far as it is used as a simple tool it works fine.
  59.  
  60.  
  61.     1. Shipped files
  62.  
  63.     Besides the C source code of the program and this manual, you can find
  64. a sample macro file which show some extra features of texpp. Also I wrote
  65. a tex-mode.el (gnu)emacs template which embeds the 'texpp' preprocessor, and
  66. a dvi viewer.
  67.     The program was written for UNIX(TM) machines, but the code is highly
  68. portable (at least I hope so). Therefore it should be easy yo transport to
  69. other machines.
  70.               
  71.  
  72.     2. Macro names
  73.  
  74.     Macro name can be any sequence of characters which does not contain
  75. braces `{' and `}', dollar sign `$', percentage sign `%', and embedded
  76. whitespaces. `Character' here means a character with ASCII code >=32 and
  77. <= 126. Each line of the text is cut at braces and whitespaces, and what
  78. is left is compared against the list of macro names. If there is a hit, then
  79. the closing braces or white spaces are erased, and the macro text is replaced.
  80.  
  81.     3. Parameters
  82.  
  83.     A macro may have up to nine parameters, which can be either before or
  84. after the macro name. Parameters are parsed starting from the macro name:
  85. each parameter is either enclosed into braces, or contains no braces and is
  86. enclosed by whitespaces. A parameter must have balanced braces, if any.
  87. In braces, it may have embedded white spaces. Parameters may contain further
  88. macro names, too, but parameter parsing is done BEFORE macro substitution.
  89. Two macros cannot share parameters. A parameter is always stripped off its
  90. enclosing characters, and then a macro substitution is executed on it. A
  91. parameter can be a macro name with further parameters (see the examples).
  92. If the macro name is enclosed into braces, all its parameters must also be
  93. inside.
  94.  
  95.  
  96.     4. Macro definition
  97.  
  98.     Macros can be defined anywhere. A definition always starts at the first 
  99. character of the line, and signified by the key words `%define' or `%mdefine'. 
  100. (Starting with percentage sign, but NO BACKSLASH is here). The form of a
  101. definition is the following:
  102.  
  103.     %define <preceeding pars> name <follow up pars>  %text% <any comment>
  104.  
  105. Local parameters are of the form #1, #2, ..., #9. No parameters are required,
  106. they can appear in any order and in any place. The REPLACEMENT text is enclosed
  107. by `%' characters (thus it cannot contain `%', but of course it may contain
  108. the `\%' pair!), and is used as it is, with the following exeptions:
  109.  
  110.     a) local parameters #1, ... #9 are replaced by the stripped and 
  111.        expanded parameters of the invocation;
  112.     b) the pair ## is replaced by a single # mark.
  113.     c) macro calls are expanded.
  114.  
  115. Rule b) also applies to the macro name. Thus `#' can also appear in a macro
  116. name, but should be written as `##'. (Of course, when looking for a macro name
  117. in the text, this corresponds to the single character!)
  118.     Macro definitions starting by `%mdefine' are applied only when in math
  119. or displayed math mode; other definitions are always applicable. The program
  120. assumes that the macro body does not switch these modes.
  121.     Besides defining macros, you can "undefine" them, too. This comes handy
  122. when a macro is used only as an auxiliary in a more complicated definition, 
  123. but not needed later. A new definition "hides" the old one, undefining
  124. restores the old meaning. This can be done by
  125.  
  126.     %undefine name
  127. or
  128.     %undefine name % <your comment here>
  129.  
  130. It is an error to undefine a macro which was not defined (or mdefined)
  131. before.
  132.  
  133.     5. Auxiliary tools
  134.  
  135.     In certain cases it is essential to suppress macro substitution. This
  136. can be done by applying the `\preserve' keyword to its single parameter:
  137.  
  138.     \preserve <anything_which_does_not_contain_whitespace_or_brace>
  139. or
  140.     \preserve {<any text with balanced braces>}
  141.  
  142. In the parameter to `\preserve' no macro substitution is made. However, 
  143. `\preserve' cannot be applied to macro parameters in macro definitions, 
  144. since parameter expansion is done BEFORE substitution.
  145.  
  146.     Different extensions of TeX use different macros to switch to and from 
  147. math mode. To comply with them, the following definitions are recognized:
  148.  
  149.     %mathmode <string_to_enter_math_mode>  <string_leave_math_mode>
  150. and
  151.     %dispmode <string_to_enter_displayed_math>  <string_leaving>
  152.  
  153. The strings cannot contain white spaces, and if only one string is given in
  154. a definition, then that is used for both entering and leaving math mode. 
  155. In LaTeX the following definitions may come handy:
  156.  
  157.     %mathmode \( \)            %simple math mode
  158.     %mathmode \math \endmath
  159.     %dispmode \[ \]            %displayed formulas
  160.     %dispmode \equation \endequation    %for numbered formulas
  161.  
  162.  
  163.     6. Examples
  164.  
  165. o 6.1. The first example defines the `*16' macro, which repeats its argument
  166.  sixteen times:
  167.  
  168.  %define #1 *2 %#1#1%        %define `*2' as an auxiliary macro
  169.  %define #1 *16 %#1 *2 *2 *2 *2%
  170.  %undefine *2            %undefine `*2', no more needed
  171.  
  172.  Texts `x 1 *16 y' and `x{1}*16 y' expand to `x1111111111111111y'. (Observe
  173.  that the trailing space after `*16' vanished). At the same time `x 1*16 y'
  174.  and `x 1 *16y' remain unchanged since the macro name `*16' cannot be
  175.  extracted.  As another example, `x {a b} *16 y' saves the space between `a'
  176.  and `b', and before the open brace but not after the macro name. So the
  177.  result is
  178.  
  179.     `x a ba ba ba ba ba ba ba ba ba ba ba ba ba ba ba by'
  180.  
  181.  
  182. o 6.2. The next example also shows that macro definitions are expanded when
  183.  defined, and not when invoked. This means, for example, that subsequent
  184.  macro definitions have no influence on an existing macro definition. This
  185.  fact is used here:
  186.  
  187.  %define [[  %[%    define [[ as a macro to be substituted by [
  188.  %define ]]  %]%    define ]] as a macro to be substituted by ]
  189.  %define [   %\{%    now define [ to mean \{
  190.  %define ]   %\}%    and define ] to mean \}
  191.  
  192.  The last two definitions make a text like
  193.  
  194.     a = [ 0, 1, 2, ..., n ] + [ 2n, ..., 4n ]
  195.  
  196.  to transform to
  197.  
  198.     a =\{0, 1, 2, ..., n\}+\{2n, ..., 4n\}
  199.  
  200.  while, e.g. a reference can be put into square brackets by using double
  201.  brackets: ` [[ 1 ]] ' which becomes `[1]'. Changing the last two and first 
  202.  two definitions here, the definition of `[[' becomes identical to that of 
  203.  `[', i.e. with `\{'.
  204.  
  205.  
  206. o 6.3. Our third example defines some model-theoretical stuff:
  207.  
  208.  %mdefine /\   % \wedge %           and sign
  209.  %mdefine \/   % \vee %             or sign
  210.  %mdefine |--  % \vdash %           syntactically follows
  211.  %mdefine |==  % \models %          semantically follows
  212.  %mdefine -->  % \rightarrow %       implies
  213.  %mdefine phi  % \varphi %
  214.  %mdefine ALL  % \forall %
  215.  %mdefine not  % \neg %             
  216.  %mdefine ...  % \ldots %           ellipsis notation
  217.  
  218.  Since the preprocessor does not treat the backslash as a special symbol,
  219.  `/\' and `\/' will be recognized without any trouble. The spaces at the 
  220.  end of each definition are necessary because before replacing the macro 
  221.  name, its surrounding white spaces (or braces) are deleted. So displayed
  222.  formulas can be written as, e.g.
  223.  
  224.     $$ |== ALL x ( phi (x) \/ not phi (x) ) $$
  225.     $$ |-- phi --> ( not phi --> phi ) $$
  226.  
  227.  
  228. o 6.4. Sometimes it is convenient to use `**' to denote exponentiation:
  229.  
  230.  %mdefine #1 ** #2 %#1^{#2}%
  231.  
  232.  Here ` e ** { x ** 2 / 2 } ' or ` e **{x ** 2 /2}' becomes `e^{x^{2}/ 2 } '
  233.  (look out for the spaces!). The braces around #2 in the definition are
  234.  necessary to ensure that all the second parameter shows up in the exponent.
  235.  Without those braces the expanded form of the first form would be
  236.  `e^x^2/ 2 ', evidently not what was intended.
  237.  
  238.  
  239. o 6.5. The next example shows how to use "before" parameters when they are
  240.  handy:
  241.  
  242.  %mdefine #1 tilde %\wildetilde{#1}%
  243.  %mdefine #1 hat %\wildehat{#1}%
  244.  
  245.  After these definition the following text
  246.     ...
  247.     a+b tilde = a tilde + b tilde
  248.     ...
  249.  expands to
  250.     ...
  251.     \wildetilde{a+b}=\wildetilde{a}+\wildetilde{b}
  252.     ...
  253.  Which one is easier to read?
  254.  
  255.  
  256. o 6.6. To overcome the problem corresponding to greek letters, one can define
  257.  
  258.  %define alpha %$\alpha$%
  259.  
  260.  (since greek letters are allowed in math mode only.) With this definition, 
  261.  however, there are certain problems. Using this macro in the text `Let alpha 
  262.  be the ...', the substitution mechanism erases ALL the spaces around the word 
  263.  `alpha', and the result is `Let$\alpha$be the ...'. Evidently no what was 
  264.  wanted. Changing the definition to
  265.  
  266.  %define alpha % $\alpha$ % now with spaces
  267.  
  268.  will put back the spaces both BEFORE and AFTER the word, thus giving 
  269.     Let $\alpha$ be the ...
  270.        ^^^^^^^^^^-- substituted text
  271.  Having this latter definition, however, you cannot write alpha's without 
  272.  intervening spaces. Another problem is that this definition cannot be used in 
  273.  math mode! A possible solution is that the macro `alpha' will be used in math 
  274.  mode only, now with the definition
  275.  
  276.  %mdefine alpha %\alpha %
  277.  
  278.  The above sentence can be written as `Let $alpha$ be the ...'. Also, two
  279.  consecutive alpha's can be produced as
  280.  
  281.     `here are two $alpha$'s: $alpha alpha$'.
  282.  
  283.  The word alpha, if found not in math mode, won't change. And, if you want
  284.  to use the word alpha in math (or displayed) mode, you can use the \preserve
  285.  keyword:
  286.     I can write $\preserve alpha$ in math mode.
  287.  
  288.  
  289. o 6.7. Strings used to switch to and from math mode are also subject to macro
  290.  replacement. Thus using the definitions
  291.  
  292.  \def\mydisplayformula#1#2{ $$ #2 \leqno #1 $$}
  293.  %dispmode .EQ .EN
  294.  %define .EQ #1 % \mydisplayformula{#1}{ %
  295.  %define .EN % } %
  296.  
  297.  an eqn-like construct for display formulas can be used so that the formula
  298.  number (written after .EQ) appears on the left of the displayed formula.
  299.  However, if you don't want this number to appear, you still have to submit
  300.  a parameter, which can be the empty string, i.e. `{}'
  301.  
  302.  
  303.    7. How to use the preprocessor?
  304.  
  305. To compile the program on a UNIX machine, simply invoke cc by typing
  306.  
  307.         cc texpp.c -o texpp
  308.  
  309. On certain systems the 'strdup()' function is missing from the standard C
  310. library. In this case, recompile the program by
  311.  
  312.         cc -DSTRDUP texpp.c -o texpp
  313.  
  314. The preprocessor works as a filter. It reads one ore more input files,
  315. processes them, and then produces a single output which can be the input
  316. for TeX. The program can be invoked by typing
  317.  
  318.     texpp file1 file2 ... filen -[wa] output_file 
  319.  
  320. `file1', ..., `filen' are processed in their given order; a file definition
  321. consisting of a single `-' sign means read from stdin. The output goes to 
  322. `output_file', the file is overwritten if the name is preceded by `-w', and
  323. is appended if `-a' is present. Error messages are inserted into the output
  324. and are duplicated at stderr. If no input file is given, the program reads
  325. from stdin; if no output file is given, then the output goes to stdout. When
  326. the program halts, the exit value is 0 if `texpp' run successfully, if any
  327. error occurred (cannot open a file or processing error) then the exit value
  328. is 1.
  329.  
  330.