Titlesec Bibliography Generator

Documents usually have some levels of chapters and/or sections to keep its contents organized. LaTeX supports this type of organization and also customization of the sectioning and numbering.

Introduction

Commands to organize a document vary depending on the document type, the simplest form of organization is the sectioning, available in all formats.

\documentclass{article}\usepackage{blindtext}\usepackage[utf8]{inputenc}   \title{Sections and Chapters}\author{Gubert Farnsworth}\date{\today}   \begin{document}   \maketitle   \section{Introduction}   This is the first section.   Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortisfacilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdietmi nec ante. Donec ullamcorper, felis non sodales...   \section{Second Section}   Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisissem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi necante...   \end{document}

The command marks the beginning of a new section, inside the braces is set the title. Section numbering is automatic and can be disabled.

Open an example in ShareLaTeX

Document Sectioning

LaTeX can organize, number, and index chapters and sections of document. There are up to 7 levels of depth for defining sections depending on the document class:

 -1 0 1 2 3 4 5

and are only available in report and book document classes.

Open an example in ShareLaTeX

Numbered and unnumbered sections

To get an unnumbered chapter, section, sub-section, etc. add an asterisk before the opening curly brace. These will not go into the table of contents.

\documentclass{article}\usepackage{blindtext}\usepackage[utf8]{inputenc}   \title{Sections and Chapters}\author{Gubert Farnsworth}\date{\today}   \begin{document}   \maketitle   \section*{Introduction}   This is the first section.   Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortisfacilisis sem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdietmi nec ante. Donec ullamcorper, felis non sodales...   \section*{Second Section}   Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam lobortis facilisissem. Nullam nec mi et neque pharetra sollicitudin. Praesent imperdiet mi necante...   \end{document}

Open an example in ShareLaTeX

Document chapters and sections in a Book/Report

As mentioned before, can be used in documents and reports. Below you can see an example:

\documentclass{report}   \begin{document}   \tableofcontents{}   \chapter{Editing compile}   \section{First Compile}   how to compile basic hello world into a pdf.   Write your favorite text editor create file and copy/paste the following (with hello.tex):   \subsection{Output formats}   different output formats (dvi, pdf)   The output of this command \$latex hello.tex will be a dvi file (hello.dvi). This file (.dvi) can be converted by \$dvipdf hello.dvi The get an pdf file from tex file, run this command \\$texi2pdf hello.tex   \chapter{Document Structure}   \section{Reserved Characters}   The following symbols characters are reserved by LATEX because they introduce a command and have a special meaning.   \end{document}

Books are the most complex type of documents when it comes to grouping the content in sections. Below a complete example with parts, chapters, sections and subsections

\documentclass{book}\usepackage[utf8]{inputenc}   \title{42}\author{Jane Doe}\date{\today}   \begin{document}   \maketitle   \tableofcontents   \part{First Part of this document}   \chapter{First}   \section{Introduction} Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco...   \subsection{Sample subsection} Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris..   \subsubsection{Sample subsubsection} Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercita...

Open an example in ShareLaTeX

Customize chapters and sections

You can use the titlesec package to customize chapters, sections and subsections style in an easy way.

