Monthly Archives: May 2011

Placing figures/tables side-by-side (\subfig)

The subfigure package was replace by the subfig package quite a while ago. I therefore decided to replace my old post on that topic (Placing figures/tables side-by-side with subfigure) with an introduction to the subfig package. The package simplifies the positioning, captioning (I wonder if that’s a word) and labeling of small “sub” figures and tables within a single figure or table environment.

So much for the theory, let’s have a look at the usage. First we load the package, the options tell LaTeX to include the subs in the listoffigures and listoftables. Most probably you will also need the graphicx package, e.g. to set the figure width.

\usepackage[lofdepth,lotdepth]{subfig}
%\usepackage{graphicx}

The subfloat command has a body, for the content, being most commonly a figure/table plus a label. The two optional arguments define the list-of-figures text and the caption. If only one is provided, the text will be used for both, somewhat similar to the caption command (\caption[⟨list entry⟩]{⟨caption⟩}).

\subfloat[⟨list entry⟩][⟨sub-caption⟩]{⟨body⟩}

We need the figure (or table) floating environment as a container for the actual subfigure/subtables. This leads us to a complete subfigure example with two subfloats:

\begin{figure}[h]
\centering
 \subfloat[][]{
   \rule{4cm}{3cm}
 }
 \subfloat[][]{
   \rule{4cm}{3cm}
 }
\end{figure}

Referencing a subfloat is straight forward. The first code line will produce the complete reference number, e.g. 4a, whereas the second line will only produce the sub-index, e.g. (a).

\ref{⟨label⟩}
\subref{⟨label⟩}

By default, the package will place the figures/tables side-by-side. Now in case you want to arrange 4 subfloats 2×2, you make use of the standard linebreak by leaving an empty line.

The following is a complete code example. You will find a link to the expected result further below.

\documentclass[11pt, a4paper, draft]{article}
\usepackage{graphicx}
\usepackage[lofdepth,lotdepth]{subfig}
\begin{document}
\listoftables
\listoffigures
\section{Example with 3 "sub" tables}
\begin{table}[ht]
\centering
\subfloat[Subtable 1 list of tables text][Subtable 1 caption]{
\begin{tabular}{l|ccc}
& 1 & 2 & 3\\
\hline
1 & A & B & C\\
2 & D & E & F\\
\end{tabular}}
\qquad
\subfloat[Subtable 2 list of tables text][Subtable 2 caption]{
\begin{tabular}{l|ccc}
& 1 & 2 & 3\\
\hline
1 & A & B & C\\
2 & D & E & F\\
\end{tabular}}
\qquad
\subfloat[Subtable 3 list of tables text][Subtable 3 caption]{
\begin{tabular}{l|ccc}
& 1 & 2 & 3\\
\hline
1 & A & B & C\\
2 & D & E & F\\
\end{tabular}}
\caption{This is a table containing several subtables.}
\end{table}
\clearpage
\section{Example with 4 "sub" figures 2x2}
\begin{figure}[h]
\centering
\subfloat[Subfigure 1 list of figures text][Subfigure 1 caption]{
\includegraphics[width=0.4\textwidth]{figure1.jpg}
\label{fig:subfig1}}
\qquad
\subfloat[Subfigure 2 list of figures text][Subfigure 2 caption]{
\includegraphics[width=0.4\textwidth]{figure2.jpg}
\label{fig:subfig2}}
\subfloat[Subfigure 3 list of figures text][Subfigure 3 caption]{
\includegraphics[width=0.4\textwidth]{figure3.jpg}
\label{fig:subfig3}}
\qquad
\subfloat[Subfigure 4 list of figures text][Subfigure 4 caption]{
\includegraphics[width=0.4\textwidth]{figure4.jpg}
\label{fig:subfig4}}
\caption{This is a figure containing several subfigures.}
\label{fig:globfig}
\end{figure}
In the text, you can refer to subfigures of figure \ref{fig:globfig} as \ref{fig:subfig1}, \ref{fig:subfig2}, \ref{fig:subfig3} and \ref{fig:subfig4} and to the sub-index as \subref{fig:subfig1}, \subref{fig:subfig2}, \subref{fig:subfig3} and \subref{fig:subfig4}.
\end{document}

The PDF result can be viewed here.

The complete documentation of this very comprehensive package is available here.

A final remark:
In order to simulate figures for testing purposes, you may either use the draft option of the documentclass command which lets you load existing or non-existing figure files and draws an empty box as placeholder. Or you may make use of the rule command that by default draws a black line of a certain length and thickness.

\documentclass[draft]{article}
...
\includegraphics[width=0.3\textwidth]{virtualfigure.jpg}

