Archive Page 2

TeX Fonts (I): font attributes

Maybe You never worry about fonts. Right, since for most people Computer Modern or Times are perfect enough to make a pdf document look good. And what’s more, seldom would there have been LaTeX tutorials covering topics on fonts. Fonts are considered for TeXhackers, not for TeX users.

On the other hand, people who are interested in typography care too much about font looks. Even a person who has just installed a brand new Windows system will proceed to his first configuration task: use ClearType to antialiase screen fonts rendering (Well, this post was written at the time of Windows XP. Things have changed dramatically since then). Let alone Unix/Linux fans and TeX/LaTeX zealots.

This series of articles does not instruct one how to install fonts, neither does it teach specifically how to use different fonts in a LaTeX document. Instead, it gives a big picture about how fonts are dealt with behind the scene. Understanding the principals helps one on knowing, such as, “what affects the document’s texts”, rather than “what steps to take to improve the layout”. It is even conjectured that this article will soon be obsolete due to the new font schemes in LaTeX3.

Under the New Font Selection Scheme (NFSS) for LaTeX2e, a font has five attributes: encoding, family, series, shape, and size. For example, OT1/cmr/m/n/10 means that the document is using a font in OT1 encoding scheme, computer modern roman (cmr) family, medium weight (m) series, normal upright (n) shape, and 10pt size. These five parameters will be displayed whenever LaTeX gives an overfull box warning.

To change the current font layout, one can use a series of \font... commands, followed by a \selectfont activation. Example:

\fontencoding{T1}
\fontfamily{ptm}
\fontseries{b}
\fontshape{it}
\fontsize{12}{15}
\selectfont

It instructs LaTeX to use bold italic 12pt ptm font with 15pt baselineskip in the T1 encoding scheme for the texts. ptm is the widely used Adobe Times font, as oppose to cm.. (computer modern) families which might be hardly popular outside the TeX world.

Just to give a sense of what values are possible for each attribute in a font:

Encoding: OT1, T1…
Family: cmr, cmss, cmtt (computer modern); ptm, phv, pcr (Adobe)…
Series: m (medium), b (bold), bx (bold extended), sb (semi-bold), c (condensed)
Shape: n (normal/upright), it (italic), sl (slanted), sc (small caps)
Size: 5pt, 9pt, 10pt, 12pt, 14.4pt, 24.88pt…

Note that not all combinations of the attributes are valid.

Besides the above lower-level commands, LaTeX provides default settings for each document class. One can use different fonts by renewcommand‘ing the defaults. For example, \familydefault is originally set as \rmdefault, which in turn is set as cmr. One can issue a command like:

\renewcommand{\rmdefault}{phv}

to use Adobe Helvetica (phv) as the default font face. Changing \seriesdefault and \shapedefault are similar. However, font size has to be changed in a different way, e.g.:

\renewcommand{\normalsize}{\fontsize{12}{14.4}\selectfont}

This sets a 12pt font size. Font encoding is usually changed by using a different package, such as:

\usepackage[T1]{fontenc}

This forces a T1 encoding.

To summarize, a font in LaTeX is specified by five attributes. Except for the encoding, the other four should be intuitive to gentle users. There are specific commands to select a different font. The encoding scheme shall be discussed in the next article. A last note is that all the above values mentioned in this article are for text fonts. Math uses different fonts and values, and needs to be dealt with additionally. But the principals are the same. There are also some tailored packages designed to set a specific font for everything (including text and math), such as mathptmx, courier, avant. Readers are encouraged to explore these packages.

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

\documentclass{article}
\usepackage[dvipsnames]{pstricks}
\pagestyle{empty}

\definecolor{myblue}{rgb}{0,.52,.78}
\definecolor{myyellow}{rgb}{.96,.76,0}
\definecolor{mygreen}{rgb}{0,.62,.24}
\definecolor{myred}{rgb}{.87,0,.14}

