Archive for the 'Sticky posts' Category

Become an Excel ninja

Okay, you know that Excel can do way more than you use it for, but you’re not exactly sure what it can do, and even if you did, you wouldn’t know how to make it do those things. Don’t panic, you’re not alone. The problem is that you are not yet an Excel ninja. Read these tips and you’ll be impressing your boss and frightening children with your spreadsheet ninjitsu skills. But remember, the journey of the Excel ninja is never-ending, and this is just the beginning. To learn more, you must visit the temple of wisdom.

Important notes

1. All of the tips below work in Microsoft Excel on both Windows and Mac. They also work in any incarnation of OpenOffice/NeoOffice Calc if you replace commas with semicolons in formulas.

2. The tips presented here don’t apply specifically to statistical analysis, but users should be aware that there are several documented problems regarding the use of Excel for statistics. If you need a reliable system for conducting statistical analysis, consider using the open-source environment R (tutorial here).

1. Formulas

Excel ninjadom is all about using formulas (or functions). Formulas turn data stored in your spreadsheet into new information: sums of ranges of cells, information about particular cells, new cells based on old cells, etc. There are many formula functions, and they can be browsed by accessing Insert > Function (tip: through the insert function dialog, you can directly access Excel’s help entry for each function).

Functions take the form:

function_name(first_argument, second_argument)

There may be many arguments, and some argument may be optional (OpenOffice users please note that you need to type function_name(first_argument; second_argument)).

To enter a formula, click in any cell and type “=” followed by the function. For example:

Formula1.png

The example above shows the sum() function in use (the result is shown on the left). After you have entered a formula, simply hit enter to have Excel calculate the result[1]. Note that the function is displayed in its cell as well as in the formula bar (top). When enter is pressed, the cell will display the result, rather the function.

The example also shows how to include a range of values in Excel (using the colon). Typing b1:b3 tells Excel to calculate the function for the cells between and including b1 and b3. Note that if the example above said sum(a1:b3) all of the cells in the rectangle created by joining all of the cells between and including a1 and b3 would be summed (and the result would be 12).

2. Show formulas

