MATH 640-499 Class 12:
Thursday, March 4, 1998

Today's tip:

The UNIX tcsh shell command line editor

Did you know that you want to edit a command line in a UNIX shell, you can do it with emacs-style key combinations provided you are using tcsh, the tc-shell. That's the standard shell at Rutgers-Camden. To see how this works, get a shell prompt and type:
echo this is a command
The try the following
  1. Type CTRL b (back one character)
  2. Type ESC b (back one word)
  3. Type CTRL a (beginning of line)
  4. Type CTRL e (end of line)
You can delete various pieces of a line.
  1. Type ESC DEL (delete word backward)
  2. Type CTRL a (beginning of line)
  3. Type ESC d (delete word)
  4. Type ESC f (forward one word)
  5. Type CTRL k (delete to end of line)
  6. Type CTRL u (delete the whole line)

Goals to accomplish during class

  1. Practice searching for math resources on the Web
  2. Add to your knowledge of table construction in HTML
  3. Learn how to define your own commands in LaTeX

In-class Activities


Search for Mathematical Information

Let's see who can find the answers to the following search questions first.
  1. What is the largest known prime number?
  2. Who was Emmy Noether?

More about HTML

Specifying image dimensions

As I mentioned in our tenth class, the folks at W3C recommend that every in-line image in a Web page have its dimensions (that is its height and width) specified. The Netscape browser that we are using resizes the image if its dimensions do not match the dimensions that you specified; however, it is a good idea to keep in mind that not all browsers treat images that way.

More about Tables

In addition to the basic tags and attributes that we covered in Class 11, there are now some newer tags and attributes that you can use to make your layout look better. For example, you can make the background color of your table different from the background color for the rest of your document by using the attributes bgcolor in the <table> tag itself. In HTML 4.0 there is a new frame attribute, use in the <table> tag to modify the part of the border that surrounds the table (as opposed to the lines within the table). The attribute value above draws a line on top of the table (use <table frame=above>. Guess what happens if you use the attribute values below, lhs, rhs, hsides, vsides (lhs for left hand side, hsides for horizontal sides, which are the ones a the top and bottom of the table.

The most popular browsers create 3D effect by using three color variations of the document's background color to draw a table border. These colors can be controlled, but the effectiveness of the 3D effect is dependent on the choice of colors and widths of the border components, so probably best not worry about colors. It is more common to adjust the width of the compound border and its components:

For example, according to Musciano and Kennedy (Chap. 11, page 376)

Usually you want a table to include some headers. There is a corresponding tag <th>, and this tag can take the attribute colspan to make a single header span several columns of the table. For example, we might want a table to tally results of a questionnaire for students in grades 10, 11 and 12, but we might want to keep the responses from male students and female students separate. Here is how to set up such a table.

<table border cellspacing=0 cellpadding=3>
<tr>
  <td colspan=1 rowspan=2></td>
  <th colspan=3 align=center>Females</th>
  <th colspan=3 align=center>Males</th>
</tr>
<tr align=center>
  <th>Grade 10 </th>
  <th>Grade 11 </th>
  <th>Grade 12 </th>
  <th>Grade 10 </th>
  <th>Grade 11 </th>
  <th>Grade 12 </th>
</tr>
<tr>
  <td align=center >Question 1</td>
  <td>25%</td>
  <td>45%</td>
  <td>10%</td>
  <td>43%</td>
  <td>21%</td>
  <td>18%</td>
</tr>

The table looks like this.
Females Males
Grade 10 Grade 11 Grade 12 Grade 10 Grade 11 Grade 12
Question #1 25% 45% 10% 43% 21% 18%

Continue with LaTeX

Custom commands in LaTeX

First, a WARNING: a command in LaTeX can either have a one-character name, like \$, or all characters in the name must be alphabetic characters rather than numbers or special symbols.

Let's start by reviewing the main ideas about custom commands in LaTeX. You can make the job of typing a LaTeX document easier if you add some of your own commands to the large number of pre-defined commands. These custom commands are sometimes called "macros", short for "macro commands". For example, you can introduce commands to abbreviate longer commands. To abbreviate the command \begin{equation} to just \be you type

    \newcommand{\be}{\begin{equation}}
Then, to put LaTeX into the equation environment, you can just type \be. Notice that the \newcommand takes two required arguments: the name of the new command and the definition of the new command.

Suppose that you have to type Greek letters often. Then it may pay to create commands for the ones that you need. Thus,

    \newcommand{\ga}{\alpha}
makes it possible to type \ga instead of \alpha.

While the savings is small for that \ga macro, your savings could be greater if you have a more complicated symbol to typeset, say an uppercase gamma with one superscript and two subscripts. If you have defined a command \gijk as follows:

    \newcommand{\gijk}{\Gamma_{jk}^{i}},
then you can type \gijk whenever you want to typeset \Gamma_{jk}^{i}.

Redefining existing commands

If a command already exists with the name you have chosen, LaTeX will give you an error message. You can insist on your own version by substituting \renewcommand for \newcommand. I do not recommend this, since you may want to use a package that makes some use of the original version of the command, and then you will most likely get some puzzling error messages.

At least, if you want to redefine a command, you should check what the current definition is. To do this you go into interactive mode. Namely, comment out the \end{document} line and run LaTeX on the file to get the * prompt.

First, you'll get a ? prompt. If you enter a return, then you should see the * prompt. When you are at the * prompt, then you can type
\show \commandname. You should try, for example, \show \vec. You should see that \vec is a math accent. You could redefine it without much danger. However, some commands are "primitive" TeX commands. They should not be redefined. An example is \hangafter. Try showing it!

Although I have seen it said that you can also type the \show \commandname command into your document itself, and get the information in the log file and on the monitor, it did not work for me when I tried it.

New commands with arguments

The real power of commands comes when you use arguments with them. So, we want to get a new command that has arguments For this you will need to have at least one more argument for the command \newcommand. The number of arguments to be taken by the command that is being defined is specified in an optional argument to the command \newcommand. You place this new optional argument right after the first argument, which gives the name of the command that you are defining. It looks like this:
   \newcommand{\name}[nargs]{command definition}
The arguments are referred to in the command definition as #1, #2, ..., #nargs. There can be no more than 9 arguments.

Let's start with a simple example. Some people like to format the names of authors in small caps when they write an article. This can be done by using the small caps text-style command \textsc You could put a name, say Frobenius, in small caps by putting in your source code \textsc{Frobenius}, but it is better to have your source code indicate not only the formatting but also the reason for the formatting. You do this by defining a new command, say, \names as follows.

    \newcommand{\names}[1]{\textsc{#1}}
The new command \names takes a single argument, a string of text, which it converts to small caps. Thus, you could have a passage in an article that looked like this:
We have made use here of an idea that goes back to \names{Frobenius}, who used it in his celebrated paper ...
Then if you later decide to change the formatting of names in your document, you can simply redefine the command \names.

Here is another simple example. Suppose that in the symbol \Gamma_{jk}^{i}, the subscripts and the superscript change. You could set up a command that would allow you to replace the i, j, and k variables whenever you wished to. For example,

   \newcommand{\vG}[3]{\Gamma_{#2#3}^{#1}}
Then you could type $\vG{a}{b}{c}$ to get an upper case Gamma with superscript a and subscripts b, c. There is no reason that the arguments have to be single characters. You could make each argument a whole word or even a phrase.

Suppose that you will almost always use your new command with the first argument taking a particular value, say x. You can make x the default value of the first argument to your command and make first argument to your command optional. That way, you can invoke the command without specifying the first argument most of the time. When you want to override the default, you can do it by putting in the optional argument. To do this, you specify the default value by an optional argument (hence in square brackets, []) to \newcommand immediately after the nargs argument. Thus, it will be the third argument to \newcommand. It looks like:

\newcommand{\name}[nargs][default#1]{command definition} 
Here is an example taken from Grätzer's book Math into LaTeX.
    \newcommand{\Sum}[2][n]{#2_{1}+#2_{2}+\cdots+#2_{#1}}
The new command that is defined in this example, \Sum has two arguments. The second argument is required. The first argument is optional and has a default value n. In the example the arguments to \newcommand have the following meanings:
  1. [2] specifies that the new command, \Sum, will have 2 arguments; it is an optional argument for \newcommand.
  2. [n], another optional argument, specifies the default value for argument #1 of \Sum: namely n here.
  3. #2 refers to the second argument of the new command \Sum. you can use emacs editing for your command line in a UNIX shell provided you are using tcsh.
  4. #1 refers to the (optional) first argument of \Sum.
Warning: The new command can take at most one optional argument, and it must be the first argument (#1, reading from left to right). When you later use the command, if you do use the optional argument, then you must enclose it in square brackets, but other arguments must be enclosed in curly brackets, {}.

EXERCISE: To understand what the commands in the examples do, create a LaTeX file, with \newcommand lines to define the new commands \names, \vG and \Sum as above. This is usually done in the preamble (that is, before the \begin{document} line), but it can be done anywhere in the document before the new command is used. Try out the following lines in the document body, leaving a blank line between them so that the typeset formulas will be displayed separately:
  1. \names{Frobenius} \names{Gr\"{a}tzer}
  2. $\vG{a}{b}{c}$
  3. $\Sum{x}$
  4. $\Sum{t}$
  5. $\Sum[i]{a}$

The ensuremath command

If you want to use a command in both math mode and text mode, then the way to do it is to use
    \ensuremath{math code}
This will ensure that its argument is always put in math mode. Namely, it will put $ signs around the argument if there are none. In the book The LaTeX Companion Goosens-Mittelbach-Samarin give the following example:
   \newcommand{\avec}[1]{%  arg1: the name of a vector
               \ensuremath{#1_1,\ldots,#1_n}}
   The series \avec{x} or $\avec{y}$  \ldots


EXERCISE: Convert the commands \vG and \Sum so that they will work in a text environment.

Finished?

If you have done all the above activities, and there is still time left, here are some other activities you can do.

Homework for after class

You should have time to start on these during class.

  1. Continue to explore a mathematical topic, with a view to using what you find as part of a project
  2. Revise your list of links to mathematical sites that you think are worth revisiting. Include the list of links either on your very basic Web page or as a separate document linked to that Web page.
  3. As an exercise for LaTeX custom command definitions, try typesetting the monster equation below from Michael Spivak's book, The Joy of TeX. You should be able to save a lot of effort by using your own commands to typeset parts of the equation.
    To use the align environment, you require the amsmath package, which can be loaded with the line \usepackage{amsmath} in the preamble (before the line \begin{document}. To get the fractions correctly sized in the numerator, you can use the command \dfrac instead of \frac. Amaze your friends by showing them how short (in terms of bytes) your source code can be!



/ Home / Contents /


email to:Martin Karel