\newcommand{\drawring}[1]{%
\pscircle[linewidth=.25,linecolor=#1](0,0){1.375}%
\pscircle[linewidth=.05,linecolor=white](0,0){1.425}%
\pscircle[linewidth=.05,linecolor=white](0,0){1.125}%
}

\newcommand{\drawarc}[3]{%
\psarc[linewidth=.25,linecolor=#1](0,0){1.25}{#2}{#3}%
\psarc[linewidth=.05,linecolor=white](0,0){1.4}{#2}{#3}%
\psarc[linewidth=.05,linecolor=white](0,0){1.1}{#2}{#3}%
}

\begin{document}

\color{white}
\fboxsep=0pt
\fbox{

\begin{pspicture}(-4.5,-3)(4.5,1.5)
%\psgrid
\rput(-3,0){\drawring{myblue}}
\rput(-1.5,-1.375){\drawring{myyellow}}
\rput(-3,0){\drawarc{myblue}{330}{30}}
\rput(0,0){\drawring{black}}
\rput(-1.5,-1.375){\drawarc{myyellow}{30}{90}}
\rput(1.5,-1.375){\drawring{mygreen}}
\rput(0,0){\drawarc{black}{330}{30}}
\rput(3,0){\drawring{myred}}
\rput(1.5,-1.375){\drawarc{mygreen}{30}{90}}

\end{pspicture}

}
\end{document}

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.

I got a badly conditioned matrix

A = [0.0031 1 0 0 0 0 0 0
0 0.0031 1 0 0 0 0 0
0 0 0.0031 1 0 0 0 0
0 0 0 0.0031 1 0 0 0
0 0 0 0 0.0031 1 0 0
0 0 0 0 0 0.0031 1 0
0 0 0 0 0 0 0.0031 1
0 0 0 0 0 0 0 0.0031];

Matlab says that its numeric rank is 7.

The interesting thing is that A is a Jordan block!

Find all the empty cells?

Suppose I have a cell array which looks like this:


C =
[] [] []
[] [] [3x3 double]
[] [] []

How can I get all the empty cells using as few commands as possible? Well, the answer is …

cellfun(@isempty, C)

So, cellfun is a function that applies a function (isempty here) to each cell of a cell array (C here). If you enjoy general Matlab functions that implicitly apply to each element of an array (say, sqrt(M) does the square roots to all the elements of M), then you must also enjoy this cellfun function that applies an arbitrary function to each element of a cell array. Check it out!

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

I Love Typography

http://ilovetypography.com/

Noninteractive Matlab

The following command runs a matlab script under Linux in a batch mode:

jchen@peach $ matlab -nodesktop -r my_script

Remember to put the exit command at the end of the script my_script.m to enforce Matlab to quit. This noninterative way is useful when you are submitting a job to a machine that you cannot interact with. Usually this machine is a super computer..

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:

\newlength{\wdo}
\newcommand{\checkoverline}[1]{\check{#1}%
\settowidth{\wdo}{$\overline{#1}$} {\kern-\wdo}%
\overline{\phantom{#1}}}

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:

checkoverline1.png

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

MATLAB sparse matrix: vec==0 vs. length(find(vec))==0

Remember that in Matlab sparse matrices are stored column-wise. Hence it is much more efficient to do column operations than to do row operations on a sparse matrix. When possible, always have two copies of the matrix A: A and AT=A’. This is important for time critical experiments.

Now let’s restrict to column operations. How do we find zero columns of a sparse matrix? There are two (or many more?) possible ways: (1) Use the A(:,i) == 0 test; (2) Use the length(find(A(:,i))) == 0 test. It turns out that the former method is way slower than the latter. Try it! Use the following codes:

m = 10000;
n = 10000;
density = 1e-4;
n_zero_col = 100;


% generate sparse matrix A
A = sprand(m, n, density);


% zero out some columns
idx = ceil(rand(n_zero_col,1)*n);
A(:,idx) = 0;


% two ways to find zero columns
% way 1: the `==0' construct
tic;
zero_cols = [];
for i = 1:n
if A(:,i)==0
zero_cols = [zero_cols i];
end
end
toc


% way 2: the `find' construct
tic;
zero_cols = [];
for i = 1:n
if length(find(A(:,i)))==0
zero_cols = [zero_cols i];
end
end
toc

« Previous PageNext Page »


Categories

Blog Stats

  • 130,298 hits

Follow

Get every new post delivered to your Inbox.