There will be cases when you need to create a spreadhseet that uses many complicated formulas. In these situations, it can be difficult to remember exactly what each formula says. However, by hitting control+` (Mac and Win), you can toggle show formulas:

ShowFormulas.png

This view makes it much easier to keep track of your work (if it was still 1999, I’d even go so far as to call this feature hella useful).

3. Filling

If you have typed a formula and you want to reproduce that formula in other cells, you can use the fill feature to do so automatically. There are several ways to do this. You can drag the cell by the dot in the bottom right of the cell:

DragPoin2.png

See it? It’s subtle. Dragging on any other part of the cell border will move rather than fill the cell. Alternatively, you can highlight the range of cells that you want filled with the formula in your original cell (make sure that cell to be copied should be included in the highlighted range, and should be either the top-most or left-most cell in the range). Once the appropriate cells are highlighted, you can either (a) select Edit > Fill > Down (or > Right if the range is horizontal) or (b) hit control+d (or control+r).

4. Special formula syntax

There are a handful of special characters of formats that you need to know in order to use formulas effectively:

$. When you fill a formula, the cell references in the formula are updated according to the direction in which you perform the fill. For example, if cell b1 contains the formula “=a1″, it will display the contents of cell a1. But if you highlight b1 and fill it down through b5, cell b5 will contain “=a5″ which will display the contents of cell a5. Alternatively, if you dragged b1 to the right, so that b1-d1 were filled, cell d1 would contain the formula “=c1″ which would display the contents of cell c1. In order to prevent the cell references from adjusting, precede the original references with “$”. For example, if you type “=$a1″ into cell b1 and drag it to the right, the filled cells will still refer to cell a1. If you type “=a$1″ into cell b1 and drag it down, the filled cells will still say “=a1″. If you were going to drag the cell both down and right, you would type “=$a$1″ to prevent the reference to cell a1 from being updated upon filling.

!. It’s possible to have a cell on one worksheet refer to a cell on another worksheet. Say you have two worksheets (Sheet1 and Sheet2). Sheet1’s first cell (a1) contains the number 5, and you want a1 on Sheet2 to have the same contents. To accomplish this, type “=Sheet1!a1″ into cell a1 on Sheet2. Note that Excel is not case sensitive, so “=sheet1!a1″ would have the same effect.

“”. When typing a string (a string is anything that isn’t a number) into a function, surround it with double quotes. For example, to check if cell a1 contains the text “Hello,” type “=if(a1=”Hello”,1,0)” into any other cell, which will return a 1 if a1 says “Hello,” and a 0 otherwise (more on conditional functions below).

&. The ampersand character allows you to glue the contents of two or more cells together. For example, if a1 contains “This and” and a2 contains “that.” then typing “=a1&a2″ into another cell would produce “This and that.”

5. Paste values

When you copy a cell (or range of cells) that contains a formula, and paste it elsewhere, the results of the formula are not pasted, the formula itself — adjusted to reflect the horizontal and vertical displacement of the formula’s cell reference relative to the orginal — is pasted. For example, if cell b2 contains the formula =”a1″ is copied and pasted into cell c3, the new formula will be “=b2″ (because the formula was moved 1 column to the right and one column down, so a1 becomes b2).

To avoid this, select Edit > Paste Special and check Values in order to paste the calculated results from the original formula, rather than the formula itself:

PasteValues.png

With the basics of formulas and formula editing out of the way, it’s time to move on to specific functions that are especially useful.

6. Logic

There are two basic logical functions: if and and.

If() checks to see if a certain condition is met and returns user-specified values depending on that condition. Here is the basic syntax:

if(condition, result_if_true, result_if_false)

The conditions can be numeric. In the following example, Excel checks to see if the specified cells are less than 3, and returns a 1 if they are and a 0 if they are not:

If.png

Note the tips that (newer versions of) Excel give while typing a function; these can guide you if you have forgotten part of a function or the arguments that it can take (the arguments in square brackets are optional and the bold argument is the one that you are currently typing).

Functions can be nested, so you could also type:

if(a1<3,sum(a1:a3),average(b1:b3))

Which would give the sum of cells a1 through a3 if cell a1 is less than 3, and the average of cells b1 through b3 if cell a1 is 3 or greater. For an example of a text condition, see Tip #4.

And() allows you to join several if statements. The problem with and is that it stands the English language on its head. You are naturally inclined to say “if X and Y, then Z.” However, if the Microsoft was the architect of English grammar, you would say “And X, Y, then Z.” Hence the syntax of the and statement:

and(condition1,condition2)

So, using the example in the image above, the formula:

if(and(a1<3,a2>10),1,”Sorry, the condition is false”)

if cell a1 is less than 3 and cell a2 is greater than 10, the cell containing the formula will return the value 1. If either of these conditions is untrue (or if they both are), the cell will say “Sorry, the condition is false.” I have intentionally mixed numeric and string user-specified results to demonstrate how they can work when used together.

7. Conditional love

Using if(), you can tell Excel to conditionally process a formula depending on the value of one cell. However, there are times when you want process a formula depending on the contents of an entire range of cells. Say, for example, that you want to know how many cells in a range are less than a particular value. Using the countif() function, this task is easy:

countif(a1:a10,”<2″)

The syntax of the countif() function differs from most in that double quotes are used to denote a non-string argument. However, you can check both numeric and string conditions in the second argument of the function. For example: countif(a1:a10;=”Smith, John”).

A similar function is sumif(), which will sum all of the cells in a specified range that meet a certain condition. For example,

SumIf.png

The above example adds the cells between a1 and a3 depending on the contents of cells b1 through b3. Note the difference between sumif() and countif(): the former has three arguments — the range of the condition, the condition itself, and the range to be summed; the latter only needs to know the range to count and the condition upon which to count.

8. Look it up

The vlookup() function scans a range of values and returns adjacent values. This function is useful when you want to assign new values based on old ones. Consider the following example:

vlookup.png

The top group of cells acts as an index. The function then scans the cells from c7 to c11 and returns (in cells b7 to b11) the letters that appear next to the scanned numbers in the index group.

The syntax for vlookup() is:

vlookup(number_to_look_up, range_to_look_in, column_to_return_value_from, don’t_return_exact_match)

An analog to vlookup() is hlookup(), which works the same way but expects the index to be laid out horizontally. Also note the argument for “don’t_return_exact_match.” This is a logical argument, so Excel only needs to know if it is true or false. You can either type “true” (but you don’t need the quotes) or simply enter a 1 for true and a 0 for false.

9. Offset

The offset() function allows you to return the value of any cell by specifying its location in your spreadsheet relative to another cell. It takes the form:

offset(reference_cell, rows_down, cols_right)

where rows_down is the number of rows down the desired cell is from the original cell, and cols_right is the number of cells to the right the desired cell is relative to the original cell (if you need to move up or left, you enter negative numbers).

offset.png

In the example above, the highlighted cell returns the value originally contained in b4 by referencing the cell 3 down and 1 over from a1. Offset is useful in many situations, especially when data need to be reorganized.

10. Substrings

One area where it can actually be more convenient to use Excel than a more sophisticated data analysis package is working with substrings — pieces of cells that contain text. Two basic substring functions are right() and left(), which take the form:

left(cell, num_characters)

and return the first num_characters on either the left or right side of the selected cell. Another useful substring function is find() which uses the following syntax:

find(”text_to_find”, cell_to_find_it_in)

Find() returns the character in the cell in which “text_to_find” first appears. For example, applying find(”this”, a1) to if a1 says “Let’s do this” would return the number 10 because the first letter of “this” is the 10th character, including punctuation and spaces, in the cell. The following example shows a practical example where left() and find() are used together to parse the last name out of a list of names. The formulas:

SubstringFormulas.png

would produce:

SubstringResults.png

The find() function searches column A for the position of commas and the left() function returns everything to the left of the comma. Note that this could be done in one formula: left(a1,find(”,”,a1)-1)).

A summary example

The following example combines many of the functions and techniques described above. You have a series of letters arranged in three columns:

ExampleStart.png

What you want is every other letter from this series, in one column:

ExampleFinish.png

To do this, try the following formulas:

ExampleFormulasLg.png

Moving from right to left, here’s what these formulas do:

Column C simply contains the offset() function. It returns values from the original data (cells a1:c4) by offsetting cell a1 by the number of rows specified by the formulas in column B and the number of columns specified by the formulas in column A. If the result of the offest() function is blank, it returns the text “No more #s.” This is accomplished using a simple if() statement that checks to see if the result is blank (e.g., the result is “” — a blank string). Note that “<>” is the Excel syntax for “not equal to.”

Column B determines the number of rows that should be skipped. We want every other letter, so we need to skip the first and third rows of the first column, then move to the next column, skipping the first and third row of that column, etc. To do this, we start with a 1 in cell b5. This is the first vertical offset. The next offset should be 3 (the third column is skipped). To do this, we check to see if the previous B cell equals 3. If it doesn’t, we need to add 2 to it. If it does, we need to skip to the next column and revert to 1. Hence the if statement: if(previous B cell = 3, 1, previous B cell + 2).

Column A does the same thing, but for columns instead of rows. It relies on the values from the B column to determine when it’s time to move over one column. Hence the if statement: if(previous B cell = 3, previous A cell + 1, previous A cell). We start with 0, so at first no columns are skipped. But when a B cell equals 3, the next A cell equals the previous A cell + 1. As a result, the A cells don’t increment until the B cells have incremented twice.

Together, these formulas give the following result:

ExampleResults.png

Which is exactly what we wanted: every other letter in one column.

Congratulations, it’s a ninja!

The material above simply introduces the use of formulas in Excel (or Calc) and gives examples of some of the more advanced functions. There are many functions, including ones specific to mathematics, statistics, finance and more. Information about additional functions is available through Excel’s built in help system as well as the web. As an Excel ninja, you now have all of the necessary information to learn and use new functions.

Notes

[1] Note that when array functions are used, control enter must be pressed in to calculate the value. Array formulas are not discussed in this tutorial. Google “linest” for an example.

This entire tutorial is available as a PDF file.

LaTeX: from beginner to TeXPert

This post introduces the LaTeX typesetting system. After digesting the information below, you’ll be able to:

  • Download and install LaTeX on your PC or Mac
  • Create basic documents using LaTeX
  • Install new LaTeX packages
  • Insert tables and figures into a LaTeX document
  • Use LaTeX’s cross-referencing, footnote and basic bibliography features
  • Insert equations into a LaTeX document

These topics cover the majority of tasks that most people need to do when writing a document. However, please note that while the LaTeX system makes it very easy to create professional-looking documents, it is both comprehensive and extensible. There are many topics that are not covered by this basic tutorial. Fortunately, LaTeX is very well documented. If you come across something that you can’t figure out how to do, ask your old friend Google for help.

What is LaTeX?

At its core, LaTeX is a typesetting system that allows authors to create highly polished documents without having to worry about formatting, page breaks, object positioning, or any other style concerns that distract authors from focusing on writing. LaTeX is pronounced “lay-tech,” as it is an extension of TeX (”tech”), the original typesetting system. You can read all about the history of TeX and LaTeX on Wikipedia.

LaTeX is used widely in a variety of professions. Mathematicians, physicists, economists, statisticians and other academics and professionals that regularly use mathematical notation in their documents often use LaTeX because of the ease with which it handles such notation. Many publishers use TeX-based systems for typesetting documents.

How does LaTeX work?

LaTeX differs from traditional word processors in two fundamental ways:

  1. Generally, LaTeX documents are written using the easy-to-learn LaTeX markup language, rather than by using a graphical interface to apply styles[1].
  2. LaTeX works with your document after you have entered your text. So unlike word processors, it can use information about the total length of your document, number of tables, etc. to find the optimal places for tables, figures, page breaks, etc.

The following is an example of a very basic LaTeX document:

\documentclass{article}
\author{Your Name}
\title{Test Document}
\begin{document}
\maketitle
This is a test document
\end{document}

With any LaTeX distribution, saving the above text as a .tex file and running LaTeX on that file would produce the following:

Testdoccropped

LaTeX is designed to create the same output on any system. So if you distributed the above text to anyone with a working LaTeX distribution, regardless of their particular system, they would get the exact same result. LaTeX outputs files in several formats, but the most popular is PDF.

Getting LaTeX

All you technically need to create LaTeX documents is a LaTeX engine — the binary files and libraries that will convert plain text tex files to polished pdf files. LaTeX can be run from the command line, so *nix and DOS aficionados will feel right at home. However, using a frontend for LaTeX can make things much easier. Most frontends are essentially text editors with functions to

  • Compile documents with LaTeX without using the command line
  • Facilitate writing in the LaTeX language (wizards for table creation, code completion, etc.)

In this document, I assume that you’ll need both a LaTeX engine and a frontend. There are many engines and frontends to choose from on every operating system. I’m going to describe how to install the most popular (and easy to install) open-source tools for OS X and Windows. The only difference between using the distributions that I describe and others is configuration and practical difference between applications, so feel free to try out other distributions.

On Mac OS X

Engine. gwTeX is a free and open-source LaTeX distribution for OS X that comes with a graphical installer. To install, you download the i-Installer application, select a mirror, then select the TeX package. Additional installation instructions are available on the download page. Once installation is complete, all you need is a frontend.

Frontend. TeXShop is a very popular LaTeX frontend for OS X. Installation requires a simple drag and drop to the /Applications folder. TeXShop is automatically configured to work with gwTeX, so if that’s the engine that you’re using, you’re set.

To test out your distribution, try saving the sample document above as a .tex file and running LaTeX on your document by pressing command-t. If everything is configured properly, a window will appear similar to the example output above, and a new PDF file (as well as a log file) will appear in the directory where your file is saved.

On Windows

Engine. MikTeX is a popular open-source distribution. To install, visit this page, download the executable, and follow the dialog. Additional installation instructions are on the download page.

Frontend. TeXnic Center is an open-source frontend with many helpful features. Installation is standard, just download and open the executable, which opens a wizard.

TeXnic center is automatically configured to work with MikTeX. To test out your setup, save the sample document above as a .tex file using TeXnic Center and select Build > Current file. If everything is set up properly, a new PDF file (along with a log file) will be created in the directory where your document is saved.

On Linux

Linux systems have their own application management utilities (apt-get or rpm, for example), and installation will depend on your particular Linux distribution. Ubuntu users can use the Synaptic Package Manager. Kile is a popular and easy-to-use frontend that works with both KDE and Gnome.

A note about file types

LaTeX can make several types of output files, including PDF and DVI (device independent) files. The type of output depends on whether PDFLaTeX is used to process the file, or another program. The default for the frontends defined above is to create PDF files, but be aware that changing these settings might affect the type of output created.

LaTeX basics

LaTeX commands

LaTeX commands generally begin with a backslash and take the form \command[options]{argument}. For example,

\section{Introduction}

would define a new section, named “Introduction.” The “%” character defines a comment, and everything from that character to the end of the line is commented out and will be ignored by LaTeX. To insert the “%” character into a document, escape it with a backslash: \%.

Quotes work a bit differently in LaTeX. To insert quote marks, use the form “text”. That is, the ` character (top left of the keyboard) twice, followed by the single quote character, ‘, twice.