\rule{4cm}{3cm}


Drop caps with \lettrine

According to Wikipedia a drop cap or initial is a letter at the beginning of a published or written work, chapter or paragraph that is larger than the rest of the text. I have to admit however that the following initial is not a LaTeX creation, but it nicely illustrates what I am talking about.

Initial example from Wikipedia

A historiated illuminated initial

Note: The image is in the public domain because its copyright has expired.

So let me show you how to create drop caps in LaTeX. First, we need to load two packages, one for scalable fonts (type1cm), removing the LaTeX restriction on font size. And secondly the actual drop cap package (lettrine):

\usepackage{type1cm}
\usepackage{lettrine}

Creating a dropping capital is now straight foward using the lettrine command which takes two mandatory and an optional argument:

\lettrine{O}{nce} upon a time...

By default, the capital will span two lines and letters of the second argument will be printed as small capitals.

Sample PDF output

Result of the lettrine example.

With little effort, we were able to produce a remarkable result. But with some tweaking, the package can do even better. For that we will need some optional arguments provided by the package, including:

  • lines: Number of lines the drop cap spans
  • findent: Controls the horizontal gap between the drop cap and the text
  • nindent: Shifts indented lines starting with the second
  • slope: Adapts the slope of the text lines to match letters like A, V, etc.
  • ante: Produces text before the drop cap
  • image=true: Loads an image (requires graphicx) as drop cap

Please see the documentation for a complete list of options and further details.

Using these options, I will now show a few examples and the result.

Example with A:
\lettrine[lines=3,slope=4pt,findent=-3pt]{A}{t vero} eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.

Example with W:
\lettrine[lines=3,slope=-4pt,nindent=-4pt]{W}{ho} are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish.

Complete code example with an image:
\documentclass[12pt]{article}
\usepackage[english]{babel}
\usepackage{graphicx, type1cm, lettrine, blindtext}
\begin{document}
\lettrine[image=true, lines=3, findent=3pt, nindent=0pt]{image.jpg}{n} the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain.
\end{document}

All the text was copied from this website and the PDF result can be viewed here.

Some known problems remain, including that “lettrine” does not work in the center environment, within lists and when a drop cap is produced at the end of a page, it might hang into the footer. But since drop caps are generally used at the beginning of a chapter or paragraph, these issues will not affect you in most cases.

Finally, I would like to remark that the small capital style of the text after the drop cap can be changed. The following command will set it to be normal text:

\renewcommand{\LettrineTextFont}{\rmfamily}

By default, lettrine uses \scshape for small capitals.


Multi-page tables using \longtable

The longtable package defines an environment that has most of the features of the tabular environment. In addition however, tables may be broken by TeX’s standard page breaking algorithm. Furthermore, it uses the same counter “table” as the table environment and has a \caption{...} command. Finally, “longtables” will be listed in the list of tables produced by \listoftables.

So let’s get started:

\usepackage{longtable}

Now, other than for single page tables, there a a few additional commands which define the headings and captions:

\endfirsthead: Line(s) to appear as head of the table on the first page
\endhead: Line(s) to appear at top of every page (except first)
\endfoot: Last line(s) to appear at the bottom of every page (except last)
\endlastfoot: Last line(s) to appear at the end of the table

Complete code example:

\documentclass[12pt]{article}
\usepackage{longtable}
\begin{document}
\begin{center}
\begin{longtable}{|c|c|c|c|}
\caption{A simple longtable example}\\
\hline
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\hline
\endfirsthead
\multicolumn{4}{c}%
{\tablename\ \thetable\ -- \textit{Continued from previous page}} \\
\hline
\textbf{First entry} & \textbf{Second entry} & \textbf{Third entry} & \textbf{Fourth entry} \\
\hline
\endhead
\hline \multicolumn{4}{r}{\textit{Continued on next page}} \\
\endfoot
\hline
\endlastfoot
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\ 1 & 2 & 3 & 4 \\
\end{longtable}
\end{center}
\end{document}

The \multicolumn command may be used in longtable in exactly the same way as for tabular. When using the multicolumn command, you may need to run your code through LaTeX several times. The documentation shows an example that only looks right after four passes. Furthermore, both portrait and landscape tables are possible.

The complete longtable package documentation can be found here.


Updating LaTeX / TeX Live

I found myself using the same TeX distribution for years, without updating it. This is not a problem usually, unless you are asking someone else to look at a particular error or problem and that person is working with another version and therefore may not be able to reproduce your issue. The TeX Live distribution is evolving, i.e. packages are extended, replaced or added and therefore it is a good idea to update your distribution once in a while. TUG provides an updated image every 1-2 years.

