MATH 640-499 Class 11:
Tuesday, March 2, 1999

Today's tips

The interactive mode in LaTeX

The ? prompt

We have seen LaTeX working in its normal mode. Sometimes, though, it is useful to put LaTeX into its interactive mode, and sometimes, LaTeX will put itself into the interactive mode when it comes across an error in the source file. By now, you have seen the prompt ? that LaTeX uses when there is an error in the source file. At the ? prompt you can press RETURN to continue, H for help (sometimes helpful, often mysterious), or X to exit.

The * prompt

If you happen to leave out the end{document} command, then you will find yourself in the interactive mode with the * prompt. You can then type end{document} to exit. However, you can use the * prompt to get information about LaTeX commands by typing \show and then the command name. This is useful if you want to define your own commands and want to check whether a command already has been defined with a given name. To get into the interactive mode, just comment out the end{document} command line by inserting % as the first character of that line. You can exit by typing end{document}.

Goals to accomplish during class

  1. Learn how to create tables in HTML
  2. Learn more about page styles in LaTeX
  3. Learn how to define commands in LaTeX

In-class Activities


Tables in HTML

You may already have noticed that arranging the display of material on a Web page by using HTML is not completely straightforward unless you want something centered. If you have several graphics to be displayed in some relation to each other, then you will probably not find any HTML tools like the ones we have used but designed specifically for this purpose. However, arranging the display can be done. One way to arrange material on a Web page is to use the TABLE tag. This allows some fairly simple spacing of graphics and text, which are put into cells. Cells are arranged as an array of rows and columns. The table is built out of rows, which are contained the matching <TR> ...</TR> pair. Each individual entry in a row is contained in the <TD> </TD> pair. Let's take an extremely simple example.
<CENTER>
<TABLE ALIGN=RIGHT>
   <tr>
      <td>Upper Left</td>
      <td>Upper Right</td>
   </tr>
   <tr>
      <td>Lower Left</td>
      <td>Lower Right</td>
   </tr>
</TABLE>
</CENTER>
This is displayed as:
Upper Left Upper Right
Lower Left Lower Right
We want a border, so we use the BORDER attribute for the TABLE tag.
<CENTER>
 <TABLE BORDER>
   <tr>
       <td>Upper Left</td>
       <td>Upper Right</td>
   </tr>
   <tr>
       <td>Lower Left</td>
       <td>Lower Right</td>
   </tr>
 </TABLE>
</CENTER>
Here is how it is rendered:

Upper Left Upper Right
Lower Left Lower Right

Here is a table taken from the Rutgers library web-site.

<CENTER><TABLE BORDER=5 WIDTH=450 CELLPADDING=10 CELLSPACING=10>
  <TR>
      <TD ALIGN=MIDDLE><A HREF="general.shtml">
        General Subjects</A><BR></TD>
      <TD ALIGN=MIDDLE><A HREF="arthum.shtml">
        Arts & Humanities</A><BR></TD>
      <TD ALIGN=MIDDLE><A HREF="science.shtml">
        Science, Technology & Medicine</A><BR></TD>
  </TR>
  <TR>
      <TD ALIGN=MIDDLE><A HREF="socsci.shtml">
        Social Sciences, Business & Law</A><BR></TD>
      <TD ALIGN=MIDDLE><A HREF="gov.shtml">
        Government Information</A><BR></TD>
      <TD ALIGN=MIDDLE><A HREF="alpha.shtml">
        Alphabetized list</A> of all indexes<BR></TD> 
  </TR>
</TABLE></CENTER>
You can view the Rutgers library Subjects Page, which contains the table.

If you view the source HTML for the Subjects Page, you'll notice the use of tables to position graphics. Also notice that the table is captioned by a centered header above it on the Subjects Page.

To make a caption part of the table, after the <TABLE> tag, add the pair of tags <CAPTION>...</CAPTION>. For instance,

<CENTER>
<TABLE BORDER>
  <CAPTION>This is a very simple table</CAPTION>
   <tr>
       <td>Upper Left</td>
       <td>Upper Right</td>
   </tr>
   <tr>
       <td>Lower Left</td>
       <td>Lower Right</td>
   </tr>
</TABLE>
</CENTER>
Here's how it is rendered:

This is a very simple table
Upper Left Upper Right
Lower Left Lower Right

Notice that the default placement of the caption is directly above the table, centered, and that if the width of the text exceeds the width of the table, then the text flows onto additional lines. You can also choose to put the caption at the bottom of the table by using the attribute ALIGN with the value BOTTOM inside the caption start tag.

Of course, you need to put labels on the columns and rows of a table. To label the columns, you put in a header row using the tag pair <th>...</th> for each entry. For the table above, we would need a new row at the top. It should have 3 entries, (perhaps with the first entry left blank) because the rows are also going to need labels, which requires a new entry in each row.


EXERCISE:In your test page, put an HTML version of the table

that we used in the third class as a LaTeX exercise. Try to get the alignment of material in the cells to match the picture. You may want to adjust the minimum space used between the cell boundary and the text inside. This is done with the CELLPADDING attribute, whose argument is always given in pixels.



Continue with LaTeX

Pagestyles in LaTeX

