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 file draws the Olympic rings using PSTricks.
% File name: rings.tex
% Compile:
%  $ latex rings
%  $ dvips rings -E -o rings.eps
%  $ eps2png -width 600 rings.eps










And this is the result:

Oh, by the way, remember the day this post was initialized, Aug 8, 2008. It’s the day for which we Chinese have been dreaming for years.

Math Spacing and Length Units


Math spacing:

\quad width of M
\! -3/18 quad
\, 3/18 quad
\: 4/18 quad
\; 5/18 quad
\ 6/18 quad
\qquad 2 quad

Lengths defined relative to characters:

em width of M
ex width of x
mu 1 mu = 1/18 em

Physical lengths (commonly used):

mm millimeter
cm centimeter
in inch 1 in = 25.4 mm
pt point 1 pt = 1/72.27 in

Physical lengths (not so commonly used):

bp big point 1 bp = 1/72 in
pc pica 1 pc = 12 pt
sp scaled point 1 sp = 1/65536 pt
dd didot point 1 dd = 1238/1157 pt
cc cicero 1 cc = 12 dd

A check sign crossed by a line?

The other day I saw someone posting a LaTeX question. He wanted $\check{x}$ and $\overline{x}$ to overlap, making a symbol just like a letter x with an accent `check’ which is crossed by a line.

It’s my first time to mix up symbols in order to produce a new one, although previously I had seen many such examples. The first command popping up my mind was \kern. Hence I tried the following solution:

\settowidth{\wdo}{$\overline{#1}$} {\kern-\wdo}%

Take the argument #1 to be the letter x for example. In line 2 I made \check{x}, and in line 4 I made \overline{x}. The x in \overline{x} is indeed invisible due to the usage of \phantom. Then the second (invisible) x is displaced to overlap with the first x by the 3rd line. What line 3 does is that it records the width of \overline{x} in \wdo, then moves \overline{x} by a displacement -\wdo. Now \check{x} and \overline{x} overlap!

I had tried to remove the command \phantom. Then both the x’s in \check{x} and \overline{x} are visible. When they overlap, in some printing devices the resultant x looks darker and bolder than usual. I ended up using the phantom to make the x look normal.

The following is finally what the new command \checkoverline produces:


Honestly, I don’t know if this is what the guy posting the question wants—the symbols look so ugly..

A Simple Counter

This is to show how to use a counter in LaTeX. Nothing fancy. You will find using a counter is really convenient when necessary. The followings are the four most important commands. Let’s name our counter ctProb.

  • To define a new counter: \newcounter{ctProb}
  • To initialize the counter: \setcounter{ctProb}{0}
  • To increment the counter: \addtocounter{ctProb}{1}
  • To print out the counter: \arabic{ctProb}

Example usage: Let’s imagine that you are an instructor of a course. You are going to assign homework problems. In LaTeX file, you would like to write as simple as:

\problem This is the first homework problem..
\solution This is the hidden solution..
\problem This is another problem..
\solution This is the solution to the problem..

Of course, you have to define the \problem and \solution command. In the \problem command there should be a counter associated to it, such that you don’t need to manually number the problems (just like the \section command). This is what you have to put in the preamble. Simple!

\paragraph{Problem \arabic{ctProb}}}
\emph{Solution}: \hspace{2mm}}

Try it!

DIY Your BibTeX Style File

This is a short article explaining how to do handy hacks on a BibTeX style file. To customize your own style, you can type in command

latex makebst.tex

to enter an interactive program and generate your own .bst file. But this is VERY tedious. For those of you who just want to modify an existing style file a little bit to cater certain formatting requirement, this article introduces some basic knowledge about a .bst file. I hope that it is a good starting point to guide you into the world of hacking. When reading this article, you will find it convenient to also read an existing style file, say plain.bst, shipped with your latex installation, side by side.

Basic Structure of a BibTeX Style File

The following shows roughly what is inside a .bst file, which logically contains four sections:

{ author

{ … }
{ … }

MACRO { … }{ … }

FUNCTION { … }{ … }


At the very top is the ENTRY command. The first argument of ENTRY defines legitimate entries in your BibTeX database, such as author, title, journal, year, etc. The second and third augments define internal (but global) integer variables and string variables.

The MACRO commands define abbreviations. For example, the command

MACRO {ieeetcad}{"IEEE Trans. Comput.-Aided Design Integrated Circuits"}

says that if you type `ieeetcad’ in your .bib file, it will be expanded as `IEEE Trans. Comput.-Aided Design Integrated Circuits’ by BibTeX. So instead of typing the long journal name for each cited article in this journal, you only need to enter `ieeetcad’. This trick functions essentially the same as your defining @string macros in your .bib file.

Then come the INTEGERS and STRINGS commands, defining variables that are needed by the immediate FUNCTION. The FUNCTION commands define all the utility functions. It is the most important ingredient in a .bst file. Hacking is mostly done to the FUNCTIONs.

After all the above preparations, BibTeX starts to do its job. The sequence of commands READ, EXECUTE, ITERATE, SORT, EXECUTE, REVERSE… exactly tells the working flow how BibTeX processes and typesets the bibliography entries. First it READs in your BibTeX database file .bib, then it EXECUTEs some commands (defined previously as FUNCTIONs), ITERATEs through all the read-in reference entries, SORTs the entries (e.g. according to author name), and then EXECUTEs, ITERATEs on the sorted entries, REVERSEs, again and again, until all the references are satisfactorily typeset. Note that BibTeX READs only once, but can EXECUTE, ITERATE, REVERSE, and SORT multiple times, until the job is done.


All the formatting of the reference entries are done in FUNCTIONs. The FUNCTIONs are implemented in stack-base, i.e., a LIFO fashion. (Well, you really don’t need to understand this, if all what you care about is formatting layout.)

For formatting of author names, let’s locate the FUNCTION format.names. You will see inside the function there is something like

"{ff~}{vv~}{ll}{, jj}"$

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.}"$

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 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:

c & c \\
c & c \\

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.)


Blog Stats

  • 255,203 hits