Tech‎ > ‎

How to Do This and That with LATEX

(Thanks to http://heather.cs.ucdavis.edu/~matloff/LaTeX/LookHereFirst.html)


Don't know how to do something in LATEX ? Look here first. Below is an index of various LATEX constructs.

In order to use this index, just look for an English term (either manually scroll through in alphabetical order, or use your browser's Find facility) which describes what you want to do.

bar (math):

    To get, say, "Y bar", use \bar{Y}. However, the bar will be rather short. To get a wider one, use \overline. For example,

       \overline{(\bar{A}+\bar{C}+\bar{D})(\bar{B}+\bar{E})} 
    
    will generate
       ____________
        _ _ _  _ _
       (A+C+D)(B+E)
    

bibliographies:

    There are many ways to do this in LATEX . Here is an example of one way. The bibliography section, at the end of the document, might look like this:
       \begin{thebibliography}{}
       
       \bibitem{anderson}
       T. Anderson {\it et al}.  ``A Case for Networks of Workstations,''
       {\it IEEE Micro}, 54-64, February 1995.
       
       \bibitem{mpi}
       M. Snir.  {\it MPI:  the Complete Reference}, MIT Press, 1996.
       
       \bibitem{trivedi}
       K.S. Trivedi, {\it Probability and Statistics with Reliability,
       Queuing and Computer Science Applications}.  Prentice-Hall, 1982.
       
       \end{thebibliography}
    
    Here is an example of references to bibliography items:
       In the last few years there has been a tremendous growth in interest
       in the use of networks of workstations (NOWs) as engines forparallel.
       processing \cite{anderson}.  Among the most important advantages of NOWs
       is their economic feasibility; they are constructed from off-the-shelf,
       ``commodity'' components; new nodes can be added very easily; and good
       infrastructure software is available, such as \cite{mpi}.
    
    To cite the number of a specific page, section or whatever, precede the citation name with the desired information in brackets, e.g.
    Durrett \cite[page 76]{durrett} references a model due to Chung and Lu,
    

    There are variations, e.g. for using the author-year format like Smith (2001).

    Many research publications want you to use BibTeX, which better organizes the whole process. Suppose for concreteness your source file is x.tex. Basically, you create a file x.bib containing the bibliography, and run bibtex on that file:

    bibtex x
    
    (Note that the .bib suffix is not there.) Then one runs pdflatex or latex on x.tex as usual. You may need to run these commands more than once, so that all the cross-references are resolved properly.

    Here is a Makefile for a file CL.tex:

    CL.pdf CL.aux:  CL.tex CL.bbl
            pdflatex CL.tex
    
    CL.bbl:  CL.bib 
            bibtex CL
    

    Place the line

    \bibliography{x}
    
    in x.tex where you would have placed the bibliography. As to the format of the file x.bib, there are many examples on the Web, but here are a couple:
    @inproceedings{cozman,
       author = "F. Cozman and I. Cohen",
       title = "Unlabeled data can degrade classification performance of
       generative classifiers",
       booktitle = "Proceedings of the Fifteenth International Florida
       Artificial Intelligence Research Society Conference",
       year = "2002",
       pages = "327--331"
    }
    
    @inbook{hall,
       author = "Alistair Hall",
       title = "Generalized Method of Moments",
       publisher = "Oxford University Press",
       pages = "31",
       year = "2005"
    }
    
    As you can see, there are various categories for conference proceedings, books and so on. Moreover, many research sites already set these BibTeX entries up for you, very convenient.

binomial coefficients:

    To get the familiar "n choose k" notation, the AMS math package does have a \binom{n}{k} capability. To use the package, you must include it, by placing a line
       \usepackage{amsmath}
    
    near the beginning of your file. But then \cases must be changed if one has that too. So, I think it's best just to use arrays, e.g.
       The notation $ \left (
       \begin{array}{c}
       8 \\
       3
       \end{array}
       \right )$
       means ``n choose k.''
    

blank space:

    Suppose you wish to put a single-character blank in your document, say a blank entry in a table. Use `\ ', i.e. a backslash followed by a blank space. This also works to suppress extra space after the period after an abbreviation, i.e. to tell LATEX not to consider this the end of a sentence.

    Use \hspace to insert a variable amount of blank space.

boldface (and other) math fonts:
    Suppose for example that you want to put "x-squared" in bold face. Here's how to do it if you are NOT in math mode already:

       {\boldmath $x^2$}
    

    If you are already in math mode, use \mathbf, etc.

braces:

    If you want { or } to appear in your text, you need to escape them, i.e. using \{ and \}.

cursive script fonts (mathematical):

In math mode, to get cursive script, use \mathcal, e.g.

$\mathcal A$

for a curse ("calligraphic") capital A.

cases (mathematical):

    Here is how one could express more symbolically the statement, "1 sub Q (t) is equal to 1 or 0, according to whether t is a member of the set Q":

       1_Q(t) =
       \cases{
          1, & if $t \in Q$\cr
          0, & if $t \notin Q$\cr
       }
    
    

    If you are using the amsmath package, then change the above to

       1_Q(t) =
       \begin{cases}
          1, & \text{if $t \in Q$}\\
          0, & \text{if $t \notin Q$}
       \end{cases}
    

color:

    Include a line like

       \usepackage{color}
    

    Then for example

       \color{blue}
    

    will make all text (and figures, etc.) from here onward blue. There is also \pagecolor for background color, \textcolor for a one-time text-color change, etc.

    For example, to get the sentence "Today is Monday" in red, write

    \textcolor{red}{Today is Monday.}
    

    Several primary colors are predefined (red, green, blue, cyan, magenta, yellow, black and white), and you can use \definecolor to make use of X11 windows colors, "mix" your own colors from the primaries, etc.

    Note that the colors may not show up with xdvi but will show up in the Postscript or PDF files.

date ("article" style):

    In the title section, if you wish to suppress the date, use \date{}.

derivatives (math)

    Ordinary derivatives just make use of fractions, e.g.
    \frac{du}{dt}
    
    For partial derivatives, use the \partial construct, e.g.
    \frac{\partial u}{\partial t}
    
    For the vector of partial derivatives ("del"), use \nabla. In evaluating a derivative at a certain value, using the "vertical bar" notation, do as in this example:
       \frac{dx}{dt} \Big |_{t=1}
    
double-spacing:

    To double space the entire document, put this in the preamble:

    \renewcommand{\baselinestretch}{2}
    

"dropped" (occupies several lines of text, as in an old-fashioned novel) initial capital letter in a paragraph:

    Use the package dropping.sty, available at CTAN. Include a \usepackage{dropping} line. For example,

       \dropping{3}{O} nce upon a time, there was...
    

    will produce a large `O' which extends through the first 3 lines of the paragraph.

enlarging the font size:

    This of course can be done through the documentclass command, but if you wish to just temporarily enlarge the font size, use \large, \Large, \LARGE and so on. For example:
       Do {\LARGE NOT} wait until you are finished writing your program to add
       comments to it.
    

equations (math):

    For displaying ordinary equations, surround the equation text by

       \begin{equation}
       \end{equation}
    

    The resulting equation will be numbered. If you don't want numbering, use eqnarray and \nonumber.

Note carefully: Do not leave blank lines in the equation environment, as it will cause an error.

escaping special characters:

    Place \ before the characters _, &, {, # when they occur in text. If you need \ itself, use $\backslash$.

floor, ceiling (math):

    Use \lfloor, \rfloor, \lceil, \rceil.
hat (math):

    Use \hat, or \widehat.

hyperlinks:

You can get hyperlinks within your document--in the table of contents, equation references etc., as well as to external Web links--by including a line

   \usepackage{hyperref}

near the beginning of your document. By the way, if you use \tableofcontents make sure to use it in that form instead of \tableofcontents{}.

implication arrow:
    Use $A \Rightarrow B$ for "A implies B."
including graphics files:

    Suppose you have an PDF file. x.pdf. (JPEG is OK too.) Then at the beginning of your document, tell LATEX to use the graphicx package:

       \usepackage{graphicx}
    

    At the place in the document at which you wish to insert your figure, type, for example:

       \begin{figure}[tb]
       \centerline{
          \includegraphics[width=3.2in]{x.pdf}
       }
       \caption{your caption}  % optional
       \label{label}  % optional
       \end{figure}
    

    Note the width option for \includegraphics. The latter command has various other options, including angle for rotation of the image, e.g. angle 180 for a reversal.

    Run pdflatex instead of latex.

infinity (math symbol):

    Use \infty in math mode.

integrals (math):

    Use \int, with the subscript/superscript symbols _ and ^ for the lower and upper limits. For example, to get the integral of x(t) from 0 to T, use

       \int_0^T x(t) dt
    

"landscape" (as opposed to "portrait") printing:

    This means that in order to read the sheet of paper printed this way, you must rotate it 90 degrees from normal; the long edges of the sheet will go from left to right, and the short edges from top to bottom.

    The LATEX  command is

       \usepackage{landscape}
    

    Margins which are stated to be horizontal (\oddsidemargin and \evensidemargin) will now be vertical and vice versa. However, \textwidth and \textheight still refer to the lines of text themselves.

letterhead, skipping over:

    Say you wish to print on, e.g., UC letterhead paper. You need to add vertical space on the first page but not subsequent pages. Use the \vskip command, but make sure to use "\ \ " first, since the former command only takes effect between paragraphs. Click here for an example.

limits (math):

    To get a nice-looking limit, use \lim, e.g.

    \lim_{n \rightarrow \infty} z_n
    
limits on integrals, sums etc. (math):

    Limits on an integral, sum and so on can be produced by using ordinary sub- and superscript operations. This looks good for inline math within text, but for displayed expressions the \limits operator can be used to place limits directly above and below the integral, instead of to the "northeast" and "southeast." For example, to write the integral from 0 to 1 in this manner,

       \int\limits_0^1
    
line break:

    Just use "\" to force LATEX  to start a new line. Click here for an example.
long equations:

    If an equation is too long to fit on a line, you can try using eqnarray to break it up. Or use the amsmath package's split environment, as follows:

       \begin{split}
       first part of long equation \\
       second part
       \end{split}
    

math symbols:

matrices (math):

    For example, to write the 2x2 identity matrix in display form:

       \begin{equation}
       \begin{pmatrix}
       1 & 0 \\
       0 & 1
       \end{pmatrix}
       \end{equation}
    
    

    There are lots of variations.

    This must be accompanied by a line

       \usepackage{amsmath}
    

    before your \begin{document}.

    If you don't use that package, just use arrays:

       \begin{equation}
       \left (
       \begin{array}{cc}
       1 & 0 \\
       0 & 1
       \end{array}
       \right )
       \end{equation}
    

min, max, log etc. (math)

    To get nicer-looking min and max expressions, use \min and \max. For example,
       \max_i x_i
    
    will result in the i going underneath "max" instead of to the side.
the multline and eqnarry environments (math):

    One choice would be to use the multline environment from the amslatex math package. To call upon the latter, put a line

       \usepackage{amsmath}
    

    before your \begin{document}. Here is an example of use of the environment:

       \begin{multline} 
       q(v) = q(v_{1},...,v_{d-1}) \\
       = P(Y = 1 | X^{(j)} = v_{1},...,X^{(d-1)} = v_{d-1})
       \end{multline} 
    

    Note the \\ signal to go to a new line. If you want the equal signs to be aligned, a nicer choice is eqnarray, e.g.

       \begin{eqnarray} 
       q(v) & =&  q(v_{1},...,v_{d-1}) \\
       & = &  P(Y = 1 | X^{(j)} = v_{1},...,X^{(d-1)} = v_{d-1})
       \end{eqnarray} 
    

    The ampersands are used here to align the equal signs. And they need not be equal signs; anything between the ampersands--including a blank--will line up with the others. If you want, say, only the last line to be numbered:

       \begin{eqnarray} 
       q(v) & =&  q(v_{1},...,v_{d-1}) \nonumber \\
       & = &  P(Y = 1 | X^{(j)} = v_{1},...,X^{(d-1)} = v_{d-1})
       \end{eqnarray} 
    

    LATEX  may place the equal sign in the above example further to the right than you wish. If so, try \lefteqn{}:

       \begin{eqnarray} 
       \lefteqn{q(v) & =&  q(v_{1},...,v_{d-1})} \nonumber \\
       & = &  P(Y = 1 | X^{(j)} = v_{1},...,X^{(d-1)} = v_{d-1})
       \end{eqnarray} 
    

norm, vector (math):

    To get the norm of a vector, use \parallel. For example, the norm of u+v is
       \parallel u+v \parallel
    
"not equals" sign (math):

    Use \neq in math mode.

page numbering:

    If you wish to have no page numbers, use \pagestyle{empty}. However, there will still be a page number generated by commands such as \maketitle and \chapter, so if you don't want any page numbers at all, then follow any such command by \thispagestyle{empty} in addition to your original \pagestyle{empty}.

paragraph indentation:

    You can specify no indentation for the entire document by the command \setlength{\parindent}{0in}. Also, you can suppress indentation for a given paragraph via the command {\noindent}.

plus-or-minus (math symbol):

    Use \pm.

quotation marks:

    Do not use the ASCII quotation mark character for quotes. Use `` for opening quotations and '' for closing them. (These are pairs of left single-quote and right single-quote characters.)

    If you have a quote within a quote, so that the latter needs the single-quote marks, use ``\,` to open and '\,'' to close.

references:

    You can make a references to almost anything--a chapter, section, equation, figure or page--by using \label. For example,
    \section{XYZ}
    \label{sectionxyz}
    
    sets up use of something like
    ...in Section \ref{sectionxyz}
    
    Use \pageref to refer to page numbers.

Note: I've found that in some distributions, I occasionally get "undefined reference" error messages, even though the reference is there. I've found that I can fix this by removing the .aux and .out files.

rotating text, including making it upside-down:

    In document preamble, have

    \usepackage[dvips]{graphics}
    

    Then for the rotated text, have:

    \rotatebox{angle, in degrees counterclockwise}{text to be rotated}
    

    (The effect will show up only after use dvips, not xdvi.)

sections, subsections etc.:

    If you want subsection depths at more than the default value (note that if you have chapters, that will mean one few level of subsections), include a line like
    \setcounter{secnumdepth}{4}
    
    If you want the same depth in your table of contents, write
    \setcounter{tocdepth}{4}
    
slides:

    You can get a PowerPoint-like slide-show presentation from LATEX  by using Prosper or Beamer.

.sty files:

    These are stored, for example, in /usr/share/texmf/tex/latex/ or /usr/share/texmf-texlive/tex/latex/ if you want to see what you have.

    If you need to use a style file, say x.sty, include a line

    \usepackage{x}
    

    in your LATEX source file. If it is not already part of your installed LATEX system, you will need to add its directory, say /u/v/w (full path name), to the search path:

    setenv TEXINPUTS .:/u/v/w:
    

    (The final colon is crucial.)

subscripts, superscripts (math):

    Use ^ for superscripts, _ for subscripts. Any sub- or superscript consisting of more than one character must be enclosed by braces. E.g.

       {(x_{ij})}^2
    

    will produce the square of x with a subscript ij.

sums (math):

tabbing:

    Suppose, for example, that you want to display some pseudocode for an algorithm, and suppose you need some math notation in it. The latter condition would mean you can't use \verbatim. But \tabbing would work. Here's an example:

       \begin{tabbing}
       set $F_1$ to the set of 1-item itemsets whose support exceeds the
       threshold \\
       for \=  i = 2 to b \\
         \> $F_{i} = \phi$ \\
         \> for \= each I in $F_{i-1}$ \\
         \> \> for \= each K in $F_1$ \\
         \> \> \> $Q = I \cup K$ \\
         \> \> \> if \= support(Q) exceeds support threshold \\
         \> \> \> \> add Q to $F_{i}$ \\
         \> if $F_{i}$ is empty break \\
       return $\cup_i F_i$
       \end{tabbing}
    

    Each \= establishes a tab mark, and each \> means to move to the next tab position. The \\ mean to start a new line.

table of contents:

    Use \tableofcontents. Don't forget that you then must run latex two or even more times.
tables:

    Use \tabular. For example, with lines:
       \begin{tabular}{|r|r|}
       \hline
       ~ & 1011 \\ \hline
       AND & 1101 \\ \hline
       \hline
       ~ & 1101 \\ \hline
       \end{tabular}
    

    Without vertical lines:

       \begin{tabular}{rr}
       ~ & 1011 \\
       AND & 1101 \\
       \hline
       ~ & 1001 \\
       \end{tabular}
    

    Without vertical or horizontal lines:

       \begin{tabular}{rr}
       ~ & 1011 \\
       AND & 1101 \\
       ~ & 1001 \\
       \end{tabular}
    

    With a wide column at the top:

       \begin{tabular}{|r|r|}
       \hline
       \multicolumn{2}{|c|}{spans the full width of the table} \\ \hline
       ~ & 1011 \\ \hline
       AND & 1101 \\ \hline
       \hline
       ~ & 1101 \\ \hline
       \end{tabular}
    

    Note: If a table entry should be blank, just put in a tile (~).

theorems, etc.

    The amsmath package has Theorem etc. environments.

    Many people redefine those environments. For example, the Journal of Machine Learning Research style file does this:

    \newcommand{\BlackBox}{\rule{1.5ex}{1.5ex}}  % end of proof
    \newenvironment{proof}{\par\noindent{\bf Proof\
    }}{\hfill\BlackBox\\[2mm]}
    \newtheorem{example}{Example}
    \newtheorem{theorem}{Theorem}
    \newtheorem{lemma}[theorem]{Lemma}
    \newtheorem{proposition}[theorem]{Proposition}
    \newtheorem{remark}[theorem]{Remark}
    \newtheorem{corollary}[theorem]{Corollary}
    \newtheorem{definition}[theorem]{Definition}
    \newtheorem{conjecture}[theorem]{Conjecture}
    \newtheorem{axiom}[theorem]{Axiom}
    
tilde (math):

    To get a tilde by itself, do \ensuremath{\sim} To get, say, "Y tilde", use \tilde{Y}.

title page ("article" style):

    Use \title, \author (with \and if multiple authors), \date and \maketitle.

union, intersection, subset, superset (math):

    Use \cup, \cap, \subset, \superset. Also, U \setminus V refers to those elements of U that are not in V.