Search This Blog

Thursday, October 25, 2012

Fancy LaTeX chapter styles

Fancy LaTeX chapter styles

Many books, theses and reports are written in LaTeX using the report or book document classes. Often, the authors make use of the default chapter style. There are, however, a great number of alternative styles available, some of which being very fancy or playful. This post is a collection of alternative chapter styles available, some as packages, others simply in form of LaTeX code.

Default chapter style
The default chapter style is often used in (academic) books and theses. Here is an example:
1\documentclass{report}
2\begin{document}
3\chapter{Default Chapter Heading}
4\end{document}


Package titlesec
The titlesec package allows basic changes to the standard chapter style, including setting the font style and size or placement of the title. You can do great things with titlesec package and the titleformat command in particular, just be creative (and let me know below)!
Here is a neat example:

01\documentclass{report}
02\usepackage[T1]{fontenc}
03\usepackage{titlesec, blindtext, color}
04\definecolor{gray75}{gray}{0.75}
05\newcommand{\hsp}{\hspace{20pt}}
06\titleformat{\chapter}[hang]{\Huge\bfseries}{\thechapter\hsp\textcolor{gray75}{|}\hsp}{0pt}{\Huge\bfseries}
07\begin{document}
08\chapter{Less is More}
09\blindtext
10\end{document}
See the documentation for more details.

Package fncychap
The fncychap package has a nice set of predefined chapter styles. The style is set through the optional argument when loading the package. Available styles include: Sonny, Lenny, Glenn, Conny, Rejne, Bjarne, and Bjornstrup. The package documentation has examples for all available styles. The package will use the LaTeX default chapter style in case the optional argument is not set (i.e. \usepackage{fncychap}).
1\documentclass{report}
2%Options: Sonny, Lenny, Glenn, Conny, Rejne, Bjarne, Bjornstrup
3\usepackage[Sonny]{fncychap}
4\begin{document}
5\chapter{Sonny}
6\end{document}
Examples of Glenn and Bjornstrup chapter styles provided from the fncychap package:



Chapter styles with memoir
The memoir document class is more flexible in terms of chapter styles than report or book.
I will not go into detail since there is extensive documentation on the memoir document class (see page 83 onwards). Furthermore, there is additional documentation describing the memoir chapter styles including code examples and output. Below you’ll find one of the examples taken from that document named hansen.


Styling the chapter
Henrik Stuart wrote this article in 2007 with a series of great chapter styles. They are also based on the memoir document class and the PGF/TikZ package. Here is an example that I like a lot, simply beautiful!

LaTeX Notes: Structuring Large Documents

LaTeX Notes: Structuring Large Documents


As soon as you start to produce documents which have multiple chapters, or documents which are of any decent size, keeping all the source text in one file becomes unmanageable. There are two basic methods you can use for managing large documents; the first is very easy to use but limited in usefulness, so we'll get that out of the way first. In each case the idea is that you have some top level document file and a number of files that get included in this file automatically when you run LaTeX.

1 The Simple Method

Using the simple method, you set up a top level document like this:
\documentstyle{...}
...
\begin{document}
\input{firstfile}
\input{secondfile}
...
\input{lastfile}
\end{document}
You then segment your text into chunks, which you keep in the files firstfile.tex secondfile.tex and so on; each of these might be a chapter, or a major section. So, firstfile.tex might look like the following (note the use of the macro programname here; this isn't standard LaTeX, it's an instance of declarative formatting):
\section{Introduction}
Way back in the beginning of time,
people used a text formatter
called \programname{nroff} ...
When you run LaTeX on the top-level file, the contents of firstfile.tex, secondfile.tex and so on will then be read in at the specified points. This simply makes it easier to handle your text by breaking it into smaller chunks. Note the following:
  • The name of each included file must actually be something.tex, since LaTeX will automatically add the .tex ending when it looks for the file.
  • You can have nested calls of this sort---i.e., the file firstfile.tex could itself simply be something like:
    \chapter{Introduction}
    There are two sections to this introductory chapter.
    \input{firstsection}
    \input{secondsection}
    
  • From the previous example you can see that:
    • Each inputted file {\em isn't} a standalone LaTeX file (in particular, it doesn't have a \documentstyle{...} line or the \begin{document} and \end{document} lines).
    • You can intersperse calls to input with other arbitrary text and LaTeX commands.
