LaTeX

From Devpit
Jump to: navigation, search

LaTeX has enough stuff to remember that this quick reference is nice if you haven't used it enough to commit it all to memory.


Quick example

It's convenient to start with a template set of files containing, for example, Makefile, MAIN.bib, MAIN.tex, title.tex, and body.tex.

% Percent makes a comment to the end of the line.
% Pick just one of the document classes below
\documentclass{article}  % other options include book, report, thesis
%\documentclass[journal]{IEEEtran}  % requires IEEEtran.cls
%\documentclass{book}
%\documentclass{thesis}

% These packages are often useful.  Look up what they do for more information.
\usepackage{moreverb}
\usepackage{url}
\usepackage{graphicx}

\begin{document}  % This ends the preamble and starts the contents

\input{title}  % Includes title.tex
\input{body}  % Includes body.tex

\clearpage
\bibliographystyle{plain}
\bibliography{MAIN}  % Name of the bibliography file -- you can repeat this to combine files.  Only cited references are shown.


\end{document}

A bibliography file is named with a ".bib" extension. In the example, "MAIN.bib" might contain:

@book{lamport1994,
        author = "Leslie Lamport",
        title = "LaTeX: A Document Preparation System",
        edition = "Second",
        year = "1994",
        publisher = "Addison-Wesley",
        address = "Reading, Massachusetts"
}

A template title.tex might contain:

\title{Click to add title}
\author{Joe Foo Bar\\Department of Computer Science\\University of Something\\Box xxxx\\Smallville, ZZ\\\texttt{joe@example.com}}
\date{\today}
\maketitle

\begin{center}
  \LaTeX is as easy as $\pi$. \cite{lamport1994}  % \cite adds a [1] to refer to the bibliography
\end{center}

The bulk of the text is, of course, in body.tex. If it's a long document like a research paper, consider breaking it up into a file for each section.

The site http://theoval.cmp.uea.ac.uk/~nlct/latex/thesis/node11.html provides some in-depth bibliography examples.

Compiling

Perhaps the most convenient package to install on your computer is teTeX. It's a distribution of LaTeX with a bunch of useful tools.

To compile the document, LaTeX must be run multiple times to resolve back-references and do other things. Remove the output files first since existing output files are used for hints.

Compile your document as follows if you have a bibliography:

rm -f MAIN.aux MAIN.dvi MAIN.log MAIN.bbl MAIN.blg MAIN.out
latex MAIN
bibtex MAIN  # Notice this is the name of the main source file, not the bibliography file.
latex MAIN
latex MAIN

If you don't have a bibliography:

rm -f MAIN.aux MAIN.dvi MAIN.log MAIN.bbl MAIN.blg MAIN.out
latex MAIN
latex MAIN

A Makefile is usually a good approach:

MAIN.dvi: Makefile *.tex *.bib
        make clean
        latex MAIN
        bibtex MAIN
        latex MAIN
        @echo
        @echo
        @echo == Final pass -- ignore errors above ==
        @echo
        latex MAIN

MAIN.ps: MAIN.dvi
        dvips MAIN

MAIN.pdf: MAIN.dvi
        dvipdfm MAIN

clean:
        rm -f MAIN.aux MAIN.dvi MAIN.log MAIN.bbl MAIN.blg MAIN.out

Also, you can use the command pdflatex instead of latex if you're making a pdf. There are some real differences between using pdflatex instead of latex followed by dvipdf, but for basic use they produce the same result.

A few things to remember

  • If something seems harder than it needs to be, google. LaTeX has been around a really long time and has been used to write many really long texts, so someone probably has a better way.
  • Quotes are written with ` and ' for single-quotes and `` and '' for double-quotes (that's two singles, not a double).
  • The package upquote can be used to get true verbatim quotes.
  • Non-breaking space: ~ (tilde)
  • To quote a URL, add "\usepackage{url}" to your preamble, then use "\url{http://devpit.org/}"
  • To quote a literal string: "\verb+backslash, tilde, brackets, braces, etc are taken literally here.+" This also prevents hyphenation. Certain characters can be substituted for the plus signs, such as underline, hyphen, single quotes, double quotes, back-quotes, pipe, tilde, parens, curly braces, etc. Note that parens are probably a bad choice since you have to use the same one on both sides rather than opposites. That is, "\verb(text(". This is not an ideal way to quote a URL.
  • "\emph{this text is italicized}"
  • To prevent an indent, add "\noindent" before a paragraph.
  • To end a page, use "\newpage", but if you want to emit any pending floating figures, use "\clearpage" instead. The latter is better for ending a section, such as before the bibliography or after the title page.
  • To construct your document from multiple files, use "\insert{basename}" to include the contents of "basename.tex".
  • To insert verbatim text: (All of these insert exact contents, including special characters. All but the first require "\usepackage{moreverb}" in the preamble.)
    • "\begin{verbatim}" and "\begin{verbatim}" work okay, but they screw up tabs.
    • "\begin{verbatimtab}[8]" and "\end{verbatimtab}" keeps tabs intact.
    • "\verbatiminput{external_file.txt}" will insert an external file, but screw up tabs.
    • "\verbatimtabinput[8]{external_file.txt}" will insert an external file and keep tabs intact. This one is good for inserting source files.

Embedding Source Code

Clearly "\verbatimtabinput" and the verbatimtab environment are the way to deal with tabs and such in source files, but it's even better to abstract out any formatting you want to use for your source code. To do this, put this in your preamble:

\def\sourcetabsize{4}
\newenvironment{sourcestyle}{\begin{scriptsize}}{\end{scriptsize}}
\def\sourceinput#1{\par\begin{sourcestyle}\verbatimtabinput[\sourcetabsize]{#1}\end{sourcestyle}\par}

Then to use it, you can embed a raw source file as follows:

\sourceinput{embedded.c}

Or you can write literal source code in your document as follows: (The % comments out the whitespace.)

\begin{sourcestyle}%
\begin{verbatimtab}[\sourcetabsize]
Source code goes here
\end{verbatimtab}%
\end{sourcestyle}

This way, if you can easily change, for example, the font and tab size of your source code. All you have to do is tweak "\sourcetabsize" and the "sourcestyle" environment.

XXX: Note: It is better to declare another environment named "source" that does something like this for literal source, but this does not work:

\newenvironment{source}{
    \par
    \begin{sourcestyle}
    \begin{verbatimtab}[\sourcetabsize]
}{
    \end{verbatimtab}
    \end{sourcestyle}
    \par
}

IEEE Format

To use IEEE's standard 10pt 2-column format, download IEEEtran.tar.gz from IEEE's site. Drop IEEEtran.cls in your document's directory, and set your documentclass as follows:

\documentclass[journal]{IEEEtran}

The other files in the tar describe the various features of this class, but this is all you need to get started quickly.

See Also