## Archive for the 'latex' Category

### DO’s and DON’Ts when typesetting a document

The following rules apply when using LaTeX2e…

• In display math mode, use $...$ instead of $$...$$.

$$...$$ is simply obsolete.

• Use \textbf, \textit instead of \bf, \it.

\bf, \it are obsolete font selection commands. Under New Font Selection Scheme (NFSS), they should be replaced with \textbf, \textit. One immediate difference is that {\it\bf blabla} will not generate the composite effect of italic shape and bold series, while \textit{\textbf{blabla}} indeed produces bold italic fonts.

• Put a tilde before references or citations, e.g., Jie~\cite{habit06}.

This prevents LaTeX from putting a line break between the word and the citation. Similar cases are: length~$l$, function~$f(x)$, etc.

• Be cautious when changing the page margin and page layout.

Studies show that articles with approximately 66 characters per line are the most readable. Reading would become difficult if putting more and more texts into each line. That’s why you see articles are typeset in multiple columns in a newspaper.

• Differentiate between text comma and math comma, e.g., type “for $x=a$, $b$, or~$c$” instead of “for $x=a,b$, or $c$”.

A line will not break at math comma. That is why sometimes you see an ugly math expression exceeding the right margin of your texts. Also there will not be a white space after the math comma. Hence, in $x=a,b$, the “b” character is so close to the comma, which is unpleasant.

• Use \emph more often than \textit when you mean to emphasize a term or a phase.

You can easily change the layout of the emphasized content (such as to bold fonts instead of italic fonts) by redefining the \emph command. However, if you use \textit, you will meet a lot of hassles when you want to change the layout.

• Put a backslash after a dot if the dot does not mean full stop.

Example: “Please see p.\ 381 for an illustration.” The backslash after “p.” reminds LaTeX that the dot does not mean the end of a sentence, so LaTeX will put a correct white space between the dot and the number 381. Usually the width of the white space is shorter than that between a full stop and the beginning character of the next sentence. More examples are “Mr.\ Xing”, “e.g.\”, and “i.e.\”.

(Corrected: Well, none of the above examples are correct… They should be: “p.~381”, “Mr.~Xing”, “e.g.,”, “i.e.,”. But I am sure that the principle itself is ok. Anyone has a good example?)

• Note the difference between hyphen, en dash, em dash, and a minus sign.

Hyphen (-) connects the two parts of a compound word, such as in “anti-virus”. En dash (--) connects two numbers that define a range, such as in “pages 1--10”. Em dash (---) is a punctuation dash. And remember that when you write a negative number, embrace it by the dollar signs, e.g., $-40$.

• Write ellipsis using \ldots instead of three dots.

The \ldots commands correctly typeset the spaces between two consecutive dots.

### Using PSTricks to draw the Olympics Rings

The other day I ran into a person who’s asking people to draw the five Olympics rings by writing latex source codes only. I said, well, I could do that, using pstricks, which was my favorite drawing tool in latex.

After inspecting the Olympics logo for a while, I had a rough idea how to draw it. The key was to draw the five rings in the order: blue, yellow, black, green, and red. For each ring, draw a circle at that color with a certain line width, and then surround the circle with two white slim circles. This gave the salient pattern of the five rings and at the intersection of two rings some whites. The rest of the job was to make the interleaving effect of the five rings instead of seeing the yellow ring placed above the blue ring and so on. The trick was, after drawing the blue and the yellow rings, draw a blue arc at the place where the blue should be on top of the yellow at one of the intersection of the two rings. For the rest of the rings, do the same trick. Drawing the arcs was almost the same as drawing the rings (a colored circle squeezed by two slim white circles), except that an arc was part of a circle from some degree to some degree.

Okay, enough explanation. Here are the pstricks codes.


This governs how a name is formatted. ff stands for first name, vv for von part, ll for last name, and jj for suffix, such as Sr. and Jr. This format says that the first name is followed immediately (separated by only a space but not a line-break) by the von part, which in turn is immediately followed by last name. After the last name comes a comma and a space, then the suffix. As another example, see the acm style:

"{vv~}{ll}{, jj}{, f.}" format.name\$

This format indicates that the von part is presented in the front, immediately followed by last name, then a comma, a space and the suffix. First name goes in the very last, followed by a period. Note that ff means to display the first name fully, while f means that only the initial letter of the first name is displayed.

You will notice that in the acm style, all the letters of a name, except the initial letter, are typeset in smallcaps. This is because in the FUNCTION format.authors, there is a line of code

author format.names scapify

where scapify is another function that small-capifies the lower case letters.

Another example. Read the FUNCTION article. From the codes, you probably get a grasp of how different fields of a reference are listed if the reference is of article type. First are the authors, then comes the article title, which is followed by journal information. The format.journal.vol.num.date is another FUNCTION doing more typesetting. But it only cares about volume, number and date. The pages information is only taken care in the FUNCTION format.pages.

Well, I have to stop here. If I explained all the FUNCTIONs, I probably needed to write a hundred-page book. As a hacker, I am sure that you would like to read the codes in existing style files yourself, and you will know a lot more about how to format in the end.

### Further Reading

Patrick W. Daly. A Master Bibliographic Style File, for numerical, author–year, multilingual applications.
Nicolas Markey. Tame the BeaST, The B to X of BibTEX.
Oren Patashnik. Designing BibTeX Styles.
Michael Shell and David Hoadley. BibTEX Tips and FAQ.

### LaTeX Tabular More

The width and height of a cell in a tabular is controlled by many parameters. Read the following codes:


You specify p{2cm} as part of your \begin{tabular}{} arguments, in the hope that the first column in the table is 2cm wide. Unfortunately, it appears wider than you think. The width of a cell in the table, without regard to the line width of separators, is actually computed by

\tabcolsep + p{length} + \tabcolsep.

The length you specify, gives place to contain characters in the cell. Between the left separator and the left side of the bounding box of the first character, there is some room which is controlled by \tabcolsep, such that the cell will not look too crowded. It is the same on the right side of the cell. In other words, \tabcolsep governs such that the contents in the cell will not be positioned right next to the boarders, which looks rather ugly.

By default, \tabcolsep is set to 6pt, which equals to 2.12mm in digital printing. In the above codes, we re-set it to 1cm. So the total width of the first column in the table is 4cm, while the second column is 5cm wide.

The mechanism of the height of a cell is a little bit different. In default setting, the distance between the upper boarder and lower boarder of a cell is \baselineskip, which is the line spacing in paragraphs. If you look at two adjacent lines of texts in the paragraph, \baselineskip is the distance between the two base lines of the texts. \baselineskip is specified at the font selection stage. The primitive command \fontsize{size}{skip} sets this value. Usually a 10pt font size is associated with 12pt line skip.

The command \arraystretch scales the height of the cell by a factor. As in the above codes, the spacing of a row in the table is 2 times the default.

Note that the default height of a row in a tabular cannot be changed by manually setting \baselineskip. As to my current knowledge, the height of a row can only be changed by specifying a different \arraystretch factor. (Similarly, if you want to change the line spacing in the texts, such as to double spacing, do not change \baselineskip. Use \baselinestretch instead.)

• 245,568 hits