LATEX Document Formatting Examples

Gene Michael Stover

created Thursday, 2005 May 19
updated Sunday, 2005 November 20

Copyright © 2005 Gene Michael Stover. All rights reserved. Permission to copy, store, & view this document unmodified & in its entirety is granted.


Contents

1 What is this?

Most of what I do here is discussed in one or more of these sources:

All I'm doing in this essay is trying out what they say, figuring it out now so I can use it when I want it later.

2 Summary

I found two ways to include images. They are:

What I'd really like to do is make text in a paragraph flow around a small image in both the printed output & the HTML output. I don't know how to do that yet.

3 Pictures in Figures

Let's start with a colour JPEG image & put it in a figure so that it displays in both the HTML & the dead tree versions of the document.

Figure 1 shows the figure. The idea is that it looks pretty much the same in the HTML & printed versions of this essay.

Figure 1: My cat, Wordsworth. 2004 December.
\begin{figure}\epsfig{file=04-640.ps}\end{figure}

Here's how I did that:

  1. I began with a colour JPEG picture that is 640 by 480.

  2. Loaded the image into The Gimp.

  3. Saved the image as a Post Script file. Options I used on the Safe As... window were:
    1. Post Script level 2
    2. Encapsulated Post Script
    3. Preserve aspect ratio
    4. Units of millimeters
    5. X & Y offsets of 1.00 & 1.00
    6. Width & Height of 120 & 90. Those dimensions have the same ratio as 640:480. I experimented to find dimensions which were small enough for the printed page.
    7. New filename of 04-640.ps

  4. Near the top of the LATEX source code for this document, I inserted \usepackage{epsfig}.

  5. The LATEX code for the figure is:
    \begin{center}
    \begin{figure}
    \epsfig{file=04-640.ps}
    \caption{My cat, Wordsworth. 2004 December.}
    \label{fig-colour-jpeg}
    \end{figure}
    \end{center}

This technique seems to work for both the PDF output & the HTML output except that in the HTML version, LATEX2HTML converted the encapsulated Post Script image to a Portable Network Graphics (PNG) file & lost the colour in the process.

Some research shows that the loss of colour happens when pstoimg converts the encapsulated Post Script image to a PNG. I experimented with ways to run pstoimg from the command line, & everything I tried lost the colors, so fixing things is not just a matter of correcting how LATEX2html invokes pstoimg. Ideally, it would be a case of improving pstoimg.

Not bad for a first try. I'm surprised & not exactly pleased that, in the printed version, the figure is on the next page, by itself. If a future version of pstoimg preserves colours, this technique will be perfect for figures containing pictures.

I tried using conditional compilation to use the Post Script image in the PDF output & a hyperlink to the JPEG in the HTML output, but no luck. The PDF output is fine, but the HTML output is a great big gray, featureless box. Figure 2 demonstrates.

Figure 2: Using conditional compilation for images in a figure. Should look fine in PDF, but doesn't work in the HTML.
\begin{figure}\htmladdimg{../pho/2004-12/04-640.jpg}\end{figure}

Here is the source code for Figure 2:

    \begin{figure}
    \htmladdimg{../pho/2004-12/04-640.jpg}
    \caption{Using conditional compilation for images in a
      figure.  Should look fine in {\sc pdf}, but doesn't work
      in the {\sc html}.}
    \label{fig-conditional-00}
    \end{figure}

So that didn't work.

4 Images outside of figures

If I don't need the image to be in a figure, conditional compilation works.

Here is an image, outside of a figure, which uses \epsfig to load an encapsulated Post Script file for the PDF output & an hyperlink to the original image for the HTML version. It switches between the two commands with conditional compilation.

04-640.jpg
Here's the LATEX source code which displayed that image:

\begin{latexonly}
\begin{center}
\epsfig{file=04-640.ps}
\end{center}
\end{latexonly}
\begin{htmlonly}
\htmladdimg[ALIGN="CENTER"]{../pho/2004-12/04-640.jpg}
\\
\end{htmlonly}

That's pretty much what I want, though it only works outisde of a figure.

5 The includegraphics command

I used \epsfig for the image. For the HTML version, that leads to the problem of lost colours because LATEX2html must convert it to an image file. The \includegraphics command should allow us to use the raw image file for both the PDF & the html.

Here are some commands I tried from the graphicx package, but they all produce errors at compile-time. I'm not sure, but I think the dvips program may be the one which spews the error.

Here are some things I tried from graphicx:

6 Text Flowing Around Pictures

6.1 Attempt 1, Failure

We'll start with a JPEG image. The one I used is at http://cybertiggyr.com/gene/dfx/thumb.jpeg. I got it from Google, & maybe I should make my own to avoid copyright problems.

I saved it as an Encapsulated Post Script file, Post Script level 2, measuring 30 millimeters high by 40 millimeters wide, but I told the Gimp to preserve the aspect ratio, so the actual image might have a slightly differet size.

Here's what I would try if I had the picins package on all my LATEX computers.

\parpic[sr][r]{\includegraphics{thumb.ps}} I used the \parpic command of the picins package to place the image to the right of this paragraph.

I sure don't like the results. In the PDF output file, the image is taller than the paragraph that contains it, & LATEX does not compensate by placing vertical space between that paragraph & the one which follows. What's more, the image is not included at all in the HTML output.

6.2 Attempt 2, Partial Success

If I want text flow around the image in the HTML output, but I'll do without the image in the hard-copy version, I can use the \htmladdimg command to achieve it in the HTML version of the document. The printed version will not show the image at all, so the text will not flow around it.