This method is limited, in the following ways:
  1. If your intention is to avoid printing the whole file every time you format it, you have to explicitly comment out (or delete) the \inputs you don't want, and this has the consequence that page numbers, section numbers and so on will only take account of the non-commented-out input files (i.e., if you comment out the \input{firstfile} in the example above then secondfile will start on page 1 as Section 1).
  2. Worse, if you have cross-references betwen the different input files (e.g., suppose secondfile includes a \ref that refers to a \label in firstfile) then LaTeX won't be able to resolve the references.
As a result, this method is best suited to keeping stuff like figures and pictures in separate files, thus making the editing of the actual text less distracting. So, if you have a very complex figure constructed using the LaTeX picture environment, you might put it into a separate file and then include it in the following way:
You can see from the really complex figure 
in Figure~\ref{complex-figure} that my theory
is better than yours.
\begin{figure}
\input{myfigure1}
\caption{My really complex figure}\label{complex-figure}
\end{figure}

2 The More Complex Method

The more complex method of managing large documents is similar to the above, except that you use the \include command:
\documentstyle{...}
...
\begin{document}
\include{firstfile}
\include{secondfile}
...
\include{lastfile}
\end{document}
Again, LaTeX looks for firstfile.tex, and so on. Now, however, each included file gets its own .aux file. This is very important, because you can then do the following:
\documentstyle{...}
...
\includeonly{secondfile}
...
\begin{document}
\include{firstfile}
\include{secondfile}
...
\include{lastfile}
\end{document}
This tells LaTeX to consult the aux files corresponding to each included file, but only to actually include the text of the files listed in the \includeonly line. Because LaTeX looks at the other aux files, it knows about section and page numbers, cross-references, and so on. This means that the output will start at the appropriate page for the text in secondfile.tex, with appropriate section numbers and so on. Simply by changing the \includeonly line and reformatting, you can get different parts of the entire document printed, with all the numbering being that which you would get had you printed the entire document. One potential disadvantage of this method is that, unlike \input, each included file will automatically begin on a new page: so you don't want to use this for small arbitrary bits of a document (such as the example of an inputted figure in the previous section), but probably only for individual sections, or, if they are pretty large and deserve to start on a new page, individual subsections. In a document that consists of multiple chapters, each chapter will start on a fresh page anyway; so you can use this method to keep the text of individual chapters in separate files. For texts where you want to keep individual sections in separate files, one approach is to develop a large document using \includes and then for the final printing change them all to \inputs. So, while a text is being written, each chapter might be an included file which consists of multiple included sections; when any particular sections are printed as a result of being mentioned in the includeonly line, each section will start on a new page. For the final text, the section \includes can be replaced by \inputs so that only the chapters start on new pages.
Note that you can have multiple files specified in the includeonly line, but you have to specify the names separated by commas {\em with no intervening spaces}. So this is okay:
\includeonly{firstbit,lastbit}
but this is not:
\includeonly{firstbit, lastbit}
From the previous example you can see that you can format discontinuous parts of the text. Don't forget that LaTeX can only take account of aux files corresponding to files that are included, but not mentioned in the includeonly line, provided those aux files exist, so you have to format each bit (or all the bits at once by specifying them all in the includeonly line) at least once first.

Latex Sectioning

Sectioning commands provide the means to structure your text into units.
  • \part
  • \chapter (report style only)
  • \section
  • \subsection
  • \subsubsection
  • \paragraph
  • \subparagraph
  • \subsubparagraph (milstd and book-form styles only)
  • \subsubsubparagraph (milstd and book-form styles only)
All sectioning commands take the same general form, i.e.,
\chapter[optional]{title}
In addition to providing the heading in the text, the mandatory argument of the sectioning command can appear in two other places:
  1. The table of contents
  2. The running head at the top of the page
You may not want the same thing to appear in these other two places as appears in the text heading. To handle this situation, the sectioning commands have an optional argument that provides the text for these other two purposes.
The "sectioning commands" have *-forms that print a title, but do not include a number and do not make an entry in the table of contents. For example, the *-form of the \subsection command could look like:
\subsection*{Example subsection}

Linked list in C++

What is a linked list?

In any programming language, you can create something called a linked list. This is one of many essential data structures. Other structures are stacks, queues and deques. These will be discussed later on in the other tutorials.
Lets look at the term linked list and break it down. The word list is obvious in the respect of having a list of data. For example, you may have a list like this:
Fruits:
1. Apples
2. Oranges
3. Pears
4. Mangos
5. Peaches

etc ...
Now the term linked. This means that the list will be tied together. In some respects, it may look like this:
Apples --> Oranges --> Pears --> Mangos --> Peaches
Each item in the list is linked to another.

Nodes

These are the elements that make it possible to have a linked list. Without them, there would be no listing.
Each node contains two parts; a part for the data and a part for the address of the next element. As you can see, this will use pointers.
Here is the class for the nodes. An explination will follow.
template
class node{

private:
 T Data;
 node* Link;

public:
 //constructor
 node(){Link = 0;}
 node(T d) { Data = d; Link = 0; }

 //accessors
 T& data(){ return Data; }
 node*& link(){ return Link; }

};
A short class indeed but a lot has happened.
First, the private section. These will contain the elements for the data and the link to the next node. They are both a template of type T which can allow more than one data type. This was discussed in the templates tutorial. The reason the Link variable is of type node is because it will be pointing to another node in memory.
Now, the constructor. The default constructor will simply put the Link to 0 (or NULL) since you have not placed any other nodes in the list yet.
The other constructor will set the Data to the parameter d in addition to the Link to NULL. Again, you have not linked to another node yet.
Now the accessors and mutators. The two functions you see will act as both. First, the data() function will return the data (acting as the get function) in addition to allow you to change the Data (acting as the set) since you are referencing.
Second, the link() function will return the link to you but allow you to change the link. For example you may need to remove or insert something in a list and therefore change the link to that node.

Putting it together

Here is a small example that shows a linked list of characters. This will print each element of the list.

Example 1:
Simple Linked List

Download source code here (Right click - Save Tagret As...)
#include 
using namespace std;

template
class node{

private:
 T Data;
 node* Link;

public:
 //constructor
 node(){Link = 0;}
 node(T d) { Data = d; Link = 0; }

 //accessors
 T& data(){ return Data; }
 node*& link(){ return Link; }

};

int main(){

 node a('a'), b('b'), c('c');

        //give the address to the link:
 a.link() = &b;
 b.link() = &c;

        //print each element:
 cout << a.data() << endl;
 cout << b.data() << endl;
 cout << c.data() << endl;

        //the list looks like this:
        //a --> b --> c

return 0;
}

Transversing the list

When you transverse a linked list, it means that you are looking at each element in the list. With the list example above, you are starting at the beginning of the list with the cout statements.
We can write a function that handles the printing instead of using an x number of cout statements in the main(). The function is below:
template 
void print (node* p){

     //as long as the list is not empty:
     while(p){
 cout << p -> data();
 p = p -> link();
     }

}
The argument is a pointer that will begin at some point in the list. You can decide where it begins. The while loop will keep going until the pointer is 0 (or NULL). Remember, a while loop will run with any number so long as it is not 0.
Now, let's append the above program and see the function at work:

Example 2:
Simple Linked List II

Download source code here (Right click - Save Tagret As...)
#include 
using namespace std;

template
class node{

private:
 T Data;
 node* Link;

public:
 //constructor
 node(){Link = 0;}
 node(T d) { Data = d; Link = 0; }

 //accessors
 T& data(){ return Data; }
 node*& link(){ return Link; }

};

template 
void print (node* p){

     //as long as the list is not empty:
     while(p){
 cout << p -> data();
 p = p -> link();
     }

};

int main(){

 node a('a'), b('b'), c('c');

        //give the address to the link:
 a.link() = &b;
 b.link() = &c;

        //start at beginning:
        print(&a);

        //the list looks like this:
        //a --> b --> c

return 0;
}
The function will begin printing from the first node. Had you given the print function a NULL node, it would not even print anything based on the while statement.