The preamble

Everything before the line “\begin{document}” is part of the preamble. A typical preamble might look like this:

\documentclass{article}
\usepackage{graphicx}
\title{Test}
\author{Test}
\date{}

In the example above:

  • \documentclass{article} tells LaTeX that the document is an article. Other classes include book, letter and slides
  • \usepackage{graphicx} tells LaTeX to use the graphicx package, which allows users to include many types of graphics in their documents. Packages are covered later on
  • \title{} and \author{} obviously define the title and author
  • \date{} tells LaTeX to leave the date blank. \date{April 2006} would print “April 2006″ as the date. Leaving the \date{} line out would cause LaTeX to use today’s date.

The \documentclass{} command has options. For example, \documentclass[11pt,twocolumn]{article} would organize body of the document into two columns. Note that options are separated by a comma. Other options include:

  • oneside or twoside - change margins for a one or two-sided document
  • landscape - change the document from portrait to landscape
  • titlepage or notitlepage - define whether there is a separate title page, or if the title, author and date info are presented at the top of the article

The document body

Everything after the preamble and between \begin{document} and \end{document} is part of the document body. Most of a LaTeX document is simply plain text. To start a new paragraph, insert two carriage returns (blank lines). LaTeX will ignore one blank line. To force a line break, use \\.

Document structure

A document’s structure is defined using \section{} commands. LaTeX is strongly based on well-structured documents. The structure tags include:

  • \section{Name}
  • \subsection{Name}
  • \subsubsection{Name}
  • \paragraph{Name}

To insert an unnumbered section, use the command \section*{Name}. The section numbering will continue as normal with the next section, subsection, etc.

The \paragraph{} command doesn’t need to be included unless you want to insert a heading for a paragraph. The image below shows the different structure commands in use:

Sections-1

Environments

Environments are special blocks of text. For example, the itemize and enumerate environments create bulleted and numbered lists, respectively. The following markup:

\begin{itemize}
\item First thing
\item Second thing
\item Third thing
\end{itemize}

\begin{enumerate}
\item First numbered thing
\item Second numbered thing
\end{enumerate}

Would produce a bulleted list followed by a numbered list.

Note that environments always begin with \begin{environmentname} and end with \end{environmentname}. They can be nested, so one item of a bulleted list might contain another bulleted list, or a numbered list, etc.

Other frequently used environments include:

  • Quote: \begin{quote}…\end{quote} creates a section of indented, quoted text
  • Verbatim: \begin{verbatim} … \end{verbatim} is similar to pre in HTML. In the verbatim environment, text is printed in a monospace font and special characters are ignored. Verbatim is useful for typing code tips
  • Description: \begin{description} \item[First item] text \end{description} creates a list or items with bolded names and hanging-indented text after the item name

Modifying text styles

The basic idea behind LaTeX is to absolve the author of formatting duties. Nevertheless, it’s still occasionally necessary to manually format certain text styles.

  • To insert bold text, use \textbf{text here}
  • To insert italic text, use \emph{text here}
  • To insert monospace text, use \texttt{text here} (the tt stands for teletype)
  • To use verbatim text within a sentence, use \verb|your text here|. Note that any delimiter can be used, for example \verb+your text here+ will produce the same results

Packages

Packages extend LaTeX’s functionality. Package installation essentially consists of two steps:

  1. Running LaTeX on the .ins file to produce .sty or .cls files
  2. Copying the newly created files to an appropriate directory and updating the LaTeX database

However, there are exceptions. The filetypes .sty and .cls stand for style and class, respectively. If a package does not come as a .ins file, but rather a sty or cls file, it does not need to be processed with LaTeX, and you can skip directly to step two. Also, running LaTeX on a .ins file usually produces a .dtx file. This file can be processed with LaTeX to create a manual for the package.

Note: To process a package file (ins or dtx) with LaTeX, just open that file with your frontend and process it like you would a normal tex file.

OS X. To install a new package on your Mac using gwTeX, process the files as described above, and move the sty, cls and other files to ~/Library/texmf. If this directory does not exist, create it.

Windows. The easiest way to install a package on a PC using MikTeX is to use the MikTeX package manager, which is available through the Start Menu. Just open the package manager, select a mirror, and navigate to the package that you want to install. MikTeX will take care of the rest. Another nice feature of MikTeX is that if you are processing a .tex file that requires a package that isn’t installed on your machine, it will prompt you to download it.

Next, I discuss two popular packages: graphicx and geometry. These packages are already installed with gwTeX and MikTeX, so there is no need to download and install them.

The graphicx package

The graphicx package allows you to insert images into a LaTeX document. To use it, first use the command \usepackage{graphicx} in your document preamble. Then, to insert a graphic, use the command:

\includegraphics[options]{filename.png}

graphicx supports many filetypes, including PDF, PNG and JPG. The options include:

  • width=Xin
  • height=Xin
  • scale=X (where x is between 0 and 1)

The geometry package

While formatting documents using LaTeX is easy, changing those default formats can be fairly difficult. The geometry package can make changing certain aspects of your document, including the margins, much easier. To change the margins to 1″ all around, for example, use

\usepackage[margin=1in]{geometry}

Other packages

For just about every modification that you might want to make to a standard LaTeX document, there is a premade package to help you do so. To learn more about the packages described, or to download new packages, visit the Comprehensive TeX Archive Network (CTAN).

Figures and tables

Figures and tables are LaTeX environments, however they have special attributes, such as the \caption{} command, which gives tables and figures names. They are called float elements, because their position in the final compiled document depends on LaTeX’s style algorithm.

Figures

To insert a figure, use

\begin{figure}[hbtp]
\caption{Figure name}
\begin{center}
\includegraphics{filename.pdf}
\end{center}
\label{your-reference-key}
\end{figure}

In the above markup,

  • \begin{figure} simply tells LaTeX that there is a figure environment
  • [hbtp] determines how LaTeX will place the figure (here (h), bottom (b), top(t), page(p)). LaTeX will first attempt to insert the figure at its insertion point in the tex file. If this is not possible due to space or other aesthetic considerations, it will try to place it at the bottom of the page, then at the top of the page, then on a special page reserved just for float elements. The order in which h,b,t and p are specified determines where LaTeX tries to place the float first. To force the graphic to appear in its original place, for example, you could put \begin{figure}[h], omitting b, p and t
  • \caption{Figure name} specifies the name of the figure
  • \begin{center} simply tells LaTeX to center the figure on the page. Don’t forget to end the centering environment before you end the figure environment
  • \includegraphics{…} specifies the location of the file that is being inserted as a figure
  • \label{your-reference-key} is a label that you can use to refer to the figure in the text. For example, if you label your figure “fig1″ then you can reference it later on by typing \ref{fig1}

Tables

A floated table in LaTeX consists of two environments: table, the actual floated entity in the text, and tabular, the data contained in the table. For example,

\begin{table}[hbtp]
\caption{This table is an example}
\begin{center}
\begin{tabular}{c|cc}
First row, first column & First row second column & First row, third column \\ \hline
Second row, first column & Second row, second column & Second row, third column \\
Third row, first column & Third row, second column & Third row, third column \\
\multicolumn{3}{c}{…}
\end{tabular}
\end{center}
\label{exampletable}
\end{table}

would produce

Table

Everything except the code between \begin{tabular} … \end{tabular} is the same as the figure environment described above. Here’ s how the
tabular environment works:

  • \begin{tabular}{c|cc} tells LaTeX to start a new tabular environment with three centered columns. The bar (”|”) after the first “c”, tells LaTeX that the first column has a vertical border. Using {lcrr} would create for columns, the first left aligned, the second centered, and the third and fourth right aligned
  • Table cells are separated by “&” and table rows are separated by “\\”
  • \hline creates a horizontal line
  • \multicolumn{3}{c}{Text here} creates a row that spans all three columns, is centered, and contains the text “Text here”

There are more complicated options for creating and inserting tables, but the rules above cover about 90% of all table needs.[2]

Annotations

LaTeX is capable of automatically creating important annotations, such as footnotes, cross references, tables of contents and bibliographies. Note that, since the following commands require LaTeX to automatically number text elements, LaTeX must be run on your document twice for proper display.

Footnotes

To insert a footnote, simply type \footnote{Footnote text here}. LaTeX will automatically insert the footnote number and text.[3]

Cross references

To reference a labeled Table or Figure, use \ref{your-reference-key} where “your-reference-key” is the argument to the \label{your-reference-key} command in the table or figure environments.

Table of contents

To insert a table of contents, simply put \tableofcontents at the beginning of your document. (You must run LaTeX twice to get the table of contents and references to work correctly.)
Bibliography

To create a bibliography, insert a list of the citations at the end of your document, using the form:

