Tuesday, August 08, 2006

Some LaTeX2e tricks



This documents were adapted from the contents searched from internet for personal use ONLY. This blogger is not intended for public use.



Contents






Adjusting space between references in the
bibliography.



LaTeX normally inserts extra space between each
item in the bibliography. A quick but ugly way of removing this space
is by setting \itemsep to -2mm (experiment to find the
right length) inside the thebibliography environment:

\begin{thebibliography}\setlength{\itemsep}{-2mm}

This is not practical if bibTeX is used, as the
thebibliography environment will be in the
.bbl file, which is overwritten each time bibTeX is run.
A better solution is a redefinition of thebibliography.
I have cut out and modified the original definition, just include the
bibspacing.sty package to
set the spacing to zero.
Another spacing may be selected by setting the
\bibspacing length. To set the spacing to one line, use:

\usepackage{bibspacing}
\setlength{\bibspacing}{\baselineskip}

NOTE: If you use the natbib package, you should
use natbibspacing.sty
instead, and be sure to load it
after natbib:

\usepackage{natbib,natbibspacing}


Adjusting spacings etc. in
itemize, enumerate and description
environments.



The standard listmaking environments are defined as special versions
of the list environment. The list
environment contains a large number of adjustable parameters, setting
the margins, spacing between items etc. etc. Some of these, such as
\itemsep can be set immediately after the
\begin command:

\begin{itemize}\addtolength{\itemsep}{-0.5\baselineskip}

Others, such as \topsep, cannot be adjusted in this way
as it is used inside the \begin statement. Anyway, in
the spirit of LaTeX these spacings should be adjustable in a global
way.



Solution: The
tweaklist.sty package redefines
the itemize, enumerate and
description packages, so that all parameters can be
adjusted. This was done by copying the original definitions, and
adding "hook commands" that are executed when entering the
environment. The hook commands are initially empty, but can be
redefined with \renewcommand.



Example: to set \topsep and \itemsep to 0 in
the enumerate environment, use:


\usepackage{tweaklist}
\renewcommand{\enumhook}{\setlength{\topsep}{0pt}%
\setlength{\itemsep}{0pt}}


The following hook commands are defined: enumhook for the
enumerate environment, itemhook for the
itemize environment, and descripthook for
the description environment.



LaTeX keeps track of nested enumerate and itemize environments. If
you only want to modify a specific nesting level, you should not use
enumhook or itemhook. Special hooks are
defined that are only called at the specific level. For the
enumerate environment they are enumhooki,
enumhookii, enumhookiii, and
enumhookiv. For the itemize environment
they are itemhooki, itemhookii,
itemhookiii, and itemhookiv. The
level-specific hooks are called after the global hook, so they can
redefine a setting in the global hook. As LaTeX does
not keep track of the nesting level of description
environments, there are no level-specific hooks for that
environment.



For a list of which parameters you can change, see the section on
lists in a LaTeX book. Suggestions:


  • Leslie Lamport: LaTeX (Figure 5.3 on page 113 in the
    1st edition).
  • Gossens, Mittelbach and Samarin: The LaTeX companion (Figure 3.5
    on page 62).


Redefining the font used in figure and
table captions.



