Archive

Posts Tagged ‘LaTeX’

LaTeX Tip of the Day – equation numbering by section or chapter

March 11th, 2009

Let’s say you have a nice LaTeX document with sections, and it’s full of numbered equations. Normally, the equation numbers will start at 1 and keep counting up. If your document gets long, though, you may find yourself with hundreds of equations, and when a reader tries to look one up, they have to search around for a while.
To fix this problem, you can number your equations within sections (so the numbers will go (1.1), (1.2),…, (2.1),(2,2),… instead of (1),(2),…), just add

\usepackage{amsmath}
\numberwithin{equation}{section}


to the preamble of your document.
Of course, if you want numbering by chapter or subsection, just replace section with the relevant name.

Typesetting , ,

LaTeX Tip of the Day – \fbox, \mbox, \makebox, \framebox

March 4th, 2009

When typesetting with LaTeX, sometimes you need to take a little control back from the automatic routines that determine linebreaks. Or perhaps you want to manually control the placement of text on a line.
The latex command \makebox[w][position]{content} inserts an unbreakable box of width w (10pt, 2cm, 0.23in, \textwidth will work), where the content is positioned flush left (use the letter l), flush right (r), centered (c), or will fill the box (s). The square brackets mean that those arguments are optional (if you only specify one, LaTeX assumes you’re giving the width). The default positioning is (c), and the default width is just big enough to fit the content (if you specify a width that is too small, the text will overflow onto text outside the box.

examples of \makebox in LaTeX

The command \framebox is exactly the same, except it draws a solid line around the box:

examples of \framebox in LaTeX

The commands \mbox{content} and \fbox{content} are equivalent to \makebox{content} and \framebox{content}, respectively (\mbox and \fbox are robust, so you should use them if possible). To change the thickness of the outline drawn by \fbox to 2pt, add the command \renewcommand{\fboxrule}{2pt} anywhere before the \fbox.

examples of \mbox and \fbox in LaTeX

Note how the boxes make the line overflow.

Typesetting , ,

LaTeX Tip of the Day – paragraph styles

February 5th, 2009

There are two really quick commands to change the way new paragraphs begin. In the preamble (before begin{document}), add:

setlength{parindent}{0.0in}
setlength{parskip}{0.1in}

and set the two lengths to suit your needs. Acceptable units are: in, cm, mm, pt, ex, em (ex and em are approximately the width of the letters x and m, and scale with the font size), or you can use setlength{parskip}{1.0baselineskip} to get a full blank line before each new paragraph (the baseline skip is the vertical distance from one line of text to the next), or replace 1.0 with some other number for bigger or smaller spacing (which also scales properly if you increase the line spacing).
If you have a paragraph with special needs, you can specify noindent right before the text. You can also insert a space equal to th paragraph indent with indent.

You can also change line spacing with

renewcommand{baselinestretch}{1.2}

where the numerical factor is 1 for single spacing, 2 for double spacing, etc.

Typesetting , ,

LaTeX Tip of the Day – cases

January 30th, 2009

If you want to define a piecewise equation in your typeset math, or if you want to express a conditional statement, or just group a set of related equations in math mode, then you’re looking for the cases environment. It’s the way to get that big curly brace on the left of a couple of lines of math. In math mode, the code

f(x,y)=\begin{cases}
0 &\text{for~}x\leq 0\,,\\
\sin x+\phi &\text{for~}x > 0\,.
\end{cases}


will produce the output:
$$f(x,y)=\begin{cases}0 &\text{for }x\leq 0\,,\\ \sin x+\phi &\text{for }x > 0\,. \end{cases}$$

Note that you use an alignment character (&) on each line, and the first character after it on each line will align vertically (it’s as if you’re making a 2-column table). Each line ends with \\ save the last one.

This entire construct only gets one equation number (if you have equation numbering on), and you can’t specify the alignment of the first part of each line. Both of these problems are solved by the cases package.

Typesetting , ,

LaTeX Tip of the Day – LaTeX logo

January 29th, 2009

On the web, we write LaTeX (lay-tech) with that goofy (and cumbersome) capitalization to distinguish it from regular old latex (lay-tecs), even though google makes no distinction. The proper way to represent LaTeX in a LaTeX document is with the command \LaTeX (make sure you use the goofy capitalization and the beginning slash), which produces:



This isn’t strictly required, but it makes things look more professional.

Typesetting , ,