\begin{thebibliography}{99}
….
\bibitem{key1} Disarray, General. 2006. “\LaTeX{}: from beginner to \TeX pert.” \emph{General Disarray Blog}. Available online at \textt{http://generaldisarray.wordpress.com}. ….
\end{thebibliography}

You must manually type the bibliography entries. To refer to an item within the text, use \cite{key}. The {99} tells LaTeX that there a maximum of 99 entries in the bibliography. LaTeX needs to know this so it can correctly justify the bibliography entries with their numbering on the left.

A more efficient way to create bibliographies is to use BibTeX, which allows you to maintain a database of citations and call them as needed in your bibliography. There are also graphical tools for managing your reference databases, so you don’t have to hard code the citations, and can easily change them to different formats. However, BibTeX is too complicated to explain in this document. For an introduction, see this page.

Inserting mathematics

There are several ways to include mathematical notation in LaTeX documents. The most common are inline notation and the displaymath environment.

Inline

To include some mathematical notation within a paragraph, without offsetting from the rest of the text, enclose the notation between dollar signs. For example, $a^2+b^2=c^2$ is our favorite theorem.

Display math

The displaymath environment lets you offset some mathematical notation from the rest of the document. The code

\[
a^2+b^2=c^2
\]

would create a paragraph break and center the equation on the page.

Equation

The equation environment can be used to place numbered equations in the text. For example,

\begin{equation}
a^2+b^2=c^2
\label{pythag}
\end{equation}

would offset the equation just like the displaymath version did, but it would have a number in parenthesis on the right, and you would by able to call it in the text by typing, for example, “as we see in equation \ref{pythag}…”

Equation array

The eqnarray environment allows you to align parts of equations at the equal sign. For example,

\begin{eqnarray}
a&=&b+c\\
d&=&e+f
\end{eqnarray}

would produce

Array

Mathematical notation

There are many commands for inserting specific mathematical operators and symbols into equations. They can all be found online, and as always, use Google if you can’t figure out a specific command. The following are some common operators and commands:

Greek letters: Generally, just use the spelled-out letter. For example, \beta, \gamma and \epsilon. For upper case, use \Gamma.

Misc symbols: \leftarrow (use \Leftarrow for a double arrow), \rightarrow, etc., \leftrightarrow (<==>, if and only if), <, >, \leq (less than or equal to), \geq (greater than or equal to)

Indexing and exponents: Subscripts are denoted using the underscore (x_i) and superscripts use the “^” key (a^2). To type “i sub j comma k” you need to write “i_{j,k}” to tell LaTeX that the “j,k” comprises the entire subscript. The bracket characters are generic grouping operators in LaTeX, and they won’t appear in your document.

Some operators: \sum{1/x} or \sum_{i=1}^{\infty}{x_i}, \prod (the product), \coprod (the coproduct), \sin, \log, \max, etc.

Decorations: \hat{x}, \tilde{x} , \overline{x}, \underline{x}, \overrightarrow{x}, \overbrace{x}, \underbrace{x}, \vec{x}

Fractions: \frac{a}{b} puts a over b.

Brackets: For brackets use “(”, “[" or \lbrace and \rbrace for "{" and "}". However, if the notation that your typing is not inline, use \left( <math here> \right) or \left\lbrace <math here> \right\rbrace.

Matrices: To insert a matrix in either the display math or equation environments, use

\left[ \begin{array}{ccc}
a & b & c \\
d & e & f
\end{array}\right]

Note that the array environment is similar to the tabular environment described above. The code shown above would produce:

Matrix

For help with other symbols and operators, see this page.

For further reference

The instructions above cover many of the basic functions of LaTeX, but there are many more. A good, thorough introduction is The Not-So-Short Introduction to LaTeX (pdf).

Download

This tutorial is available as a PDF file.

Notes

[1] Although commercial implementations of LaTeX, such as Scientific Word, do offer a graphical interface, and LyX is an open-source, LaTeX-based what-you-see-is-what-you-mean typesetting system that essentially uses a graphical interface to apply LaTeX markup to text.
[2] OpenOffice users can use Calc2LaTeX to convert between Calc spreadsheets and LaTeX tables. MS Office users can try Excel2LaTeX, which does the same thing, using Excel spreadsheets. Both utilities are cross-platform.
[3] To create an “attribution” footnote, where the first footnote is marked by an asterisk, use the \thanks{text here} command.

Ten things every Microsoft Word user should know

[Update: This article has been Dugg! If you're reading this, and about to flame about how OpenOffice or LaTeX is better, please read this first. Thanks. -John]

[Update: Since you're obviously in the mood for learning, you may want to give my Excel tutorial a read: Become an Excel ninja.]

[Update: You can download this tutorial as a pdf file.]

Most people use word processors like MS Word as they would a typewriter — manually making section headers bold and centered, inserting hard breaks between paragraphs, etc. This formatting method is fine for short documents, but for long documents that include multiple sections, figures, tables and other elements that need to be styled consistently throughout the text, it pays to learn Word’s advanced features.

These features are easy-to-use, but poorly documented and, in my experience, underused — even by professionals that frequently write long documents. This tutorial presents ten tips to help you start using Word the smart way.

1. Styles

The first five tips introduce and explain the use of styles in Word. Styles are user-defined formats that control the look and feel of paragraphs, characters, tables and lists. By creating styles and assigning them to the elements of your document, you can more easily control your document’s formatting.

In Windows, the Styles pane can be turned on by clicking on the “AA” button on the Formatting toolbar:

StylesWin1.png

On the Mac, the Styles pane is one of the modules on the Formatting palette. On either OS, the pane looks something like this:

Mac1.png

The functions of the different parts of the pane are obvious. Current style of selected text shows either the style currently in use, or a summary of the formatting if the selected text doesn’t use a style. The options under Pick style to apply are either default or user-defined styles. Using the List dropdown, you can change which styles are shown in the Pick style list.

Mac2.png

Clicking on a style name applies that style to the selected text. Clicking on the Style type icon allows you to modify the style. The Modify Style dialog allows you to change every aspect of that style (font, paragraph formatting, and more, depending on the type of style selected).

Mac3.png

For example, the next two images show the same text. The first has indented paragraphs; the second has unindented paragraphs with a 12pt space between them. Rather than creating this formatting manually, the entire page was changed from the 1st to the 2nd style by editing the Normal style from Paragraph: Indentation: First Line: .5in to no paragraph indentation, but Paragraph: Spacing: After: 12pt. Note that on the second example, while it looks like there is a blank line between the paragraphs, there isn’t — the cursor automatically goes from one paragraph to another, leaving a blank space that is the same height as a line of text between.

MacIndent.png

 

MacSkip.png

Okay, that introduces styles. Now on to the good stuff.

2. Header styles and the Table of Contents

If you wanted the first-level header for your document to be Times New Roman, 16pt, bold and centered, you could easily create a new style with that formatting. However, Word has 9 built-in heading styles. They may not look the way you want them to out of the box, but you can easily change them to reflect your preferred formatting.

Why use these styles? If you use the built-in header styles, Word will recognize the structure of your document. This means that Word will know that you are typing a header, a subheader, a subsubheader, etc. It also means that, when you’re done typing your document, you can create an automatic table of contents by selecting Insert > Field > Index and Tables > TOC. You can even have Word automatically number your headings by editing the numbering styles from the Modify Styles dialog.

3. Table styles

If you produce documents that contain many tables, you’re probably familiar with the tedium of formatting table after table after table. Using table styles can make this process substantially easier.

To create a default table style, modify the Table Grid style. When you edit a table style, you can define separate styles for the header row, even and odd rows, the left and right columns, etc. So if you want the header row to be bold with a double border, and the rest of the table to be normally weighted with no border, you can edit the Table Grid style so that any table inserted into the document will automatically have that form.

MacTable.png

You can make additional modifications to the table after the general Table Grid style has been applied.

4. Character styles

Say, for example, you are creating a document that uses code examples (like HTML). You may want to set any code examples in a monotype font when referencing them within a sentence. The old-fashioned way to do this is to select Font > Courier New, type your code, then revert back to the old font. This can become tedious, and if you wanted to change the code tips to another font, you’d have to manually edit every instance of Courier New in your document.

Instead, you could create a new character style by selecting New Style from the Formatting pane and setting Style type to character. Change the font to Courier and now, anytime you want to refer to a piece of code within a paragraph, you can type the code fragment, select it, and select the style that you just created. Only the selected text will be changed.

MacCharacter.png

The difference between a character style and a paragraph style is that choosing a character style only affects the selected text, while choosing a paragraph style changes all of the text in the paragraph containing the selection to that style.

5. Line and page breaks

Have you ever inserted a table or a graphic into a Word document only to find that a new page starts right after the heading but before the table or graphic? The natural thing to do in this situation is to insert an extra carriage return or a page break right before the heading to ensure that both the heading and the table/graphic appear on the same page, right? But what happens when, later on, you’re making changes and add a new paragraph before the table heading? Now there is too much space between the table header and the paragraph that precedes it. And before you can finalize your document, you have to visually inspect and manually edit every page of your document to make sure that there are no other instances of the same problem.

There is an easy, styles-based solution to this problem. First, define a new style for table/graphic headers (or use the automatic style, described in part 6). Then, in the Modify Style dialog, select Format > Paragraph and click on the Line and Page Breaks tab. Check the box marked keep with next. Now, if Word encounters a situation where the paragraph/table/image/etc. after the header is going to start on a new page, it will make sure that the header is also on the new page. This also works for section headers.

Also, in the Line and Page Breaks tab, note the Page break before option. Selecting this option will ensure that elements of the current style will begin a new page. This is useful if, for example, you want new Heading 1 elements to always start a new page.

6. Captions and cross references

Once again, imagine that you are creating a document with many tables. The low-tech way to number your tables is to hard-code the table numbers into their headers (”Table 1: Blah…”) and refer to tables using those manually assigned numbers (”Table 1 shows that…”). But what happens if, halfway through your document, you realize that you need a new table between Table 14 and Table 15. Now you have to renumber every table after Table 15. This could take a long time if you have, say, 50 tables, and the likelihood that you’ll miss an in-text reference is fairly high.

Word has an automatic table/figure numbering feature, however. Instead of manually creating a table header with a number, you could select the entire table, right click, and choose Caption. This will open a dialog that allows you to automatically insert a “Label-Number” caption, with control over the numbering format (”Table 1.3.4,” or “Figure 5″). The caption will automatically have Word’s built Caption style, which you can edit using the standard Modify Style dialog from the Styles pane. You can also choose whether the caption will be placed above or below the table or figure.

But what about those in-text references to tables and figures. Easy. Choose Insert > Cross reference and select the table or figure that you want to refer to. Word will automatically insert the text “Table X” (or “Figure X”) where X is the automatically generated number of the table or figure. You can even have Word insert the entire caption, if desired. The cross reference feature also works for numbered items, footnotes, endnotes, etc., and can save a lot of time for writers creating long documents.

Tip: Sometimes the fields that display the automatic references become incorrect when new tables, figures, footnotes, etc. are inserted. Although the document will print correctly, the on-screen display will be off. To force Word to update all of these cross-reference fields, click on Print Preview, then close preview to view your document.

7. Turn off auto formatting

A common complaint about Word is its tendency to automatically create numbered and bulleted lists. Sometimes users want to manually make a numbered list, or begin a sentence with the “-” character without beginning a new bulleted list that uses that character. Such formatting is very easy to turn off. Select Tools > AutoCorrect > AutoFormat to turn off/on automatic lists, smart quotes, character-based formatting, fractions, etc.

8. Character-based formatting

But before you go rushing to the Tools menu to turn off all of the automatic formatting, consider taking a moment to learn how such formatting works. If you understand it, it could save you a lot of time. For example, by typing text surrounded by asterisks (*like this*), you can have Word make your text bold. Or, by surrounding text with the underscore character, you can have work make the selected text italic. When you don’t have to take your fingers off the keyboard to apply formatting changes, you can work quite a bit faster.

9. Continue previous list

The main reason why people want to turn off automatic numbered lists is to insert indented or other text between list items:

MacList.png

This can be difficult using the automatic numbered (or bulleted) list feature because Word’s default behavior is to start item number 9 after you hit return, or to begin item 8a after you hit tab. However, by creating a list using the automatic numbering function, turning the list off after the desired list item, inserting whatever needs to go before the next item, then inserting a new list, and choosing Format > Bullets and numbering:

MacContinue.png

and selecting Continue previous list, you can force the list to start at the number of the item that you left off, +1.

10. Keyboard shortcuts

Finally, a few keyboard shortcuts that might save you some time:

  • Cmd+T: Hanging indent (hit Cmd+T again to increase the hanging indent; hit Cmd+Shift+T to decrease/remove the hanging indent)
  • Cmd+=: Subscript (hit Cmd+= again to revert to the normal font)
  • Cmd+Shift+=: Superscript
  • Cmd+Shift+L: Start a bulleted list (this can also be accomplished by starting the sentence with an asterisk, assuming that this hasn’t been turned off using the AutoCorrect menu)

Note: Replace Cmd with Cntrl for Windows.

Notes

  • Most of these screenshots show Word running on Mac OS X, but the interfaces are very similar for recent versions of Word for Windows.
  • I know, this is long, but it had to be written. I’m tired of having to collaborate with other authors that are still in the word processing Stone Age. These tips take about 5 minutes to learn, and they’ll save hours over the years.
  • Word is far from the only system that offers these features. Similar functionality can be found in most word processing systems, including OpenOffice.org. If you really like logical markup and automatic formatting, consider learning LaTeX. I chose to write about word because (a) Word is the most common word-processing application and (b) it’s actually a pretty polished product.

Free software toolkit

Open-source or just plain free applications to cover 95% of your computing needs. For Mac OS X and Windows users. Disclaimer: These are the applications that I use and prefer, but alternatives exist.

Basics
Browser: Firefox
Office: NeoOffice (Mac), OpenOffice (Win)
Text editing: TextWrangler (Mac), Crimson Editor (Win)
HTML Editing: Nvu
Image Editing: Seashore (Mac), Gimp (Win)
FTP: Cyberduck (Mac), CoreFTP (Win)
Antivirus: ClamXav (Mac), Avast (Win)
Compression: DropCompress (Mac), 7-Zip (Win)

OS-Specific
OS X system optimizer: OnyX
OS X app and file launcher: Quicksilver
Windows PDF creation: PDF Creator
Windows spyware remover: AdAware

Academic and research
Statistics: R
LaTeX engine and frontend: MacTeX (Mac), ProTeXt (Win)

Lots more
These are just the basics. For lots more, see my Nifty OS X apps directory page, which has links to many other applications, and links to more links to more applications.

Nifty OS X apps list

These are free applications that I have tried and liked. This list is updated as needed.

Last update: 19 June 06

Key:

X11: requires Apple's X11
OSS: Open Source Software
Ltd: Limited version of a commercial app

Word processors and text editors

  • NeoOffice - Native OS X version of OpenOffice.org (word processor, spreadsheet program, drawing program, presentation program and equation editor) [OSS]
  • OpenOffice.org - OpenOffice.org for OS X (not a native OS X application) [OSS, X11]
  • VooDooPad Lite - A notepad application that supports multiple linked pages within a document and has outlining feautres. Exports to DOC and RTFD [Ltd]
  • TextWrangler - The lite version of BBEdit, a very advanced editor [Ltd]
  • Smultron - A simple, powerful editor [OSS]
  • Aquamacs - An Aqua version of the classic and extensible Emacs text editor [OSS]

Utilities

  • ClamXav - A free virus scanner [OSS]
  • CominePDFs - Merge pdf files into one large document [OSS]
  • PDFLab - Merge and combine pdf files
  • DesInstaller - Reads package receipts and removes every file installed by a pkg file
  • DropCompress - Drag and drop to zip or tar.gz files [OSS]
  • YemuZip - Create zip files
  • UnRarX - Rar extractor
  • OnyX - An OS X system preferences adjustor and system optimizer
  • Quicksilver - An advanced file and application launcher with many plugins
  • Butler - A file, application, etc. launcher
  • Fink - Tools for installing Linux applications (apt-get style) [OSS, some require x11]
  • Himmelbar - An application launcher
  • Tigerlaunch - An application launcher
  • iScroll - Adds two-finger scrolling to pre-2005 iBooks and PowerBooks [OSS]

Web and graphics

  • Firefox and Thunderbird - Duh [OSS]
  • Camino - The OS X native browser from Mozilla with slightly better OS X integration [OSS]
  • Opera - Another browser with advanced features
  • Shiira - A native browser based on Web Kit, has an integrated RSS reader [OSS]
  • Shrook - A feature-packed RSS reader
  • NetNewsWire Lite - An RSS reader [Ltd]
  • Vienna - An open-source RSS reader [OSS]
  • Nvu - Graphical XHTML and CSS editor (creates standards-compliant XHTML, based on Mozilla composer) [OSS]
  • Taco - An XHTML editor
  • Bleezer - Desktop blog editor
  • Qumana - Desktop blog editor
  • Cyberduck - Graphical FTP client [OSS]
  • OneButton FTP - FTP client [OSS]
  • Seashore - A native Mac OS X image editor, based on Gimp [OSS]
  • Gimp.app - Gnu Image Manipulation Program for OS X [OSS, X11]. Also see: GIMPshop, the same code, but with Photoshop style menues [OSS, X11]
  • ImageWell - Basic image editor
  • Inkscape - A vector drawing app [OSS, X11]
  • Google Earth - Satellite imaging tool

Academic and research

  • R - The statistical programming environment based on the S language [OSS]
  • Gretl - Gnu Regression, Econometrics and Time Series Library, a graphical econometrics and statistics application [OSS, X11]
  • MacTeX - A packaged LaTeX distribution for OS X that includes gwTeX, TeXShop, BibDesk, i-Installer, additional fonts and other utilities [OSS]
  • i-Installer/gwTeX - The i-Installer application, which allows easily downloading and installing gwTeX and other LaTeX utilities for the Mac [OSS]
  • TeXShop - Frontend for LaTeX on OS X [OSS]
  • iTeXMac - Another LaTeX frontend for OX S [OSS]
  • TeXMaker - Another LaTeX frontend for OS X [OSS]
  • LyX - A graphical (what you see is what you mean) frontend to LaTeX (works with gwTeX/MacTeX or teTeX via Fink) [OSS]
  • BibDesk - A graphical BibTeX database manager [OSS]
  • Plot - A native 2D plotting utility

Music

  • Finale Notepad - Simple sheet music composition [Ltd]
  • Senuti - An application that allows you to download music from your iPod to your Mac [OSS]
  • Audacity - An audio recording/editing tool [OSS]

Resources for more

Next Page »