Often you want figure captions in a different font than the main text
(for example slightly smaller, or in italics). Adding
e.g. \small in the beginning of each
\caption almost works, but there are a few
problems.


  1. The label (Figure 27:) is not affected by the font changing
    command. This may be fine in some cases, but will look a bit strange
    if the font size is changed.

  2. If you use a list of figures, the font change command will be included
    in it, unless the optional argument to \caption is used
    to make a different (shorter) caption for the list of figures.

  3. You may want a way to set the font globally, so it is easy to change
    (and so you don't forget the font command in figure 27).


The solution is adding this to the preamble of your document.

% Different font in captions
\newcommand{\captionfonts}{\small}

\makeatletter % Allow the use of @ in command names
\long\def\@makecaption#1#2{%
\vskip\abovecaptionskip
\sbox\@tempboxa{{\captionfonts #1: #2}}%
\ifdim \wd\@tempboxa >\hsize
{\captionfonts #1: #2\par}
\else
\hbox to\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\vskip\belowcaptionskip}
\makeatother % Cancel the effect of \makeatletter

Fortunately, you don't have to understand it to use it! Change the
first line to choose another font change. NOTE: I cannot get
this to work with new-style font commands, so use the old style in the
\captionfont definition, i.e. use \em instead of
\emph.

Redefining the label used in
captions.



Do you want "Fig. 1" instead of "Figure 1" in all the captions? This
is easy, just use

\renewcommand{\figurename}{Fig.}

For tables, redefine \tablename.


Preventing figures from appearing on a
page by themselves.



LaTeX's figure placement algorithm is quite biased in favor of putting
figures on a page by themselves, instead of on the top of a page with
some text below it. Often, I find the result esthetically unappealing
(to be polite). Fortunately, the parameters of the algorithm can be
changed. The main problem is that LaTeX per default only allows a
part of the top of a text-page (70%) to contain figures, and
requires at least 20% of a page to be text when text and figures
share a page. These parameters should be set to more reasonable values,
for example 85% and 10%.

\renewcommand{\topfraction}{0.85}
\renewcommand{\textfraction}{0.1}

This helps, but sometimes LaTeX puts a figure on a page by itself,
although it would fit perfectly well on the top of a page. This
happens when the figure will not fit on the page where it was
defined. LaTeX then attempts to put it on a figures-only page
before it attempts to put it at the top of the next page. A
page may contain figures alone if the figure(s) use at least half the
page. To prevent half-empty pages this limit should probably be
increased to around 75%.

\renewcommand{\floatpagefraction}{0.75}

Be careful not to make \floatpagefraction larger than
\topfraction, then you risk to produce a figure that can
neither go on the top of a text page, nor on a page by itself. If
that happens, the figure and all later figures will be
postponed until next time a \clearpage is executed
(typically at the end of a chapter or the end of the document). This
will also happen if a figure is too large to fit on a page.

Putting the figure and the caption side
by side.


Normally the caption goes below or above the figure, but you may want
it to the left or the right instead. The minipage
environment is useful. The following example puts the figure on the
left side using 58% of the with, and the caption on the rigth using
38% of the with. The remaining 4% of the page width ends up
separating the two due to the \hfill command.

\begin{figure}
\begin{center}
\begin{minipage}[t]{0.58\linewidth}
\epsfig{file=somefile.ps, width=\linewidth}
\end{minipage}\hfill
\begin{minipage}[t]{0.38\linewidth}
\caption{This is the caption.\label{fig:rawss}}
\end{minipage}
\end{center}
\end{figure}

Note the placement of the \label command, it must appear
inside the \caption command, or after it but still inside
the minipage environment. Otherwise references to the
figure will be wrong.



The vertical alignment of the figure and the caption may be
troublesome, you may have to put the figure inside a
\raisebox command.


\raisebox{2cm}{\epsfig{file=somefile.ps, width=\linewidth}}

Or change the [t] optional argument to the minipage
environments to [c] or [b]. Experiment!

Including PostScript graphics


Including PostScript graphics can be done in many ways, and there are
many pitfalls. The following systematic way usually works, it is
based on the \epsfig command, which calls
\includegraphics. You may prefer to use
\includegraphics directly, or to use the
\epsf package, it is a matter of taste.


  1. Include the package: \usepackage{epsfig}

  2. The PostScript picture should contain a well formed bounding box
    comment indicating where on the paper the figure appears if it is
    printed, so LaTeX can calculate how to scale and move it to fit where
    you want it. Many programs produce figures with wrong bounding
    boxes.



    The bbox command can calculate the bounding
    box, it works in 95% of the cases. You can also measure it yourself
    by printing it or using ghostview
    (preferred). The bounding box command is in the beginning of the file
    and looks like this:

    %%BoundingBox: 0 10 612 792

    this means that the figure fits inside a rectangle with lower left
    corner at (0,10) and upper right corner (612,792). The unit is 1/72
    inches (.3528 mm) measured from the lower left corner of the paper.
    Ghostview shows the coordinates of the cursor in the upper left
    corner, it makes it easy to measure bounding boxes.


    If the bounding box comment is incorrect, fix it or use the
    bb= argument to \epsfig.

  3. Include the figure where you want it using a command like

    \epsfig{file=graph.ps, angle=-90, width=\linewidth}

    this command includes the figure in graph.ps, rotates it 90 degrees
    clockwise (good for Xmgr graphs) and scales it to the current line
    width. LaTeX automatically reserves space for the figure


Useful arguments to \epsfig:

  • bb=0 10 612 792 Override the bounding box comment in the
    file.
  • clip= Clip anything in the file outside the bounding box.
    Particularly useful if the figure contains a white background that
    otherwise will overwrite some text. Note that the '=' should be there
    although nothing follows it.
  • angle=45 Rotate the figure 45 degrees anticlockwise.
  • width=5cm Scale the figure so it becomes 5cm wide. Often
    it is useful to give the length as a fraction of the linewidth,
    e.g. width=0.5\linewidth
  • totalheight=3cm Scale the figure so it becomes 3cm high.


The order of arguments is important, the actions are performed in the
order specified, so if you scale to a specific width and then rotate,
you have in effect scaled to a specific height!



Note when rotating figures: In TeX a box has three dimensions: width,
height and depth. Height is how much it goes above the baseline,
depth how much it goes below. Epsfig rotates around the lower left
corner, so if you rotate by -90 degrees the entire figure ends up
below the baseline, i.e. the height is zero but it has a depth. For
this reason the height= will not work, but
totalheight= does. You may want to use
\raisebox to move it above the line.



Examples:



  • Including an Xmgr graph: The bounding box is normally wrong, use
    bbox to measure it and fix it.

    \epsfig{file=graph.ps, angle=-90, width=0.7\linewidth}


  • Including an ARTview figure: The bounding box is often too big,
    as it includes the size of the white background (Note: It is
    technically correct to include the background, but not practical when
    including in LaTeX). Measure the dimension with bbox.
    \epsfig{file=a0000.ps,bb=48 62 505 486,clip=,width=8cm}

    The clip= is necessary to prevent the white background
    from causing trouble.


Discourage hyphenation.


When TeX splits your text into lines it tries to do it the "least
ugly" way, by assigning penalties to things that don't look nice (such
as words split over two lines). The penalty for splitting a word is
rather low, so you may want to increase it when writing texts where
split words look bad (in particular posters and viewgraphs). This
will produce lines with a little more extra spacing between words, and
you should therefore increase TeX's tolerance for such lines. I have
found the following values to be useful (put it in the preamble).

\hyphenpenalty=5000
\tolerance=1000

You may have to experiment a bit (adjusting \tolerance
appears to be most promising). A \hyphenpenalty of 10000
(almost) prevents hyphenation, but produces overlong and/or ugly lines.

Where did I find these tricks?


Most of what this page contains come from one of the following
sources. You can look there yourself for solutions to similar
problems.


  • The book "The LaTeX Companion" by Goosens, Mittelbach and Samarin
    (Addison Weskey, 1994).

  • The
    TeX
    FAQ
    . Frequently asked questions about TeX and LaTeX.

  • The archive of comp.text.tex on
    DejaNews. Comp.text.tex is a NetNews
    discussion group where people discuss TeX/LaTeX. It is highly
    probable that someone already discussed your problem, so you can find
    the solution in DejaNews' database. Go to
    DejaNews, select Power Search,
    write comp.text.tex in the Forum field and some keyword(s) in the
    Find field. Press Find. Good luck....

  • If you are really desperate, you may try to post you
    question on comp.text.tex, maybe
    somebody somewhere in the world will be nice and answer after a few
    days. Or maybe you'll just be added to a lot of junk email lists :-(





Labels: