[comp.text] LaTeX: defining macros with optional arguments?

bsa@telotech.uucp (Brandon S. Allbery) (07/28/89)

Here I go, delving into something I should probably keep out of...

I want to reimplement my troff indexing scheme in LaTeX, now that I have it up
and running (and found a copy of the manual).  (This is the same one I offered
to others; I'll post the troff version soon, since I've received a few
requests for it.  I should note that some parts of it look a bit weird because
troff is broken on this system; it should work on other systems, however.)

(BTW, I'm curious as to why LaTeX doesn't have a multilevel-index mechanism
already.  It seems common enough in many technical books I've seen.)

The troff macros look like:

	.sX primary-head [secondary-head [-]]

which inserts an index entry for "primary-head, secondary-head" and, if the
secondary-head is non-null, inserts a "see" entry for "secondary-head
primary-head" unless the third argument is blank.

Under LaTeX, I see this happening as follows:

	\tag{primary}[secondary]	-- insert index entries
	\tag*{primary}[secondary]	-- insert main index entry
						but suppress "see" entry

I would like these to output the tags in "secondary primary" form into the
text body as well, unless another optional (first) argument is provided; in
that case, output the first argument instead:

	\tag[printedform]{primary}[secondary]
					-- insert index entries and print
						"printedform" in text body

	\tag*[printedform]{primary}[secondary]
					-- as above, but suppress "see" entry

My main problem is that I can't quite get a handle on what magic incantations
one uses to trap * and optional arguments in LaTeX macros.  The manual doesn't
go in-depth on hacking LaTeX, and I haven't been able to figure out what the
stuff in latex.tex is doing.  Can someone explain to me (in words of one
syllable or less ;-) how one goes about processing *-commands and optional
arguments in home-grown LaTeX macros?

(I'll probably post the result of this when I'm done, since someone out there
might be able to find a use for hierarchical indexes.)

Thanks in advance,
++Brandon
  Brandon S. Allbery @ telotech, inc.   (I do not speak for telotech.  Ever.)
*This article may only be redistributed if all articles listed in the header's*
*  References: field, and all articles listing this one in their References:  *
*		       fields, are also redistributed.			      *

chris@mimsy.UUCP (Chris Torek) (07/28/89)

In article <1989Jul28.020403.15263@telotech.uucp> bsa@telotech.uucp
(Brandon S. Allbery) writes:
>... I can't quite get a handle on what magic incantations one uses to
>trap * and optional arguments in LaTeX macros.

Stars are found with \@ifstar, and optional arguments with \@ifnextchar[;
some documentation for these can be found near line 1500 of latex.tex.
Essentially, one defines \foo as

	\def\foo{\@ifstar{\@starredfoo}{\@regularfoo}}

for starred things, and

	\def\bar{\@ifnextchar[{\@bar}{@bar[defaultarg]}

for things that have optional arguments with defaults.

>The manual doesn't go in-depth on hacking LaTeX,

latex.tex is actually more informative than {\em The \LaTeX book}.

>Can someone explain to me (in words of one syllable or less ;-) how one
>goes about processing *-commands and optional arguments in home-grown
>LaTeX macros?

Only a little of TeX can be explained this way :-) .  However, it is
worth noting here that in raw TeX, one can get funny characters into
macros in two ways: either set the \catcode of the characters to 11
(`letter'), or use something like

	\expandafter\def\csname w&e*i+r-d\endcsname{foo}

\csname ...\endcsname turns `...' into a control sequence regardless
of what is in the `...'.  The \expandafter makes TeX `pick up' the
\def, hold on to it, read the next token (\csname), expand it (gobbling
up the ...\endcsname), and then act on the \def.  \expandafter can
be very strange; see {\em The \TeX book}, in particular Appendix D.

LaTeX has some internal macros to `sanitise' these \csname tricks
(\@namedef, \@nameuse).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

chpf127@ut-emx.UUCP (John W. Eaton) (07/28/89)

In article <1989Jul28.020403.15263@telotech.uucp> bsa@telotech.uucp
(Brandon S. Allbery) writes:

> I want to reimplement my troff indexing scheme in LaTeX, now that I
> have it up and running (and found a copy of the manual).

It may be worth looking at MakeIndex first.  Source is available via
anonymous ftp from sun.soe.clarkson.edu

> (BTW, I'm curious as to why LaTeX doesn't have a multilevel-index
> mechanism already.  It seems common enough in many technical books
> I've seen.)

Used with MakeIndex I believe it does.  If this doesn't do what you
want it might still be a good place to start.

> My main problem is that I can't quite get a handle on what magic
> incantations one uses to trap * and optional arguments in LaTeX
> macros.  The manual doesn't go in-depth on hacking LaTeX, and I
> haven't been able to figure out what the stuff in latex.tex is
> doing.

As the LaTeX book is mostly for users, it seems to me that a detailed
explanation of latex.tex is sort of beyond its scope (not that I
haven't ever wanted such a description myself :-).

> Can someone explain to me (in words of one syllable or less ;-) how
> one goes about processing *-commands and optional arguments in
> home-grown LaTeX macros?

Buy the TeXbook.  Read it.  (Oops, a two syllable word snuck in there :-)
-- 
John Eaton
chpf127@emx.utexas.edu
Department of Chemical Engineering
The University of Texas at Austin
Austin, Texas  78712