LaTeX
From charlesreid1
Contents
Installation
Yosemite (10.10)
I still recommend installing MacTeX from here: http://www.tug.org/mactex/
Mac Leopard (10.5) and Snow Leopard (10.6)
For Macs, I recommend installing MacTeX from here: http://www.tug.org/mactex/
Problems with pdftex?
There may be a conflict with the version of pdftex installed by Fink and the version installed by MacTeX. In this case, I recommend one of the following options:
1. Define your $PATH
variable (see Dot files) to point to the MacTeX version of pdflatex (located in /usr/texbin/pdftex
).
Fink may be putting /sw
(or whatever your Fink prefix is) at the FRONT of your path, by default, if you are sourcing the file /sw/bin/init.sh
or /sw/bin/pathsetup.sh
or something similar. If this is the case, remove the line sourcing these from your dot file, and just add /sw
to your path manually.
2. Don't call pdftex
from the command line without specifying which pdftex, e.g. don't run
$ pdftex [...]
but instead, run
$ /usr/texbin/pdftex [...]
Finding TeX-Related Directories
To install files, first you'll need to know where your TeX-related directories (which all live under the texmf/
directory) are located.
If you aren't sure, just run the texhash
command, which will update the TeX database with the contents of the texmf directory. You should see where your texmf directories are.
Alternatively, you can make your own, and pass the location of your custom texmf directory to the texhash command.
The following directory schema is Mac-specific but it is how we organize our user tex/latex/lyx files:
You can put your TeX-related files in ~/Library/texmf
or in ~/Library/texmf/tex
You can put your LaTeX-related files in ~/Library/texmf/tex/latex
You can put your LyX-related files in ~/Library/texmf/tex/latex/lyx
You can put your BibTeX-related files in ~/Library/texmf/bibtex/bib
You can put your .bst (bib style files) in ~/Library/texmf/bibtex/bst
NOTE: If any of these directories don't exist, you'll need to create them. Most TeX or LaTeX installers won't create these directories for you. Just do mkdir -p ~/Library/texmf/tex/latex
and you're on your way.
SECOND NOTE: When you put a new file or folder into the texmf directory, you should do a rehash to update the TeX database to include the files in this texmf directory with the list of available files:
texhash ~/Library/texmf
Windows
use MikTeX: http://miktex.org/
LaTeX Overview
LaTeX is a document typesetting system used primarily for scientific and mathematical documents. Latex consists of Tex source code, which is typeset and output into one of a number of different formats (PDF, DV, PS, etc.)
The following LaTeX code is a selection from Batchelor's classic Introduction to Fluid Dynamics:
\documentclass[12pt]{article}
\usepackage{amsmath}
\title{\LaTeX Example}
\date{}
\begin{document}
\maketitle
The function $\boldsymbol{u}(\boldsymbol{x},t)$ will thus be the primary
dependent variable in our analysis, and other flow quantities such as
pressure will likewise be regarded as being functions of $\boldsymbol{x}$ and $t$.
When $\boldsymbol{u}$ is independent of $t$, the flow is said to be \emph{steady}.
A line in the fluid whose tangent is everywhere parallel to $\boldsymbol{u}$ instantaneously
is a line of flow, or a \emph{streamline}; the family of streamlines at time $t$ are solutions of
\begin{equation}
\frac{dx}{ u(\boldsymbol{x},t) } = \frac{ dy }{ v(\boldsymbol{x}, t) } = \frac{ dz }{ w(\boldsymbol{x}, t) },
\end{equation}
where $u,v,w$ are the components of $\boldsymbol{u}$ parallel to rectilinear
axes and $x,y,z$ are the components of $\boldsymbol{x}$. When the flow is steady,
the streamlines have the same form at all times. A related concept is a \emph{stream-tube},
which is the surface formed instantaneously by all the streamlines that pass through a given closed
curve in the fluid.
\end{document}
and, when typeset, it generates the following text as output:
Latex Symbols
Some useful symbols:
Permutations
Latex:
a = \bigl(\begin{smallmatrix} 1 & 2 & 3 & \cdots & n-1 & n \\ 2 & 3 & 4 & \cdots & n & 1 \end{smallmatrix}\bigr)
Math:
Latex:
\alpha \top \beta
Math:
Latex:
\binom{a}{b}
Math:
Latex Symbols
Some useful symbols:
Permutations
a = \bigl(\begin{smallmatrix} 1 & 2 & 3 & \cdots & n-1 & n \\ 2 & 3 & 4 & \cdots & n & 1 \end{smallmatrix}\bigr)
results in:
\alpha \top \beta
results in:
\binom{a}{b}
results in:
Parts of a Latex Document
The following barebones LaTeX document will be used as a starting point for discussing different parts of a LaTeX document:
\documentclass[english]{article}
\begin{document}
This is a basic latex document.
\end{document}
Preamble
The preamble is the portion that comes before the \begin{document}
. It first specifies the class of the document. This must be specified because LaTeX needs to know what settings to use for things like margins, font, sections, subsections, etc. The first class you can try is "article", but there are a number of different classes, such as book, phdthesis, letter, etc. You can even write your own LaTeX class, if you find yourself getting picky about the same fonts, settings, layouts, etc., and always use the same settings (or at least, want to bundle them together in an easy-to-use package). Many scientific journals provide their own class files for journal articles submitted to the journal.
The preamble is also where you specify packages to use. Many mathematical functions and notation require a mathematics package; additionally, other packages for graphics, colors, tables, etc. also use packages, and are not included by default.
\documentclass[english]{article}
% The following packages provide AMS (American Mathematical Society) functions, fonts, and symbols:
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
% The following package allows graphics to be put into LaTeX documents:
\usepackage{graphpicx}
% The following package allows wrapping text around figures:
\usepackage{wrapfig}
\begin{document}
This is a basic latex document.
\end{document}
Additionally, other document settings can be specified in the preamble.
The title, author, and date of the document can be set using:
\title{Document Title}
\author{Author Name}
\date{\today}
The size of paragraph indents can be set using:
\setlength{\parindent}{16pt}
The depth of section numbering (i.e. 1.2.3.4.5.6.....) can be set using:
\setcounter{secnumdepth}{3}
The basis for equation numbering can be set using
\numberwithin{equation}{section}
Some information, such as page numbers, may be included by default in the header or footer. To empty the header and footer, add this to the preamble:
\pagestyle{empty}
Margins
Margins can also be set in the preamble. The default margins are 1 inch. The left margin can be set by adding to or subtracting from the default value, by using \addsidemargin
:
\setlength{\addsidemargin}{-0.25in}
% Alternatively,
\addsidemargin=-0.25in
which will make the left side margin 1 in - 0.25 in = 0.75 in.
To make the left and right margins match, the text width must be set using
\setlength{\textwidth}{7.0in}
% Alternatively,
\textwidth=7.0in
The right margin is the remaining space left after the left margin and page width are specified.
Plenty of additional details and settings relating to margins can be found here: http://en.wikibooks.org/wiki/LaTeX/Page_Layout
Body
Top Matter
The top matter consists of things set after the \begin{document}
that set information about the document. The title, author, and date may be set in the preamble, or it may be set in the top matter. The main title, which contains the title, author, and date of the document, can be inserted using the LaTeX command \maketitle
. Also, more complex authors and titles can be set. for example,
\title{Document Title}
\author{Author1 \and Author2 \\
Department \\
University \\
City \\
State \\
Country \\
\texttt{address@email.com}
}
\date{\today}
and the title can be inserted via
\maketitle
A separate page for the title can be created using the titlepage
environment:
\begin{titlepage}
\title{Turbulence Project}
\author{Charles Reid}
\date{\today}
\maketitle
\end{titlepage}
More advanced title page creation may be found here: http://en.wikibooks.org/wiki/LaTeX/Title_Creation
Abstract
The abstract of a document, like the document body, may be created by putting it between \begin{abstract}
and \end{abstract}
. For example, the LaTeX version of Einstein, Podolsky, and Rosen's paper Can Quantum-Mechanical Description of Physical Reality Be Considered Complete? http://prola.aps.org/abstract/PR/v47/i10/p777_1 would look something like this:
By default, the abstract has the title Abstract. However, this can be changed using
\renewcommand{\abstractname}{Executive Summary}
Sections
In a document, a hierarchy of sections can be created using the LaTeX commands \section
, \subsection
, \subsubsection
, etc.
The preamble may contain \setcounter{secnumdepth}{N}
, which will number N levels of sections. These are, in order,
Section Type | Level Number |
Part | -1 |
Chapter | 0 |
Section | 1 |
Subsection | 2 |
Subsubsection | 3 |
Paragraph | 4 |
Subparagraph | 5 |
The section's level number is important. To set the numbering depth for the document, the command to be used is:
\setcounter{secnumdepth}{N}
where N is in the "Level Number" column.
Likewise, to set the level of sections included in the table of contents, the command is:
\setcounter{tocdepth}{N}
Paragraphs
Paragraphs are blocks of text separated by an empty line. The first paragraph of a section will not be indented. All other paragraphs will be indented.
If you want to change the amount by which each paragraph is indented, use
\setlength{\parindent}{1cm}
(or any other value in valid units).
Other environments
Environments are denoted using \begin{environment}
and \end{environment}
.
The alignment of text can be changed using three environments:
\begin{flushleft}
The flushleft macro will cause the text to be left justified
(meaning LaTeX will align it to the left, and attempt to get
the text flush with the left and right margins).
\end{flushleft}
\begin{flushright}
The same thing with flushleft, except this makes the text right justified.
\end{flushright}
\begin{center}
This text will be center aligned.
\end{center}
The verbatim environment can also be used to include text that won't be interpreted by the compiler:
\begin{verbatim}
The verbatim environment
reproduces every
character you input,
including all s p a c e s!
It also does not interpret
Latex macros like \emph{this}.
\end{verbatim}
Additionally, multi-line comments can be included using the comment
environment:
This would be some text.
\begin{comment}
And a very long comment
might go here.
This is more convenient than
putting a percent sign
in front of every single
line in the comment.
\end{comment}
More text would probably go here.
This requires the verbatim
package, so you have to put this into your preamble:
\usepackage{verbatim}
Lists
Latex has lots of different types of lists.
Bullet lists:
\begin{itemize}
\item First
\item Second
\item Third
\item Fourth
\end{itemize}
Numbered lists:
\begin{enumerate
\item First
\item Second
\item Third
\item Fourth
\end{enumerate}
Description/definition lists:
\begin{description}
\item[First] Description of first
\item[Second] Description of second
\item[Third] Description of third
\end{description}
Customizing enumerated lists:
http://en.wikibooks.org/wiki/LaTeX/Formatting#List_Structures
Notes
Footnotes
Footnotes can be made using the \footnote
macro.
This is some text.\footnote{This is a footnote.}
Footnotes can be enumerated using numbers, letters, or symbols. The following code goes in the preamble, and sets the footnote enumeration.
Latex code | Enumeration |
\renewcommand{\thefootnote}{\arabic{footnote}}
|
Arabic numbers (1,2,3,...) |
\renewcommand{\thefootnote}{\roman{footnote}}
|
Lowercase Roman numerals |
\renewcommand{\thefootnote}{\Roman{footnote}}
|
Uppercase Roman numerals |
\renewcommand{\thefootnote}{\alph{footnote}}
|
Lowercase letters |
\renewcommand{\thefootnote}{\Alph{footnote}}
|
Uppercase letters |
\renewcommand{\thefootnote}{\fnsymbol{footnote}}
|
Series of 9 symbols |
Latex Symbols
Some useful symbols:
Permutations
a = \bigl(\begin{smallmatrix} 1 & 2 & 3 & \cdots & n-1 & n \\ 2 & 3 & 4 & \cdots & n & 1 \end{smallmatrix}\bigr)
results in:
\alpha \top \beta
results in:
Packages
Installing LaTeX Packages
Installing LaTeX packages is very confusing and unclear, which makes using them a big pain in the arse.
The "Installing LaTeX Packages" instructions at Wikibooks [1] suck.
The following webpage was helpful to locating different things related to the LaTeX distribution: http://pages.uoregon.edu/koch/texshop/whereisstuff.html
Mac
I use the MacTex TeX distribution, instructions may change depending on which distribution you use.
Personal TeX files should be stored in ~/Library/texmf
. (Note that this is different from /Library
, which is system-wide.
When TeX needs to find a file, it searches things in ~/Library/texmf/
first, and the system-wide texmf
second, so if you modify a standard TeX file and place it there, the modified file will be used.
The folder structure inside ~/Library/texmf
should mimic that of the texmf trees in your TeX distribution (for MacTex, located in /usr/local/texlive/2009
or somewhere similar).
- TeX will locate any file in
~/Library/texmf/tex
or in a subfolder of this folder - LaTeX will locate any file in
~/Library/texmf/tex/latex
or a subfolder of this folder - LyX will locate any file in
~/Library/texmf/latex/lyx
- Bibtex will locate any file in
~/Library/texmf/bibtex/bib
or in a subfolder of this folder
Examples
Cancel Package
The Cancel package is a small, simple, handy package that allows you to put slashing arrows through terms in equations. This allows, for example, the "goes to zero" or "goes to infinity" notation often used for terms in engineering equations.
Installing the Cancel package on Mac was as easy as unzipping the downloaded Cancel package, and putting the .sty
file in ~/Library/texmf/tex/latex
.
For more information, see the Cancel Package article.
AtBeginEnd Package
The AtBeginEnd package is a package that simply allows you to add vertical space or other text to the beginning and end of your document.
Scripting
Can incorporate PDF to Latex workflows into Bash scripts, but it takes some care. Here's how it should look:
# latex to pdf pdflatex \ -interaction nonstopmode \ -halt-on-error \ $TEXFILE $PDFFILE \ && <command that will only be run if pdflatex command succeeds>
These two flags are important, because they ensure that LaTeX will keep going if it encounters errors, and ensure that it returns an error code if the compilation fails.
This enables linking the pdflatex command to other commands via &&
, which will only run the second command if the first one succeeds.
Also see scripts used to create the Tripos Bot here: https://git.charlesreid1.com/charlesreid1/tripos-bot/src/master/latex
Software
There are a large number of programs and pieces of software for interfacing with LaTeX.
Editing Latex: The simplest is to use LaTeX through a text editor, such as Vim or Emacs, or a LaTeX text editor such as TexMaker or TexShop. Alternatively, one may use a full WYSIWYM (What You See Is What You Mean) editor like LyX.
Personally, I recommend TexMaker for creating LaTeX documents by hand, and LyX for putting together LaTeX documents with lots of equations, or in a short time.
Examples
See Documents page for several examples of documents created using Latex.