LaTeX: Inserting Images

The most command way to insert images or figures into a LaTeX file is by using the \includegraphics command. This command comes from the graphicx package.

Simple usage is as follows:

% At top of the document
\usepackage{graphicx}
% At location where image is to be inserted
\includegraphics{foo.pdf}

Tried with: MiKTeX 2.9

Using BibTeX

BibTeX is used to add citations to a LaTeX document. Using it along with a LaTeX document is a bit unconventional. It is illustrated below with a simple example.

1. Assume, the LaTeX document is Main.tex and the bibliographic records have been placed in a file named References.bib.

2. Add a bibliographystyle command to Main.tex to indicate the bibliography style to use. For example, to use the default plain style use:

\bibliographystyle{plain}

3. Add a bibliography command to Main.tex to indicate the file containing the bibliographic entries to be used. Multiple bibliography files can be specified (separated by commas) and the .bib file extension is dropped. For example, add this line to Main.tex:

\bibliography{References}

4. Compile Main.tex. For example:

pdflatex Main

It will compile, but will output warnings that there were undefined references. The compilation produces a Main.aux file.

5. Run BibTeX on Main.tex:

bibtex Main

This is one of the confusing aspects of BibTeX: it is run on the LaTeX file and not on the bibliography file! :-D

BibTeX will look for the Main.aux file, look at the undefined references in it and try to resolve them using the bibliography file(s) indicated in Main.tex.

BibTeX produces a file named Main.bbl, which is a database of references.

6. Compile Main.tex (just like in step 4). It will still complain about undefined references.

7. Compile Main.tex again (you heard it right!). The output file now should have its citations correctly.

To summarize, here are the commands for Main.tex and References.bib:

pdflatex Main
bibtex Main
pdflatex Main
pdflatex Main

Tried with: MikTeX 2.9

LEd and MikTeX 2.9

The LEd (LaTeX Editor) was last released in 2009 and supported MikTeX 2.8 back then. To use it with MikTeX 2.9, open ConfigurationOptions and change these settings:

  • Set TeX executables option to C:\Program Files\MiKTeX 2.9\miktex\bin (32-bit Windows) or C:\Program Files (x86)\MiKTeX 2.9\miktex\bin (64-bit Windows).
  • Leave the TeX distribution option at MikTeX 2.8

Tried with: LEd 0.53

LaTeX: Import & Subimport for Document Organization

Large LaTeX documents can be organized nicely by using the \input command. For more on that see this post. However, the \input command is not intelligent, it blindly includes the content of its files.

For example, a Report.tex includes a 01-IntroDir/01-Intro.tex file that uses lots of images stored in 01-IntroDir. All of the \includegraphics commands in 01-IntroDir/01-Intro.tex would have to include the 01-IntroDir/ prefix:

% 01-IntroDir/01-Intro.tex
\includegraphics{01-IntroDir/Image0.png}
\includegraphics{01-IntroDir/Image1.png}
\includegraphics{01-IntroDir/Image2.png}

To intelligently include the content in other directories such that all their content paths are also relative, the \import and \subimport commands from the import package can be used.

For example, Report.tex can be changed to:

% Report.tex
\usepackage{import}
\subimport{01-IntroDir/}{01-Intro.tex}

When \import or \subimport are used, the imported files can refer to their images/files relative to themselves. So, the above 01-IntroDir/01-Intro.tex can be simplified to:

% 01-IntroDir/01-Intro.tex
\includegraphics{Image0.png}
\includegraphics{Image1.png}
\includegraphics{Image2.png}

LaTeX: Using input command for document organization

Creating and maintaining a large LaTeX document using a single .tex file can be painful. It would be wise to organize it as sections, with one or more .tex files for each section.

For example, a large Report.tex could be broken into 2 smaller sections: 01-Intro.tex and 02-Results.tex. These files can be included into the main Report.tex using the \input command like this:

% Report.tex
\input{01-Intro.tex}
\input{02-Results.tex}

If 01-Intro.tex and 02-Results.tex include a lot of images, then it might be nice to move them and their respective images/files into their own subdirectories, say 01-IntroDir and 02-ResultsDir. In that case, Report.tex is changed to:

% Report.tex
\input{01-IntroDir/01-Intro.tex}
\input{02-ResultsDir/02-Results.tex}