361 In the HTML version, this paragraph flows around an image on the right. In the printed version, there is no image.

(New paragraph) It works fine.

7 Figures with borders

Here's how to put a border around a figure so the reader can more easily see that it's a figure & not part of the text. Such a border is particularly useful for figures whose content is mostly text (such as pseudocode).

This technique doesn't work well, so keep reading until you come to the ``usepackage float / boxed'' technique.

Figure [*] is a figure containing pseudocode & without a border.

Figure 3: Pseudocode without a border
\begin{figure}\begin{enumerate}
\item Given: Numbers {\em a} \& {\em b}.
\item...
... \Rightarrow \sqrt{c^{2}}$.
\item Return {\em c}.
\end{enumerate}
\end{figure}

Table [*] is an attempt at a bordered figure containing text by using a tabular environment inside the figure.

Figure 4: Pseudocode with a border
\begin{figure}\begin{center}
\begin{tabular}{\vert rl\vert} \hline
1. & Given:...
...}}$. \\
6. & Return {\em c}. \\ \hline
\end{tabular}\end{center}
\end{figure}

Here is the source code for the figure with the border.

    \begin{figure}
    \begin{center}
       \begin{tabular}{|rl|} \hline
       1. & Given: Numbers {\em a} \& {\em b} \\
       2. & Let $a^{2} \Rightarrow a \times a$ \\
       3. & Let $b^{2} \Rightarrow b \times b$. \\
       4. & Let $c^{2} \Rightarrow a^{2} + b^{2}$. \\
       5. & Let $c \Rightarrow \sqrt{c^{2}}$. \\
       6. & Return {\em c}. \\ \hline
       \end{tabular}
    \end{center}
    \caption{Pseudocode with a border}
    \label{fig-pseudocod-borderless}
    \end{figure}

Three bummers include:

  1. Since it's a table, I must number each row myself. (A good LATEX programmer could fix that, I'm sure.)
  2. Again since it's a table, multi-line entries could be difficult. I might have to insert line breaks.
  3. Again since it's a table, nested steps will be difficult.

7.1 usepackage float / boxed technique

Here's the best way I know so far to make a figure with a boarder.1

Figure 5 shows a figure with a border. Well, that's what it shows in the LATEX output, such as a DVI, PostScript, or PDF file. As of 2005 November 20, LATEX2HTML does not convert it correctly, so you see a regular figure with ``boxed figure'' before it & ``plain figure'' after it.

boxed figure

Figure 5: Pseudocode with a border in LATEX, with preceeding & following crap in HTML
\begin{figure}\begin{center}
\begin{enumerate}
\item Given: Numbers {\em a} \&...
...qrt{c^{2}}$.
\item Return {\em c}.
\end{enumerate} \end{center}
\end{figure}
plain figure

Let's get rid of the ``boxed figure'' & ``plain figure'' garbage in the HTML output. Figure 6 shows the result.

Figure 6: Pseudocode with a border in LATEX output, no border in HTML output
\begin{figure}\begin{center}
\begin{enumerate}
\item Given: Numbers {\em a} \&...
...qrt{c^{2}}$.
\item Return {\em c}.
\end{enumerate} \end{center}
\end{figure}
So we get a figure with a border in LATEX & a regular figure (often with a shaded background) in LATEX2HTML. It's not exactly what I wanted, but it's good enough.

Here's the source code for the previous figure:

\ begin{latexonly}
\floatstyle{boxed} \restylefloat{figure}
\ end{latexonly}
 \begin{figure}
 \begin{center}
    \begin{enumerate}
    \item Given: Numbers {\em a} \& {\em b}
    \item Let $a^{2} \Rightarrow a \times a$
    \item Let $b^{2} \Rightarrow b \times b$.
    \item Let $c^{2} \Rightarrow a^{2} + b^{2}$.
    \item Let $c \Rightarrow \sqrt{c^{2}}$.
    \item Return {\em c}.
    \end{enumerate}
 \end{center}
 \caption{Pseudocode with a border in \LaTeX\ output, no
 border in {\sc html} output}
 \label{fig-pseudocod-boxed2}
 \end{figure}
\ begin{latexonly}
\floatstyle{plain} \restylefloat{figure}
\ end{latexonly}

Here are all the instructions about using this technique:

  1. At the top of the document, put \usepackage{float}. Because it's only useful to LATEX, I guess you could wrap it in a latexonly environment.

  2. If you want all your figures to have borders, put a latexonly wrapper around `` \floatstyle{boxed} \restylefloat{figure}'' after the usepackage float. In fact, you could put all three commands on the same line & wrap them in latexonly.

  3. If you just want certain figures to have borders, then in front of such a figure, put a latexonly wrapper around ``\floatstyle{boxed} \restylefloat{figure}'' in front of the figure & ``\floatstyle{plain} \restylefloat{figure}'' after the figure.

A. Other File Formats

Bibliography

1
Patrick W. Daly.
Graphics and colour with
Max-Planck-Institut Für Aeronomie, June 1998.
http://tex.loria.fr/graph-pack/grf/grf.htm.

2
Michel Goossens and Frank Mittelbach.
The LATEX Companion.
Addison Wesley Longman, Inc., 1993.
ISBN 0201541998.

3
Michel Goossens and Sebastian Rahtz.
The LATEX Web Companion: Integrating TEX, HTML, and XML.
Addison Wesley Longman, Inc., 1999.
ISBN 020143317.

4
Leslie Lamport.
LATEX: A Document Preparation System.
Addison-Wesley Publishing Company, Inc., 1986.
ISBN 0-201-15790-X.

Gene Michael Stover 2008-04-20