Windows:
If you have MiKTeX installed (part of proTeXt), there is a program that you can run in: All Programs -> MiKTeX -> Maintainance -> Update. This will open a dialog that helps you update the distribution.

Mac OS X:
Under Mac OS X, the TeX Live distribution is called MacTeX. It contains a neat little graphical application that lets you update your distribution. The application is called “TeX Live Utility” and can be found in Applications -> TeX. It has a built-in update function to keep the application itself up-to-date.

If you prefer the terminal, try:

sudo tlmgr update --self
sudo tlmgr update --all

Linux (Ubuntu):
It is a little tricky to update TeX Live under Linux and as usual depends on the distribution as well as your local settings. So below I describe the way I did it on my computer. It should at least give you an idea how to do it or what you may have missed in case you run into some problems. I am running Ubuntu.

I leave it to the reader to decide what he is more comfortable with, running commands with sudo or change the rights for the texlive directory you are updating. I will use the latter approach here. Set the username accordingly as well as the install directory if it differs.

sudo chown -hR your_user_name /usr/local/texlive/2010

If it has not been done before, add the install directory of the TeX Live distribution to the path. The tlmgr-command (texlive manager) is in that directory.

export PATH=$PATH:/usr/local/texlive/2010/bin/i386-linux/

Next you have to set the location where you want to update from. This has to be done only the first time you are updating. In addition, you may be asked to update “tlmgr” also in case there is a newer version.

tlmgr option location http://mirror.ctan.org/systems/texlive/tlnet
and
tlmgr update --self

Finally, you are ready to update the TeX Live distribution.

tlmgr update --all

This final step may take a moment, time for a coffee.


\the command

Frankly speaking I don’t know what for, but \the in front of a length command displays its value (in pt):

\the\textwidth translates to \the\textwidth

A list of length “macros” can be found on wikibooks.

Use the layouts package to convert pt to other units including mm, cm and in:

\usepackage{layouts}
...
\printinunitsof{mm}\prntlen{\paperwidth}

In general, the layouts package enables the display of various elements of a document’s layout and experimentation with different potential layout designs.


Beamer: An introduction to LaTeX presentations

Beamer is a LaTeX document class that provides extensive functionality to create presentations. Here, I will only show the basics and after reading this guide you will be able to create a simple presentation in LaTeX. I am aware there are a lot of tutorials available out there and this is not different from any other tutorial. I hope however, I can encourage some of you who have hesitated so far, for whatever reason, to create your next presentation with LaTeX. And I’m sure it will be a lot of fun, with similar effort. I should add that the output will obviously be a PDF file (with all its advantages!). Luckily, PDF-viewers (including Adobe Acrobat) provide a fullscreen-mode for presentation purposes.

So lets get started!

\documentclass{beamer}

Now that line is straight forward, not much to say about it. Once that’s done, we have to choose a theme. This website gives a visual overview of the most common themes. I like Singapore:

\usetheme{Singapore}

Next, still in the preamble, we prepare the title page, using a similar set of commands as for other document-classes:

\title{Your Presentation Title}
\author{The author}
\date{February 4, 2011}

A frame may have one or several slides. Since PDFs are static, dynamic “effects” such as adding more content to a frame are achieved by two consecutive slides in the output file.

We use the previously defined title page to create our first (single-slided) frame:

\begin{frame}
\titlepage
\end{frame}

Similar to articles, sections, subsections, etc. are available and can be used to define an outline, printed with \tableofcontents. For many themes, the outline will be displayed in the header/footer and provides direct access to a certain section of the presentation. Frame-titles are created using \frametitle{Title}.

Most of the time, a frame will show a list of items created through the well known itemize-environment:

\begin{frame}
\frametitle{Title of the Frame}
\begin{itemize}
\item First item
\item Second item
\item ...
\end{itemize}
\end{frame}

Now what if you don’t want to show all the items at once, but one after another. The \pause-command will take care of it. Just add it anywhere you want to “pause” and will produce 3 slides. In presentation mode, the next bit of information is only shown after you press a key (usually space or arrow keys). So the above code example now looks as follows:

\begin{frame}
\frametitle{Title of the Frame}
\begin{itemize}
\item First item \pause
\item Second item \pause
\item ...
\end{itemize}
\end{frame}

Figures are used similarly as within other document-classes:

\usepackage{graphicx}
...
\begin{figure}
\includegraphics[scale=0.5]{img.jpg}
\caption{Sample caption.}
\end{figure}

Finally, I will show you something a little more advanced. Two columns, with items on the left and figures on the right side. The idea is to show an item along with an image. We want one item after the other to appear, while the image replaces the previous. Let me give you the code first and then explain some of the details:

\begin{frame}{A More Advanced Example}
\begin{columns}
\begin{column}{5cm}
\begin{itemize}
\item<1-> Figure 1
\item<2-> Figure 2
\item<3-> Figure 3
\end{itemize}
\vspace{2cm}
\end{column}
\begin{column}{5cm}
\includegraphics<1>[scale=0.1]{img1.jpg}
\includegraphics<2>[scale=0.1]{img2.jpg}
\includegraphics<3>[scale=0.1]{img3.jpg}
\end{column}
\end{columns}
\end{frame}

What’s new here is called overlay specification within an environment (itemize) and lets you display different text/content on different slides or a range of slides.

<1-> indicates that this item will be displayed from slide 1 onwards in this frame. We could also have used <1-3>. Whereas the actual figures will only be displayed on their specific slide, e.g. <1>.

If there is no environment, the set of things to display has to be enclosed by the overprint-environment:  \begin{overprint}...\end{overprint}.

Another thing is the “overlay specification” for commands, e.g. to change the text-color for slides 2 and 3:

\color<2-3>[rgb]{1,0,0} This text is red on slides 2 and 3, otherwise black.

Here, the overlay specification always has to follow the command before any additional arguments. I have to admit, slightly useless, but at least it illustrates the result nicely (example was taken from the user guide page 81).

The following a complete code sample with a few frames containing the various examples described above:

\documentclass{beamer}
\usepackage{graphicx}
\usetheme{Singapore}
\title{Presentation Title}
\author{The Author}
\date{May 4, 2011}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}
\frametitle{Outline}
\tableofcontents
\end{frame}
\section{List of Items}
\begin{frame}
\frametitle{List of Items}
\begin{itemize}
\item First item \pause
\item Second item \pause
\item ...
\end{itemize}
\end{frame}
\section{Figure Example}
\begin{frame}
\frametitle{Figure Example}
\begin{figure}
\includegraphics[scale=0.1]{img1.jpg}
\caption{Sample caption.}
\end{figure}
\end{frame}
\section{Overlay Specification}
\begin{frame}
\frametitle{Overlay Specification}
\begin{columns}
\begin{column}{5cm}
\begin{itemize}
\item<1-> Figure 1
\item<2-> Figure 2
\item<3-> Figure 3
\end{itemize}
\vspace{3cm}
\end{column}
\begin{column}{5cm}
\includegraphics<1>[scale=0.1]{img1.jpg}
\includegraphics<2>[scale=0.1]{img2.jpg}
\includegraphics<3>[scale=0.1]{img3.jpg}
\end{column}
\end{columns}
\end{frame}
\end{document}

The packages hyperref, xcolor, color are automatically loaded when using the beamer class.

A comprehensive user guide can be downloaded from CTAN.


Date and time

I’m sure most of you have used \date{...} to print a date in a title (\maketitle) or letter. Probably you also know the more dynamic command \today to produce today’s date. Setting the date dynamically is not recommended (or even prohibited by some compilers) for usage in letters (i.e. \date{\today} or simply \date), since it makes tracing impossible.

Now what about time? Latex knows two packages that provide functionality related to time, datetime and KOMA-Script’s scrtime.

datetime:

\usepackage[option (see below)]{datetime}

\currenttime prints the actual time (hh:mm by default). In order to change the time format use:

\settimeformat{...}  with one of the following arguments:

xxivtime Twenty-four hour time, e.g. 22:28 (default).
hhmmsstime Twenty-four hour time, e.g. 22:28:00.
ampmtime Twelve hour time, e.g. 10:28pm.
oclock Displays the current time as a string, an interesting construct :-) .

You may also use any of these directly in the preamble where you load the package as an option.

The datetime package easily let’s you adapt the time separator:

\renewcommand{\timeseparator}{.}

The complete documentation can be found here.

scrtime:

\usepackage[option]{scrtime}

The option 12h=true can be used for the 12h-time-format. However, there is no am/pm that is automatically added to the time.

The current time (hh:mm by default) is produced using:

\thistime

The separator can be set as optional argument, e.g. \thistime[.]. Furthermore, using the form \thistime* will not produce a zero in front of the minutes in case the number is below 10.

Use \settime{} to set a constant value for \thistime. I’m not sure how to undo it though.

The complete documentation can be found here as part of the KOMA-Script documentation (in German).

Finally, both packages include commands for date-formating which I will not further describe here. Please see the docmentation for details. In general, the “datetime” package offers more functionality and therefore probably is the better choice in most cases, unless you are using the KOMA-Script anyway.


Follow

Get every new post delivered to your Inbox.

Join 316 other followers