Tag Archives: book

Create small TOCs/LOFs/LOTs using \minitoc

In large documents, it is helpful to add a “mini-table-of-contents” at the beginning of every chapter in order to get a better overview of the chapter. The minitoc-package provides a nice, easy-to-use and customizable layout for TOCs on a chapter level.

How to crate a minitoc:

\usepackage{minitoc}

Any minitoc can only be created if you have a “global” table-of-contents, its content is used for generation. So in your document, you first have to notify Latex you want to use minitoc and then create the overall table of contents:

\dominitoc
\tableofcontents

Now you can add minitocs at the beginning of your chapters:

\chapter{...}
\minitoc
\section{...}

Minilofs and minilots:

Similarly, \minilof and minilot will create list-of-figures and list-of-tables for your chapters.

Remarks:

Obviously, this only makes sense in books and reports, as in articles there are no chapters available. Here the minitoc-package provides additional commands for articles:

\secttoc
\sectlof
\sectlot

and to partition books:

\parttoc
\partlof
\partlot

The package documentation can be found at CTAN


Define your own “list of …”

I got this very interesting question lately, how to define a custom list of examples, similar to the list of figures. But as this is not only applicable to examples, but to all sorts of things (e.g. questions, theorems, proofs, lemmas, answers, etc.) and hence might be useful for other people, I decided publish a new post on that issue.

As usual, you will need to import a package.

\usepackage{tocloft}

The package lets you customise your table of contents, list of figures or list of tables (which is basically what the name stands for).

In addition, the package also lets you define your custom lists easily and hence is exactly what we were looking for.

In order to keep this as general as possible, I will use “X” instead of “example”, “proof”, etc. At the end, I will post a complete example, showing how to create a “list of examples”, which you can simply copy-paste and adapt for your needs.

After having added the package, we want to define the name that appears as a title of our list:

\newcommand{\listXname}{List of Xs}

Next we use the command provided by tocloft to define a new list of whatever:

\newlistof{X}{ex}{\listXname}

Now we define the actual X-command (similar to figure or table), which has a counter that is increased by one every time an X is used:

\newcommand{\X}[1]{%
\refstepcounter{X}
\par\noindent\textbf{X \theexample. #1}
\addcontentsline{exp}{example}
{\protect\numberline{\thechapter.\theexample}#1}\par}

By using the X-command (\X{Your text}), Latex will print the following:

X 1 Your text

The last line will add all your typed Xs to the list of Xs.

That’s it for the definition. The command “\listofX” will create a list of all your defined Xs similar to the ToC, LoF and LoT.

\begin{document}
\tableofcontents
\newpage
\listofX
\newpage
...
\end{document}

You can also add references to the different Xs, by adding a label just after the X-command (see example below):

Example: List of Examples

\documentclass{report}
\usepackage{tocloft}
\usepackage[english]{babel}
\newcommand{\listexamplename}{List of Examples}
\newlistof{example}{exp}{\listexamplename}
\newcommand{\example}[1]{%
\refstepcounter{example}
\par\noindent\textbf{Example \theexample. #1}
\addcontentsline{exp}{example}
{\protect\numberline{\thechapter.\theexample}#1}\par}
\begin{document}
\tableofcontents
\newpage
\listofexample
\chapter{Two examples}
\example{Your first example}
\label{1st_ex}
\example{Your second example}
\label{2nd_ex}
\chapter{One example}
\example{Your third example. (See example \ref{1st_ex} and \ref{2nd_ex})}
\end{document}

If you want to reset your example counter for new chapters, you would need to add the following lines before the beginning of your document:

\makeatletter
\@addtoreset{example}{chapter}
\makeatother

Click here for the complete tocloft documentation as well as the package.


Of theorems, lemmas and proofs

Different packages of Latex provide nice and easy-to-use environments for theorems, lemmas, proofs, etc. The following post will show you the mostly used layouts and how to change numbering. 
Theorem styles: For theorems, corollaries and lemmas, you need the following package:

\usepackage{amsthm}

which allows you to define new environments using either:

\newtheorem{env_name}{caption}[within]

or

\newtheorem{env_name}[numbered_like]{caption}

 where

  •  env_name: Environment name
  • caption: Text printed before the number, e.g. Theorem, Lemma, etc.
  • within: The name of a defined counter
  • numberered_like: Name of a defined theorem-like environment

Note: The command may at most have one optional argument. Let me give an example: First we define a new theorem “Theorem”, which takes the numbering of the section plus a consecutive number:

\newtheorem{thm}{Theorem}[section] 

For your theorems you can now use the defined environment:

\begin{thm}...\end{thm}

 For lemmas which have the same numbering as the previous thm-environment, we need to define a new theorem type:

\newtheorem{lem}[thm]{Lemma}

 Note: Environments different from thm, lem or cor need to be defined previously, having a different counter which starts from zero:

\theoremstyle{remark} 
\newtheorem{rem}{Remark}
\begin{rem}This is a remark.\end{rem}

Note: To omit the numbering, you can use the star as for chapters, sections, etc.:

\newtheorem*{lem}[thm]{Lemma}

Proof: A proof usually comes without a numbering as it follows a numbered theorem, corollary, etc. For a proof, you need the following package:

\usepackage{amsthm}

and then you can directly use the proof-environment:

\begin{proof}...\end{proof}

The closing statement automatically prints a qed-sign (square box) on an empty line after the last statement, terminating the proof.Note: If you want to place the qed-sign on the last line, i.e. if your proof ends with an equation, you can use the command:

\qedhere

inside the equation-environment (before the \end{equation}).


Add (Hyper-)Links to PDFs

Adding links to PDFs , both internal and external, certainly only makes sense for digital copies of the file. Nevertheless, they still come in handy, for example for actual WWW-addresses or as direct reference inside a document.

Local references

As usual, you first need the package which handles the links:

\usepackage{hyperref}

This does the job for the moment. The package “hyperref” will automatically insert links wherever you place a citation or reference (\cite{}, \ref{}).

Global references

Referencing to URLs or local files can be done as follows:

\href{url}{text}

Try this:

\href{http://texblog.wordpress.com}{\bf{Blog on Latex Matters}}

Colour references

So far, the references were marked by a colour rectangle around them, which is not very appealing (unless you are using Apple Preview). Here is how you remove the border and if you wish define your own colours for references.

Either add this optional argument to the previous package:

\usepackage[colorlinks]{hyperref}

or use a new line and set the colour:

\usepackage{hyperref}
\hypersetup{colorlinks}

If you want to define your own colours, you will have to use the latter, as the colours are part of the argument of “hypersetup”. But in order to do so, you first need to define colours, which is why you will need the colour-package:

\usepackage{color}

Then you can define different colours you may want to use for your references, e.g.:

\definecolor{darkred}{rgb}{0.5,0,0}
\definecolor{darkgreen}{rgb}{0,0.5,0}
\definecolor{darkblue}{rgb}{0,0,0.5}

Latex uses the standard RGB (red/green/blue) colour model. The three values define the intensity for each channel within the interval I=[0,1] (just because Latex looks nicer:-).

Now you are ready to use the defined colours, by extending the previously used command:

\hypersetup{ colorlinks,
linkcolor=darkblue,
filecolor=darkgreen,
urlcolor=darkred,
citecolor=darkblue }

Note: If your tex-distribution does not contain the hyperref-package, you can download it here (hyperref.zip), including the complete documentation.


Landscape in Latex

The default page layout is “portrait”, but sometimes it is still useful/necessary to have the whole document or only single pages changed to “landscape”. The latter might be due to a large table or figure. This post will tell you how to change the page layout of the whole document or single pages to “landscape”. In addition, it is possible to make single pages appear left side up in the PDF, making them more readable.

Changing the whole document to “landscape” can be done be using the geometry-package:

\usepackage[landscape]{geometry}

You can also just change the page content to landscape, but not the actual page layout through the optional argument of the command “documentclass”. It does not make much sense, but you can do it:

\ documentclass[landscape, 12pt]{report}

Next I will show you how to change the page layout of single pages. The lscape-package provides according possiblities:

\usepackage{lscape}

With

\begin{landscape}
...
\end{landscape}

you define the section of your document to be set to “landscape”, e.g. a large table or figure.

This will not automatically rotate the page in the PDF and is useful if the document is destined for printing.

If you want to make appear the left side up, better readable on screen, the pdflscape-package will do it:

\usepackage{pdflscape}

and again:

\begin{landscape}
...
\end{landscape}

for the page to be “landscape”, while the rest will remain in “portrait” orientation. Nevertheless, the header/footer will also be changed in orientation.


Header/Footer in Latex with Fancyhdr

Creating headers and footers in Latex can be done by using the package “fancyhdr”. This is a short introduction, showing the most important features of the package. If you know “fancyhdr” and are looking for something particular, refer to the fancyhdr-documentation.

First of all, you need to tell Latex to use the package:

\usepackage{fancyhdr}

and change the style from “plain” to “fancy”:

\pagestyle{fancy}

You will now the get the default fancy pagestyle which adds a line at the top of every page, except for some exceptions (title-page, abstract, new chapter in report).

Default fancyhdr page style:

Above the line, Latex will print headings:

Book/report

Left-hand side: section
Right-hand side: chapter
Note: if you use the optional documentclass argument “twoside”, Latex will alter the position of the section and chapter. (e.g. \documentclass[twoside]{report}, also introducing non-symmetric margins).

Article

For acticles, Latex will print the section only (chapters cannot be used with articles).
The footer only includes the page number which is centered by default.

Custom fancyhdr page style:

Even though fancyhdr has a default page style, you are free to define headers/footers yourself , which is not too difficult after all.

First you need to clear the default layout:

\fancyhead{}
\fancyfoot{}

There are seven letters you need to know before you can define your own header/footer:

E: Even page
O: Odd page
L: Left field
C: Center field
R: Right field
H: Header
F: Footer

Now it’s time to start defining your own layout. The definitions are added to the preamble:

\fancyhead[CO,CE]{---Draft---}
\fancyfoot[C]{Confidential}
\fancyfoot[RO, LE] {\thepage}

The decorative lines can be changed by increasing/decreasing their thickness (0pt means no line):

\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

Note: According to the fancyhdr-documentation, the default layout is produced by the following commands:

\fancyhead[LE,RO]{\slshape \rightmark}
\fancyhead[LO,RE]{\slshape \leftmark}
\fancyfoot[C]{\thepage}
\headrulewidth 0.4pt
\footrulewidth 0 pt


Flush figure before end of page

Usually figures are placed wherever there is enough space, e.g. for “begin{figure}[ht]” at the beginning of the next page. What if you want to place the figure within the text, because you have other figures which you do not want to place only at the beginning of the second next page…

There is a little trick, which lets Latex place figures before the end of a section by using an exclamation mark (\begin{figure}[h!t]), as long as there is enough space on that page.
The exclamation mark let’s you place figures more precisely in the text and is more often useful than not. It can be used to the same extend for tables.


Problem with label for tables having small text-size

I recently had the following problem, I was using a label to reference a table where I placed the “tabular”- environment into the “small”- environment to decrease its size. The number indicated by the reference was not the table index, but rather the number of the section/subsection within which I placed the table. The problems seems to exist in both, Miktex and MacTex…

A table ususally has the following structure (for an introduction to tables click here) with the “small”- environment added to slightly decrease its size:

\begin{table}[htdp]
\caption{default}
\begin{small}
\begin{center}
\begin{tabular}{|c|c|}
\end{tabular}
\end{center}
\end{small}
\label{default}
\end{table}

Placing the label inside both, “small”- and “center”- environment solves the problem.
Hence:

\begin{table}[htdp]
\caption{default}
\begin{small}
\begin{center}
\begin{tabular}{|c|c|}
\end{tabular}
\label{default}
\end{center}
\end{small}
\end{table}

I actually don’t know why Latex does not translate the reference correctly, as the label is still within the “table”-environment, at least the problem can be solved that way.


Creating two columns in article, report or book

Three different styles have to be distinguished when creating multiple columns in a Latex document. Either we want the whole document to have two columns, single pages or only part of a page. In order to do so, three different Latex commands are used…

Whole document (using article to write a paper):

The only thing you need to do is changing the first command of your Latex-file.

\documentclass[11pt,twocolumn]{article}

It will automatically create two columns in the entire document.

Note: if you are writing a paper, IEEE provides useful templates which can be used and adapted to your needs. You can download them from their “Author Digital Tool Box“.

Single pages:

The command \twocolumn starts a new page having two columns. Accordingly, \onecolumn starts a new page with a single column assuming you are in a two column environment as described above. Both commans do not take any arguments.

The is a way to define the distance between the two columns, use

\setlength{\columnsep}{distance}

If you need a line to separate the columns, the following command will do the job:

\setlength{\columnseprule}{thickness}

Part of a page:

I have posted another article on that, just have a look there. \minipage can also be used for text, not only for figures and tables.


How to use Bibtex as a reference library for Latex

Having all your references in a BibTeX-file (*.bib) is more convenient for reusage than typing or copy the whole list of references everytime you are writing a new book, report or article. Therefore, it is worth to learn how to use BibTeX from the beginning, it will save you a lot of time…

BibTeX offers a whole list of entry fields, entry types and different bibliography styles.
Entry fields (standard):

  • address: Publisher’s address (usually just the city, but can be the full address for lesser-known publishers)
  • annote: An annotation for annotated bibliography styles (not typical)
  • author: The name(s) of the author(s) (in the case of more than one author, separated by and)
  • booktitle: The title of the book, if only part of it is being cited
  • chapter: The chapter number
  • crossref: The key of the cross-referenced entry
  • edition: The edition of a book, long form (such as “first” or “second”)
  • editor: The name(s) of the editor(s)
  • eprint: A specification of an electronic publication, often a preprint or a technical report
  • howpublished: How it was published, if the publishing method is nonstandard
  • institution: The institution that was involved in the publishing, but not necessarily the publisher
  • journal: The journal or magazine the work was published in
  • key: A hidden field used for specifying or overriding the alphabetical order of entries (when the “author” and “editor” fields are missing). Note that this is very different from the key (mentioned just after this list) that is used to cite or cross-reference the entry.
  • month: The month of publication (or, if unpublished, the month of creation)
  • note: Miscellaneous extra information
  • number: The “number” of a journal, magazine, or tech-report, if applicable. (Most publications have a “volume”, but no “number” field.)
  • organization: The conference sponsor
  • pages: Page numbers, separated either by commas or double-hyphens
  • publisher: The publisher’s name
  • school: The school where the thesis was written
  • series: The series of books the book was published in (e.g. “The Hardy Boys”)
  • title: The title of the work
  • type: The type of tech-report, for example, “Research Note”
  • url: The WWW address
  • volume: The volume of a journal or multi-volume book
  • year: The year of publication (or, if unpublished, the year of creation)

Entry fields (non-standard):

  • affiliation: The authors affiliation.
  • abstract: An abstract of the work.
  • contents: A Table of Contents
  • copyright: Copyright information.
  • ISBN: The International Standard Book Number.
  • ISSN: The International Standard Serial Number. Used to identify a journal.
  • keywords: Key words used for searching or possibly for annotation.
  • language: The language the document is in.
  • location: A location associated with the entry, such as the city in which a conference took place.
  • LCCN: The Library of Congress Call Number.
  • mrnumber: The Mathematical Reviews number.

In addition, each entry contains a key that is used to cite or cross-reference the entry. This key is the first item in a BibTeX entry, and is not part of any field.

Entry types:

  • @article

An article from a journal or magazine.
Required fields: author, title, journal, year
Optional fields: volume, number, pages, month, note, key

  • @book

A book with an explicit publisher.
Required fields: author/editor, title, publisher, year
Optional fields: volume, series, address, edition, month, note, key

  • @booklet

A work that is printed and bound, but without a named publisher or sponsoring institution.
Required fields: title
Optional fields: author, howpublished, address, month, year, note, key

  • @conference

The same as inproceedings, included for Scribe (markup language) compatibility.
Required fields: author, title, booktitle, year
Optional fields: editor, pages, organization, publisher, address, month, note, key

  • @inbook

A part of a book, which may be a chapter (or section or whatever) and/or a range of pages.
Required fields: author/editor, title, chapter/pages, publisher, year
Optional fields: volume, series, address, edition, month, note, key

  • @incollection

A part of a book having its own title.
Required fields: author, title, booktitle, year
Optional fields: editor, pages, organization, publisher, address, month, note, key

  • @inproceedings

An article in a conference proceedings.
Required fields: author, title, booktitle, year
Optional fields: editor, pages, organization, publisher, address, month, note, key

  • @manual

Technical documentation.
Required fields: title
Optional fields: author, organization, address, edition, month, year, note, key

  • @mastersthesis

A Master’s thesis.
Required fields: author, title, school, year
Optional fields: address, month, note, key

  • @misc

For use when nothing else fits.
Required fields: none
Optional fields: author, title, howpublished, month, year, note, key

  • @phdthesis

A Ph.D. thesis.
Required fields: author, title, school, year
Optional fields: address, month, note, key

  • @proceedings

The proceedings of a conference.
Required fields: title, year
Optional fields: editor, publisher, organization, address, month, note, key

  • @techreport

A report published by a school or other institution, usually numbered within a series.
Required fields: author, title, institution, year
Optional fields: type, number, address, month, note, key

  • @unpublished

A document having an author and title, but not formally published.
Required fields: author, title, note
Optional fields: month, year, key

[Source: http://en.wikipedia.org/wiki/BibTeX]

Example bibliography entry in bib-file:

@article{zhou2005ada,
title = {Adaptive Successive Erosion-based Cell Image Segmentation for p53 Immunohistochemistry in Bladder Inverted Papilloma.},
author={Zhou, H. and Mao, K.},
journal={Conf Proc IEEE Eng Med Biol Soc},
volume={6},
year={2005},
}

Cross-referencing:

It is possible to cross-reference different entries by using the crossreference field, e.g. crossref = {zhou2005ada},

Usage in Latex:

Using your BibTeX-file in Latex is done by specifying a style: \bibliographystyle{style} as well as the bib-file \bibliography{filename1, filename2} (without the bib-extension, just the name). The most commonly used styles are: plain, acm, ieeetr, alpha, abbrv, siam.

Many other styles are available, see here for an extensive list.

PDF generation:

Generating your PDF is a bit of a hassle, as you have to run Latex several times, you need to run the following series of commands:

  1. latex input-file : complains about undefined citations
  2. bibtex input-file : generates a bbl-file
  3. latex input-file : complains about undefined citations
  4. latex input-file

You will have to repeat the procedure every time you add or remove citations, as they will imply changes in the bbl-file.

Note:

  • If you have been using Endnotes so far and want to switch to LaTeX/BibTeX, you won’t need to retype you whole references-database again. It is possible to create a BibTeX-references file from your library in Endnotes.
  • Always look for bibtex-entries on the web. You can sometimes find them in article libraries or on the web page of the authors. They are complete and will save you a lot of time searching and typing the data.

Follow

Get every new post delivered to your Inbox.

Join 316 other followers