# Ryan's Blog

## Latex scientific notation, made easy

Posted in latex by ryanlayer on January 13, 2012

\providecommand{\e}[1]{\ensuremath{\times 10^{#1}}}

Then, typing

The [111] crystal planes are 3.2\e{-10} m apart.

http://www.tapdancinggoats.com/easy-scientific-notation-in-latex.htm

## Metapost Graphs

Posted in latex by ryanlayer on October 24, 2009

Data file: data.d

1 4
2 2
3 1
4 2
5 4

Metapost file:

beginfig(1);
draw begingraph(3in,2in);
gdraw “data.d”;
glabel.bot(btex $x$-axis etex, OUT);
glabel.lft(btex $y$-axis etex, OUT);
glabel.top(btex The graph of $y=f(x)$. etex, OUT);
glabel.rt(btex $y={1\over{4-x^2}}$ etex, 2,3);
endgraph;
endfig;

Tagged with: , ,

## Practical Latex and Metapost Tips

Posted in latex by ryanlayer on October 24, 2009

Found what looks like a great collection of practical Latex and Metapost tips.

http://online.redwoods.cc.ca.us/instruct/darnold/staffdev/

Tagged with: ,

## Latex in Metapost

Posted in latex by ryanlayer on October 23, 2009

You can even embed TeX inside your pictures for labels and the like.  First lets start with a plain label

label.pos(“label text”, <coordinates>;

where <coordinates> can be just a point like (0,0) or the midpoint of a line 1/2[A,B], where A and B are two points, or some other expression. ‘pos’ is a relative position indicator for the label which can be top, bot, lft, rt, or on the corners: ulf, urt, llf, lrt. We can add TeX to a label by putting TeX commands between the commands btex and etex:

label.bot(btex $TeX commands$ etex,(0,0));

Note that this will only work for TeX commands, not LaTeX specific ones like \frac! If you need to use LaTeX commands you need to put the following at the top of the .mp file

verbatimtex
%&latex
\documentclass{article}
\begin{document}
etex

Note that the ‘%&latex’ line is not a comment, though it starts with % – it is a command to use LaTeX, and that no \end{document} is necessary. Furthermore, the TeX will be typset in text style by default, so you’ll need to add a \displaystyle inside the $’s so that your \frac’s aren’t squashed. Note furthermore that if your metapost contains TeX then a standard postscript viewer will not be able to read it. This is no real limitation since you’ll probably want these in a TeX document anyway. To view them, you’ll have to wrap it in a TeX documents and view the DVI output, or convert that to ps (or you can use the script linked at the bottom of the page). Another option is to add the line ‘prologues := 1’ at the beginning of the mpost file, which will try to substitue postscript fonts for the TeX. This can be good for previewing, but the results are never satisfying. Finally there is the dotlabel command, which works exactly as label, except it places a dot at the indicated point. Tagged with: ## Handy Latex Metapost Bibtex Makefile Posted in latex by ryanlayer on October 8, 2009 This Makefile is not the most robust thing in the world, but it is generic enough for me to use on almost all of my documents. The command you want to run is make FILE=<latex file name without extension>. If your file is doc.tex, then you will run make FILE=doc. The assumption here is that the tex, mp, and bib files all have matching names (e.g., doc.tex, doc.mp, and doc.bib). The Makefile checks to see if the metapost file and bibtex file exist before attempting to process them. The Makefile creates dvi, ps, and pdf files. NOTE: You will have to insert tabs to make this work MPOSTDIR:=./ BIBDIR:=./ LATEX_OPTS:=-halt-on-error FILE:=$(strip $(wildcard *.tex)) MPOSTFILE:=$(wildcard $(MPOSTDIR)/$(FILE).mp)
BIBFILE:=$(wildcard$(BIBDIR)/$(FILE).bib) all: pdf pdf: ps ps2pdf$(FILE).ps

ps: dvi
dvips -t letter $(FILE).dvi -o$(FILE).ps

dvi: mp bib
latex $(LATEX_OPTS)$(FILE).tex

quick: mp
latex $(LATEX_OPTS)$(FILE).tex
dvips $(FILE).dvi -o$(FILE).ps

bib:
ifdef BIBFILE
latex $(LATEX_OPTS)$(FILE).tex
bibtex $(BIBDIR)/$(FILE)
latex $(LATEX_OPTS)$(FILE).tex
endif

mp:
ifdef MPOSTFILE
cd $(MPOSTDIR) && \ mpost$(FILE).mp && \
cd ..
endif

clean:
rm -f *.aux
rm -f *.bbl
rm -f *.blg
rm -f *.log
rm -f *.dvi
rm -f *.ps
rm -f *.tex\~

Tagged with: ,

## Latex Mathematical Symbols

Posted in latex by ryanlayer on October 6, 2009

In the following tables you find all the symbols normally accessible from math mode.

To use the symbols listed in Tables 3.123.16, the package amssymb must be loaded in the preamble of the document and the AMS math fonts must be installed on the system. If the AMS package and fonts are not installed, on your system, have a look at
CTAN:/tex-archive/macros/latex/packages/amslatex

Tagged with: ,

## Squeezing Space in LaTeX

Posted in latex by ryanlayer on October 6, 2009

Here are some tips on how to squeeze a little more onto LaTeX pages. Note that these methods may adversely affect the appearance of the document, so use them with caution. Rephrasing and editing your text often leads to better results.

The savetrees package executes many of these methods, packing as much text as possible onto each page.

## Fonts

Without changing the point size of a font you might be able to fit more text on a page by choosing a more compact font like Times

## Page Layout

The a4 package will give you narrower margins. To have more control use the geometry package. If you use the layout package then \layout will produce a test page showing the values of the variables that control page layout.

Prof. Koenraad De Smedt’s suggested that by using \linespread{0.9} (which multiplies the normal space between lines by a factor 0.9) you can often get satisfactory results.

## Changing lengths

There are many variables in LaTeX determining lengths. Two commands to change them are \addtolength and \setlength. The dimensions understood by LaTeX include cm, mm, in and pt. Variables can be set to a negative value.

Some useful variables are

• Page Layout
• \columnsep: gap between columns
• \topskip: between header and text
• \textheight: height of main text
• \textwidth: width of text
• \oddsidemargin: odd page left margin
• \evensidemargin : even page left margin
• Paragraphs
• \parindent: indentation of paragraphs
• \parskip: gap between paragraphs
• Floats (tables and figures)
• \floatsep: space left between floats.
• \textfloatsep: space between last top float or first bottom float and the text.
• \intextsep : space left on top and bottom of an in-text float.
• \dbltextfloatsep is \textfloatsep for 2 column output.
• \dblfloatsep is \floatsep for 2 column output.
• \abovecaptionskip: space above caption
• \belowcaptionskip: space below caption
• Maths
• \abovedisplayskip: space before maths
• \belowdisplayskip: space after maths
• \arraycolsep: gap between columns of an array
• Lists
• \topsep: space between first item and preceding paragraph.
• \partopsep: extra space added to \topsep when environment starts a new paragraph.
• \itemsep: space between successive items.

## Environments

The atbeginend.sty package provides \BeforeBegin{environment}{code-to-execute}, \AfterEnd ... etc. These commands can be used to remove space around and within environments. This can be useful in situations where the environment resets values that over-ride those you’ve created. For example, \AfterBegin{itemize}{\addtolength{\itemsep}{-\baselineskip}} in a LaTeX file squeezes items together by resetting \itemsep inside the environment.

The mdwlist package has a itemize* environment.

The paralist package offers a compactitem environment (that puts less space between items) and an inparaenum environment (that doesn’t create new paragraphs for each item).

## Figures and Tables

To save space, you can put more than one graphic inside a figure environment by using the subfigure package. It’s also possible to wrap text around a figure using the floatflt package.

Using “\begin{figure} ... \centering ...” rather than “\begin{figure} ... \begin{center} ...” saves space. Sometimes excessive white space around a figure isn’t LaTeX’s fault. It may be that a postscript figure contains a big white border. ps2epsi can be used to produce a minimal bounding box, or you can use the clipping feature of the \includegraphics command of the graphicx package.

By default, LaTeX doesn’t like to fill more than 0.7 of a text page with tables and graphics, nor does it like too many figures per page. This behaviour can be changed by placing lines like the following before \begin{document}

\renewcommand\floatpagefraction{.9}
\renewcommand\topfraction{.9}
\renewcommand\bottomfraction{.9}
\renewcommand\textfraction{.1}
\setcounter{totalnumber}{50}
\setcounter{topnumber}{50}
\setcounter{bottomnumber}{50}


To reduce the size of captions use the caption package.

If you have a big table you might wish to bracket it by \begin{small}\end{small}.

You can reduce the gap between table columns by using \setlength{\tabcolsep}{1pt}. It may also be possible to scale a whole table as you can a piece of graphics, using \resizebox{!}{5cm}{\begin{tabular} ... \end{tabular}} though you need to view the output as postscript.

## Controlling where space goes

If latex can’t fill a page (perhaps because the next object is a figure or table that doesn’t fit, or because the next object is a heading that latex doesn’t want to put at the foot of a page) then by default it will spread the vertical white space on the page so that the “height” of the page is the default height. If you want all the extra white space to appear at the bottom of such pages, add \raggedbottom before your \begin{document} line.

Vertical space around a heading depends on \parskip, \baselineskip and other parameters that LaTeX sends to the @startsection routine. Remember also that LaTeX doesn’t like putting a heading right at the bottom of a page – it would rather leave space.

Unless you want to redefine the sectioning commands yourself, it’s worth looking at the titlesec package, which offers space-saving alternatives to the standard sectioning commands (especially \chapter). Even just \usepackage[small,compact]{titlesec} might save you quite a lot.

## Contents and bibliography

To reduce the linespacing in a bibliography (the same idea works for contents pages) use the setspace package

\begin{spacing}{0.9}
\tableofcontents
\end{spacing}
...

\begin{spacing}{0.9}
\bibliographystyle{plain}
\bibliography{refs}
\end{spacing}


If you’re using the natbib package (recommended) then you can change the value of \bibsep to control the gap between items. Otherwise put the following (suggested by Axel Reichert) in the preamble

  \let\oldthebibliography=\thebibliography
\let\endoldthebibliography=\endthebibliography
\renewenvironment{thebibliography}[1]{%
\begin{oldthebibliography}{#1}%
\setlength{\parskip}{0ex}%
\setlength{\itemsep}{0ex}%
}%
{%
\end{oldthebibliography}%
}


You can use the same idea to modify other environments – theglossary, etc.

Tagged with: , ,

## Including graphics in a LaTeX document

Posted in latex by ryanlayer on October 6, 2009

There are two ways to incorporate images into your LaTeX document, and both use the graphicx package by means of putting the command  \usepackage{graphicx} near the top of the LaTeX file, just after the documentclass command.

The two methods are

1. include only PostScript images (esp. “Encapsulated PostScript”) if your goal is a PostScript document using dvips
2. include only PDF, PNG, JPEG and GIF (Stefan correctly pointed out that GIF is not supported, you must convert to a supported fomat) images if your goal is a PDF document using pdflatex, TeXShop, or other PDF-oriented compiler.

In all cases, each image must be in an individual 1-image file; no animation files or multipage documents.

## Only PostScript images

Most mathematical/scientific graphics software allows you to save graphics (figures, diagrams, graphs) in PostScript form; this includes Mathematica, Maple, Matlab, IDL, and xfig (a marvelous X figure-drawing program). Even bitmap images like JPEG and PNG files can be converted to PostScript form with programs like “xv” or “convert

”.

Where you want a PostScript image to appear, use the \includegraphics command, possibly with scaling or rotation options, e.g.,

  \includegraphics{myfig.eps}
\includegraphics[width=60mm]{myfig.eps}
\includegraphics[height=60mm]{myfig.eps}
\includegraphics[scale=0.75]{myfig.eps}
\includegraphics[angle=45,width=52mm]{myfig.eps}


Of course, any of these can be inserted into a figure environment, and/or a centering environment or a framebox.

Then you must compile the document with latex followed by dvips -Ppdf, which produces a PostScript document with embedded PostScript images. You can convert the PostScript document to PDF using “ps2pdf” or “dvipdf”.

Two characteristics which the component (Encapsulated) PostScript image files must have are

• no file contains more than one page
• each has a valid BoundingBox line

If the PostScript file does not include a bounding box line, you can insert it using the text editor of your choice.

## Only non-PostScript images

The compiler pdflatex (Unix) and TeXShop (Macintosh) convert LaTeX source directly to PDF, and do not accept PostScript images. Instead, they take PDF images, as well as bitmap pictures in PNG or JPEG or GIF format. So to use pdflatex, you must convert any PostScript images to one of these other forms. For photos, JPEG is best. For other bitmap images, PNG is best. For non-bitmap images (e.g., graphs, drawings, stuff with text and symbols) it is best to convert to PDF, using the command epstopdf (in the usual TeX bin directory, e.g., /usr/local/tex/bin/epstopdf). The command

unix>   epstopdf myfig.eps

produces the file myfig.pdf, which can then be used in the command \includegraphics{myfig.pdf}. for compilation with pdflatex or TeXShop.

The possible \includegraphics options with PDF/PNG/JPEG images are much the same as with PostScript, e.g.,

  \includegraphics{myfig.pdf}
\includegraphics[width=60mm]{myfig.png}
\includegraphics[height=60mm]{myfig.jpg}
\includegraphics[scale=0.75]{myfig.pdf}
\includegraphics[angle=45,width=52mm]{myfig.jpg}


With PNG or JPEG you should specify an explicit width or height rather than “scale”, since bitmap images have no intrinsic size, nothing corresponding to Bounding Box information, although graphicx seems to use 72 pixels per inch as a default size for bitmap images.

## NASA GISS Help with Latex

Posted in latex by ryanlayer on October 5, 2009
Tagged with:

## Latex cline

Posted in latex by ryanlayer on October 5, 2009
\cline

\cline{i-j}

The \cline command draws horizontal lines across the columns
specified, beginning in column i and ending in column j, which
are identified in the mandatory argument.

It is used in the tabular and array environment.

Two (or more) successive \cline commands draw lines in the same
vertical position. Typically, \cline commands are placed after a
\ (line breadk) command so that they separate items on two lines;
they do not, however, need a line break command afterwards.

\begin{tabular}{|r|l|}
\hline