Note that the \input command just pulls the text from 01-IntroDir/01-Intro.tex into the main Report.tex for compilation. Thus, inside 01-IntroDir/01-Intro.tex commands like \includegraphics still need to provide the full path of their image relative to the main Report.tex:

% 01-IntroDir/01-Intro.tex

% Image IntroPic.png is in 01-IntroDir

% This does not work:
\includegraphics{IntroPic.png}

% This is how to correctly include:
\includegraphics{01-IntroDir/IntroPic.png}

LaTeX: Color Text

Adding color to text in LaTeX is super easy!

Include the color package:

\usepackage{color}

Specify the color and the text you want colored to the textcolor command wherever needed:

\textcolor{red}{This is colored in red!}

To see other predefined color names go here.

LaTeX: Adding Draft Watermark

While collaborating on or sharing an unfinished PDF document produced from LaTeX it is nice to remind everyone that it is in the draft stage. This can be indicated elegantly by adding a DRAFT watermark on every page behind the page content. To do this use the package draftwatermark in the LaTeX document preamble:

\usepackage{draftwatermark}

The draftwatermark package uses the everypage package to insert the watermark in every page, so that package might have to be installed too.

By default, the watermark is added to all pages. To insert on only the first page use:

\usepackage[firstpage]{draftwatermark}

The default watermark text is DRAFT. To insert a different watermark text (say BIO-HAZARD) use this command:

\SetWatermarkText{BIO-HAZARD}

The default lightness (or darkness) of the watermark text is too light for my taste. To set a different lightness use:

\SetWatermarkLightness{0.5}

The lightness value ranges from 1.0 for white and 0.0 for black. By default it is set to 0.8.

The default scale of the text is 1.2, which I find too small. To change the scale of the watermark text use:

\SetWatermarkScale{4}

Beamer: Creating a Handout

Having image animations and itemized list transitions in a presentation generated using Beamer looks great! But, printing out these animations and transitions on paper is wasteful and redundant. To discard these transitions while creating a PDF for printing as handouts, specify the handout option to Beamer in the preamble:

\documentclass[handout]{beamer}

LaTeX: KOMA-Script

KOMA-Script is a LaTeX package that introduces modern updates to the classic document classes that LaTeX uses by default. Not only are the styles in the old document classes outdated, they are also set for the letter paper size. The letter paper size is used only in the USA, with the rest of the world having moved on to the ISO A4 size a long time ago. The KOMA-Script layout is noticeably larger, using more of the space in the A4 paper size. The size comparison of the default LaTeX document class with that of KOMA-Script can be seen in the figure above. KOMA-Script also uses a different set of fonts for these document classes, which in my opinion looks fresher.

Using KOMA-Script for your LaTeX documents is very easy. First, download and install the koma-script package. Next, replace the standard document classes (article, book, letter, report) with those provided by KOMA-Script (scrartcl, scrbook, scrlttr2, scrreprt). That is it! :-)

Wondering why the document class names have a prefix of scr? KOMA-Script is the successor of an older package which was named Script, which introduced the scr prefix.

If you are using TeX Live (in Ubuntu for example), KOMA-Script can be found in the texlive-latex-recommended package.

(Via Top 10 LaTeX Modules by Nico Schlömer)

LaTeX: Footnotes

A footnote is a note placed at the bottom of the page that references text in the page above it. Footnotes can be added to a LaTeX document using the TeX \footnote macro.

For example, the above footnotes can be obtained by:

Footnotes\footnote{A note at the page bottom.} are frowned upon in the \LaTeX community\footnote{No idea why!}.

I personally do not like this style. I find it distracting to put the footnote text in the middle of the referencing text. An alternative is to provide a footnote mark in the referencing text and place the footnote text later. This can be done using the LaTeX \footnotemark and \footnotetext commands.

For example, to obtain the footnotes as shown above:

Footnotes\footnotemark[1] are frowned upon in the \LaTeX community\footnotemark[2].

\footnotetext[1]{A note at the page bottom.}
\footnotetext[2]{No idea why!}

If you are using only a single footnote, then the footnote number argument can be skipped:
Footnotes\footnotemark are frowned upon in the \LaTeX community.

\footnotetext{A note at the page bottom.}

Reference: Section 4.5 Footnotes from the book Digital Typography Using LaTeX.