\documentclass[a4paper,12pt]{book}\usepackage[utf8]{inputenc}\usepackage{titlesec}   \titleformat{\chapter}% command[display]% shape{\bfseries\Large\itshape}% format{Story No. \ \thechapter}% label{0.5ex}% sep{\rule{\textwidth}{1pt}\vspace{1ex}\centering}% before-code[\vspace{-0.5ex}%\rule{\textwidth}{0.3pt}]% after-code     \titleformat{\section}[wrap]{\normalfont\bfseries}{\thesection.}{0.5em}{}   \titlespacing{\section}{12pc}{1.5ex plus .1ex minus .2ex}{1pc}     \begin{document}   \chapter{Let's begin}   \section{First Attempt}   Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...   \section{Second attempt}   Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...   \end{document}

There are two general commands:

where and are optional parameters, and:

• is the sectioning command to be redefined: \part, \chapter, \section, \subsection, \subsubsection, \paragraph or \subparagraph.
• is sectioning paragraph shape; possible values are: hang, block, display, runin, leftmargin, rightmargin, drop, wrap, frame.
• is the format to be applied to the title, label, and text; for example
• specify sectioning label.
• is the horizontal separation between label and title body and it must be a length and not be empty.
• is code preceding the title body.
• is code following the title body.

and

where:

• increases the left margin.
• is the vertical space before the title.
• is the separation between title and non-sectioning text.

The starred version of this command () kills the indentation of the paragraph following the title.

Open an example of the titlesec package in ShareLaTeX

\titleformat{<command>}[<shape>]{<format>}{<label>}{<sep>}{<before-code>}[<after-code>]
\titlespacing{<command>}{<left>}{<before-sep>}{<after-sep>}

LaTeX and the document class will normally take care of page layout issues for you. For submission to an academic publication, this entire topic will be out of your hands, as the publishers want to control the presentation. However, for your own documents, there are some obvious settings that you may wish to change: margins, page orientation and columns, to name but three. The purpose of this tutorial is to show you how to configure your pages.

We will often have to deal with TeX lengths in this chapter. You should have a look at Lengths for comprehensive details on the topic.



Documents can be either one- or two-sided. Articles are by default one-sided, books are two-sided. Two-sided documents differentiate the left (even) and right (odd) pages, whereas one-sided do not. The most notable effect can be seen in page margins. If you want to make the article class two-sided, use .

Many commands and variables in LaTeX take this concept into account. They are referred to as even and odd. For one-sided document, only the odd commands and variables will be in effect.

Page dimensions

A page in LaTeX is defined by many internal parameters. Each parameter corresponds to the length of an element of the page, for example, is the physical height of the page. Here you can see a diagram showing all the variables defining the page. All sizes are given in TeX points (pt), there are 72.27pt in an inch or 1pt ≈ 0.3515mm.

1. one inch +
2. one inch +
3. = 31pt
4. = 20pt
5. = 12pt
6. = 25pt
7. = 592pt
8. = 390pt
9. = 10pt
10. = 35pt
11. = 30pt
• = 7pt (not shown)
• = 0pt
• = 0pt
• = 597pt
• = 845pt

The current details plus the layout shape can be printed from a LaTeX document itself. Use the layout package and the command of the same name:

To render a frame marking the margins of a document you are currently working on, add

to the document.

Page size

It will not have been immediately obvious - because it doesn't really cause any serious problems - that the default page size for all standard document classes is US letter. This is shorter by 18 mm (about 3/4 inch), and slightly wider by 8 mm (about 1/4 inch), compared to A4 (which is the standard in almost all the rest of the world). While this is not a serious issue (most printers will print the document without any problems), it is possible to specify alternative sizes as class option. For A4 format:

 \documentclass[a4paper]{article}

More size options with geometry

One of the most versatile packages for page layout is the geometry package. The immediate advantage of this package is that it lets you customize the page size even with classes that do not support the options. For instance, to set the page size, add the following to your preamble:

 \usepackage[a4paper]{geometry}

The geometry package has many pre-defined page sizes, like a4paper, built in. Others include:

• a0paper, a1paper, ..., a6paper,
• b0paper, b1paper, ..., b6paper,
• letterpaper,
• legalpaper,
• executivepaper.

To explicitly change the paper dimensions using the geometry package, the paperwidth and paperheight options can be used. For example:

 \usepackage[paperwidth=5.5in, paperheight=8.5in]{geometry}

Page size issues

If you intend to get a PDF in the end, there are basically three ways:

pdflatex myfile # TeX → PDF latex myfile # TeX → DVI dvipdf myfile # DVI → PDF latex myfile # TeX → DVI dvips myfile -o myfile.ps # DVI → PS ps2pdf myfile.ps myfile.pdf # PS → PDF

Sadly the PDF output page size may not be completely respectful of your settings. Some of these tools do not have the same interpretation of the DVI, PS and PDF specifications, and you may end up with a PDF which has not exactly the right size. Thankfully there is a solution to that: the command lets the user pass PostScript or PDF parameters, which can be used here to set the page size appropriately.

• For to work fine, using the package geometry usually works.
• For the DVI and PS ways, the safest way to always get the right paper size in the end is to add
 \documentclass[...,a4paper,...]{...}\special{papersize=210mm,297mm}

to the tex file, and to append the appropriate parameters to the processors used during output generation:

dvips -t a4 ... ps2pdf -sPAPERSIZE=a4 ... # On Windows: ps2pdf -sPAPERSIZE#a4 ... [1]

If you want US Letter instead, replace 210mm,297mm by 8.5in,11in and a4paper by letter. Also replace by in command-line parameters.

Page size for tablets

Those who want to read on tablets or other handheld digital devices need to create documents without the extra whitespace. In order to create PDF documents with optimal handheld viewing, not only must the text field and margins be adjusted, so must the page size. If you are looking for a sensible dimension, consider following the paper size used by the Supreme Court of the United States, 441pt by 666pt (or 6.125 inches by 9.25 inches), which looks great on tablets. You could also use the Supreme Court's text field size of 297 pt by 513 pt, but this is too wide for fonts other than Century Schoolbook, the font required by the Supreme Court.

Margins

Readers used to perusing typical physical literature are probably wondering why there is so much white space surrounding the text. For example, on A4 paper a document will typically have 44 mm margin widths on the left and right of the page, leaving about 60% of the page width for text. The reason is improved readability. Studies have shown[2][3] that it's easier to read text when there are 60–70 characters per line—and it would seem that 66 is the optimal number. Therefore, the page margins are set to ensure optimal readability, and excessive margin white space is tolerated as a consequence. Sometimes, this white space is left in the inner margin with the assumption that the document will be bound.

If you wish to avoid excessive white space, rather than changing the margins, consider instead using a two-column (or more) layout. This approach is the one usually taken by print magazines because it provides both readable line lengths and good use of the page. Another option for reducing the amount of whitespace on the page without changing the margins is to increase the font size using the 12pt option to the document class.

If you wish to change the margins of your document, there are many ways to do so:

• One older approach is to use the fullpage package for somewhat standardized smaller margins (around an inch), but it creates lines of more than 100 characters per line with the 10pt default font size (and about 90 if the 12pt documentclass option is used):

For even narrower margins, the fullpage package has a cm option (around 1.5cm), which results in about 120 characters per line at the 10pt default font size, about double what is considered readable:

 \usepackage[cm]{fullpage}
• A more modern and flexible approach is to use the geometry package. This package allows you to specify the 4 margins without needing to remember the particular page dimensions commands. You can enter the measures in centimeters and inches as well. Use cm for centimeters and in for inches after each value (e.g. 1.0in or 2.54cm). Note that by default (i.e. without any options) this package already reduces the margins, so for a 'standard layout' you may not need to specify anything. These values are relative to the edge of paper (0in) and go inward. For example, this command provides more conventional margins, better using the vertical space of the page, without creating the dramatically long lines of the fullpage package (if the 11pt documentclass option is used, the line lengths are about 88 characters for letter-sized paper and slightly less when using a4paper).
 \usepackage[top=1in, bottom=1.25in, left=1.25in, right=1.25in]{geometry}

It can also recreate the behavior of the fullpage package using

 \usepackage[margin=1in]{geometry}

You can combine the margin options with the page size options seen in this paragraph.

• You should not use the a4wide package for a page with A4 document size with smaller margins. It is obsolete and buggy. Use geometry package instead like this:
• Edit individual page dimension variables described above, using the and commands. See the Lengths chapter. For instance,

Odd and even margins

Using the geometry package, the options left and right are used for the inside and outside margins respectively. They also have aliases inner and outer. Thus, the easiest way to handle different margins for odd and even pages is to give the twoside option in the document class command and specify the margins as usually.

 \documentclass[twoside]{report}\usepackage[inner=4cm,outer=2cm]{geometry}%left=4cm,right=2cm would be equivalent

This will result in a value of 4cm on all inner margins (left margin for odd number pages and right margin for even pages) and 2cm margin on outer margins.

Setting the same value for the inner and outer for geometry will remove the difference between the margins. Another quick way to eliminate the difference in position between even and odd numbered pages would be setting the values to evensidemargin and oddsidemargin to the half of odd's default:

 \setlength{\oddsidemargin}{15.5pt}\setlength{\evensidemargin}{15.5pt}

By default, the value of evensidemargin is larger than oddsidemargin in the two-sided layout, as one could wish to write notes on the side of the page. The side for the large margin is chosen opposite to the side where pages are joined together.

See the Lengths.

Top margin above Chapter

The top margin above a chapter can be changed using the titlesec package. Example: [1]

 \usepackage{titlesec}\titlespacing*{\chapter}{0pt}{-50pt}{20pt}\titleformat{\chapter}[display]{\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{20pt}{\Huge}

The command must be used when the spacing of a chapter is changed. In case of a section this command can be omitted.

Page orientation

When you talk about changing page orientation, it usually means changing to landscape mode, since portrait is the default. We shall introduce two slightly different styles of changing orientation.

Change orientation of the whole document

The first is for when you want all of your document to be in landscape from the very beginning. There are various packages available to achieve this, but the one we prefer is the geometry package. All you need to do is call the package, with landscape as an option:

 \usepackage[landscape]{geometry}

Although, if you intend to use geometry to set your paper size, don't add the commands twice, simply string all the options together, separating with a comma:

 \usepackage[a4paper,landscape]{geometry}

Using standard LaTeX classes, you can use the same class options:

 \documentclass[a4paper,landscape]{article}

Change orientation of specific part

The second method is for when you are writing a document in portrait, but you have some contents, like a large diagram or table that would be displayed better on a landscape page. However, you still want the consistency of your headers and footers appearing the same place as the other pages.

The lscape package is for this very purpose. It supplies a landscape environment, and anything inside is basically rotated. No actual page dimensions are changed. This approach is more applicable to books or reports than to typical academic publications. Using pdflscape instead of lscape when generating a PDF document will make the page appear right side up when viewed: the single page that is in landscape format will be rotated, while the rest will be left in portrait orientation.

Also, to get a table to appear correctly centered on a landscaped page, one must place the tabular environment inside a table environment, which is itself inside the landscape environment. For instance it should look like this:

 \usepackage{pdflscape}% ...\begin{landscape}\begin{table}\centering% optional, probably makes it look better to have it centered on the page\begin{tabular}{....}% ...\end{tabular}\end{table}\end{landscape}

For books (and in general documents using the option), the -environment unfortunately does not pay attention to the different layout of even and odd pages. The macro can be fixed using a few lines of extra code in the preamble[4].

Change orientation of floating environment

If you use the above code, you will see that the table is inserted where it is in the code. It will not be floated! To fix this you need the package rotating. See the Rotations chapter.

Margins, page size and rotation of a specific page

If you need to rotate the page so that the figure fits, the chances are good that you need to scale the margins and the font size too. Again, the geometry package comes in handy for specifying new margins for a single page only.

 \usepackage{geometry}\usepackage{pdflscape}% ...\newgeometry{margin=1cm}\begin{landscape}\thispagestyle{empty}%% Remove header and footer.\begin{table}\begin{center}\footnotesize%% Smaller font size.\begin{tabular}{....}% ...\end{tabular}\end{center}\end{table}\end{landscape}\restoregeometry

Note that order matters!

Page background

The eso-pic package will let you print content in the background of every page or individual pages.

The starred-version of the command applies to the current page only.

Multi-column pages

Using the twocolumn optional class argument

Using a standard Latex document class, like article, you can simply pass the optional argument twocolumn to the document class: which will give the desired effect.

While this approach is useful, it has limitations. The multicol package provides the following advantages:

• Can support up to ten columns.
• Implements a multicols environment, therefore, it is possible to mix the number of columns within a document.
• Additionally, the environment can be nested inside other environments, such as figure.
• multicol outputs balanced columns, whereby the columns on the final page will be of roughly equal length.
• Use multicols* environment for unbalanced columns, whereby each column is completely filled before starting with the next column.
• Vertical rules between columns can be customised.
• Column environments can be easily customised locally or globally.

Using multicol package

The multicol package overcomes some of the shortcomings of twocolumn and provides the environment. To create a typical two-column layout:

 \begin{multicols}{2} lots of text \end{multicols}

Floats are not fully supported by this environment. It can only cope if you use the starred forms of the float commands (e.g., ) which makes the float span all columns. This is not hugely problematic, since floats of the same width as a column may be too small, and you would probably want to span them anyway. See this section for a more detailed discussion.

The multicol package has two important parameters which can be set using :

• , sets the width of the vertical rule between columns and defaults to 0pt
• , sets the horizontal space between columns and the defaults to 10pt, which is quite narrow

To force a break in a column, the command is used.

Manual page formatting

There may be instances, especially in very long documents, such as books, that LaTeX will not get all page breaks looking as good as it could. It may, therefore, be necessary to manually tweak the page formatting. Of course, you should only do this at the very final stage of producing your document, once all the content is complete. LaTeX offers the following:

 Ends the current page and starts a new one. Breaks the current page at the point of the command. The optional number argument sets the priority in a scale from 0 to 4. Stops the page being broken at the point of the command. The optional number argument sets the priority in a scale from 0 to 4. Ends the current page and causes any floats encountered in the input, but yet to appear, to be printed.

Widows and orphans

In professional books, it's not desirable to have single lines at the beginning or end of a page. In typesetting such situations are called 'widows' and 'orphans'. Normally it is possible that widows and orphans appear in LaTeX documents. You can try to deal with them using manual page formatting, but there's also an automatic solution.

LaTeX has a parameter for 'penalty' for widows and orphans ('club lines' in LaTeX terminology). With the greater penalty LaTeX will try more to avoid widows and orphans. You can try to increase these penalties by putting following commands in your document preamble:

 \widowpenalty=300 \clubpenalty=300

If this does not help, you can try increasing these values even more, to a maximum of 10000. However, it is not recommended to set this value too high, as setting it to 10000 forbids LaTeX from doing this altogether, which might result in strange behavior.

It also helps to have rubber band values for the space between paragraphs:

 \setlength{\parskip}{3ex plus 2ex minus 2ex}

Alternatively, you can use the package to reserve some lines and thus to prevent page breaking for those lines.

 \needspace{5\baselineskip} Some text on 5 lines.

Troubleshooting

A very useful troubleshooting and designing technique is to turn on the showframe option in the geometry package (which has the same effect as the showframe package described above). It draws bounding boxes around the major page elements, which can be helpful because the boundaries of various regions are usually invisible, and complicated by padding whitespace.

 \usepackage[showframe]{geometry}

Notes and References

This page uses material from Andy Roberts' Getting to grips with LaTeX with permission from the author.