We have seen how to make minor changes to the page style of your LaTeX document. Namely, the commands \pagestyle and \thispagestyle can be used to change the header and footer, and the \pagenumbering{style} command can be used to select a display style for the page numbers. However, there are further changes that you can make to your page style.

For example, you can use the command \setlength to adjust various page parameters. For example, to make a very narrow page, you might use the command

\setlength{\textwidth}{3in}
This goes in the preamble. That is, it goes somewhere between the \documentclass command and the begin{document} command. Note that the first argument is actually a command name. You'll get an error message if you try
\setlength{textwidth}{3in}

Exercise: Set the first paragraph of Alice in Wonderland as a 3 inch wide column in LaTeX.

Some useful length commands are \parindent (the amount of horizontal white space used to indent a paragraph), \hangindent (the amount of horizontal white space used to indent all but the first line of a paragraph -- use with noindent at the beginning of each paragraph), and \parskip (the amount of extra vertical white space inserted between paragraphs.

The units can be specified as in (inches), pt (points: 72.27pt = 1in), pc (pica: 1pc = 12pt), mm (millimeters), which are absolute measurements, and also as the relative units em and ex, which are roughly the widths of the letters M and x, respectively, in the current font.


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. 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}}
This line instructs LaTeX to create a new command with the name "\be" to act as the command (with argument) \begin{equation} Then, to put LaTeX into the equation environment, you can just type \be.

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. Then you can type
\show \commandname. Try this with the command \vec. You should see that \vec is a math accent. That is, it is used in one of the mathematics modes to put an accent on a character. In this case, the accent is a small arrow placed above the character. You could redefine the command \vec without much danger. However, some commands are "primitive" TeX commands. They should not be redefined. An example is \hangafter. To find out what this command does, try using the * prompt to show it! You can also type the \show \commandname command into your document itself (for example, \show \hangafter) and get the information in the log file and on the monitor. <

Arguments for custom commands in LaTeX
The real power of commands comes when you use arguments with them, so we want to define commands that have arguments. To do this we must instruct LaTeX how many arguments the command will have. We begin with a simple but useful example used by Grätzer when he wrote the book Math into LaTeX. Whenever he wanted to refer to an environment name (for example, the center environment, he wanted to have the name displayed in some special typeface, which would be consistent throughout the book. He defined a new command for the purpose.
\newcommand{\env}[1]{\texttt{#1}}
Here is its meaning. Define a new command "\env" with a single (required) argument (which will be the name of the environment, of course). The command will take the argument, which is called simply "#1", and display it on the page in the text environment font called typewriter text, "tt" for short. For example, whenever he typed the environment name "equation" he would do so as
\env{equation}

A command can have up to nine arguments.

Here is an example based Grätzer's book Math into LaTeX. We will build from a simple command with no arguments. Call it "\Sum", and define it so that $\Sum$ will print an expression for the sum of terms c-subscript-i with i ranging from 1 to n.

\newcommand{\Sum}{c_{1}+c_{2}+\cdots+c_{n}}
Now, this is not so useful because if we wanted to use a variable other than "n" for the upper limit of the sum, we would need another new command and similarly if we wanted different summands, say T_1, T_2, ... rather than c_1, c_2, ... . We can create a command with two required arguments, the first one specifying the upper limit.
 
\newcommand{\NewSum}[2]{#2_{1}+#2_{2}+\cdots+#2_{#1}}

EXERCISE: To understand what this command does, create a LaTeX file, put the above \newcommand line into the preamble (that is, before the \begin{document} line, and try out the following commands in the document body.

\[
A=\NewSum{n}{a} 
\]

\[
B=\NewSum{p}{T} 
\] 
You can also define a command with a single optional argument and one or more required arguments. WARNING: The optional argument must come first. Here is an example taken from Math into LaTeX.
\newcommand{\NNSum}[2][n]{#2_{1}+#2_{2}+\cdots+#2_{#1}}
The arguments to \newcommand have the following meaning in the example:
  1. [2] is an optional argument that specifies how many arguments the new command will have; in this case, 2 arguments.
  2. [n] is another optional argument, which specifies the default value of the optional argument; in this case, it is n.
  3. #2 refers to the second argument of the new command \NNSum.
  4. #1 refers to the (optional) first argument

EXERCISE: To understand what this command does, create a LaTeX file, with the \newcommand line in the preamble (that is, before the \begin{document} line, to define \NNSumand try out the following lines in the document body:

$\NNSum{x}$

$\NNSum{t}$

$\NNSum[i]{a}$


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. It's time to look seriously towards 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. (For Thursday, March 4) Finish writing your short paper in LaTeX designed to teach a first year student the how and why of completing the square.
  4. As an exercise for LaTeX custom command definitions, we will be 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. For Homework to do before our next class (Thursday, March 4) define a new command (or two) to create the numerator of the expression a-sub-n in the monster equation (bottom line). Later, your new command(s) can be used to define further new commands, so you can build up the monster equation bit by bit. Ultimately, you may be able to amaze your friends by demonstrating how short (in terms of bytes) your source code can be!
    To line up the equal signs you should use the align environment, which is found in the package amsmath, (so, in the preamble include the command \usepackage{amsmath}). To get the fractions correctly sized in the numerator, you can use the command \displayfrac instead of \frac.


/ Home / Contents /


email to:Martin Karel