Creating LaTeX Stylesheets

Using a stylesheet makes it easy to keep in one place all the styles used by the document. Stylesheets are a useful method of organizing all the styles used throughout your LaTeX project. You could also have a single stylesheet file, stylesheet.sty for example, that is used across multiple projects.

The key development effort to create high-quality technical documentation is in the stylesheet. This took a considerable amount of time to develop. Getting it right is critical for the quality of the documentation.


Note: The upcoming topics will refer to the stylesheet frequently. You define the style then you need to implement it in the document.


Why Use Style Sheets?

You could put all your styles in the head of each .tex file in your project but that makes no sense, especially if you have multiple files referring to the same style sheet.

Furthermore, keeping all your styles in a single file makes maintaining the styles vastly easier. For example, you could comment out particular commands, especially commands that include chapters, to help you during your development process.

You could also comment out chapters that are specific to outputs. I am sure there are other ways of doing this, but for me, simplicity is key, so commenting out of a command or include works.

Another useful way you could use stylesheets is to include examples of how to use a particular style (commented out, of course).

Using a stylesheet subdirectory

For my LaTeX projects, I created a subdirectory called \styles, not surprisingly, and I placed in this directory a single style sheet called mystyles.sty. If more than one stylesheet is needed, you could place them in this subdirectory. You could also have several stylesheets for specific outputs and just comment out the unneeded stylesheet in the root file.

Here is an example project structure. Notice the \styles directory.

Project Root
├── manual-ROOT.tex        # document root file
├── \styles                # stylesheet subdirectory
├── \chapters              # subdirectory to store all chapters
├── \append                # subdirectory to store any appendices
├── \images                # subdirectory for shared images

Including the stylesheet in the root file - main.tex

To use the stylesheet in your project, you simply include a command in the root file main.tex immediately following the \documentclass command but before the \begin{document} command.

Here is an example snippet from a main.tex file.

1
2
3
4
5
6
7
8
9
10
11
12
13
% Copyright Glenn J. Lea
% Project name
%
% MAIN FILE
%

%\RequirePackage[l2tabu, orthodox]{nag}
\documentclass[a4paper,11pt,twoside]{book}  # Use Book class

% INSERT PS-template.sty                    # Insert Stylesheet
\usepackage{styles/PS-template}

\begin{document}                            # Required element

Example stylesheet

Here is a segment of an example stylesheet I used. I kept all related styles together within sections in the stylesheet using comments. I shortened the comments to make the stylesheet easier to read.

Packages

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
% Packages
  \usepackage[T1]{fontenc}
  \usepackage{amsmath}
  \usepackage{amssymb,amsfonts,textcomp}
  \usepackage{hhline}

% Creates custom columns
  \usepackage{array}

% Create links for clickable URLs
  \usepackage{hyperref}

% Langauge selection to US English
  \usepackage[english]{babel}

% Standard graphics support.
  \usepackage{graphicx}

Base fonts and text formatting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
% Font Selection Styles and Packages, where Helvetica is the base font.
  \usepackage{lmodern}
  \usepackage{helvet}
  \renewcommand{\familydefault}{\sfdefault}

% Monospaced font
  \usepackage{courier}

% Allow relative size specifications, for example \smaller, \larger.
  \usepackage{relsize}

% Table of contents fonts and formatting
  \usepackage[titles]{tocloft}

% Redefine spacing
  \setlength{\cftbeforechapskip}{2ex}

  \setlength{\cftbeforesecskip}{0.5ex}

% Text styles
  \newcommand\textstyleCourier[1]{\texttt{#1}}
  \newcommand\textstyleDefaultParagraphFont[1]{#1}
  \makeatletter
  \newcommand\arraybslash{\let\\\@arraycr}
  \makeatother

Title Page

1
2
3
4
5
6
7
8
9
10
11
12
% Title page packages and colour settings
  \usepackage{xcolor}
  \usepackage{lipsum}  % Used for dummy text.
  \definecolor{titlepagecolor}{RGB}{0,38,75}
  \definecolor{namecolor}{RGB}{241,133,0}

% General use colour definitions
  \definecolor{blue}{rgb}{0,38,75}
  \definecolor{orange}{rgb}{241,133,0}
  \definecolor{gray}{rgb}{0.4, 0.0, 0.4}
  \definecolor{codeBackground}{RGB}{251, 251, 244}
  \definecolor{codeBackground}{white}

Warnings, Notes and Info boxes colours

1
2
3
4
5
6
7
8
9
% Background Colors
  \definecolor{warning}{RGB}{255,231,231} % Warning
  \definecolor{note}{RGB}{255,255,211} % Note
  \definecolor{info}{RGB}{224,239,255} % Info

% Line Colors
  \definecolor{infoline}{RGB}{158,182,212} % Info
  \definecolor{noteline}{RGB}{247,223,146} % Note
  \definecolor{warnline}{RGB}{51,51,51} % Warning

Chapter heading styles

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
% Sectioning Styles and Packages
  \usepackage{titlesec}
  %\usepackage{sectsty}

% Chapter heading redefinition

% Redefine size and position of chapter heading
  \titleformat{\chapter}
    {\fontsize{19pt}{0em}\selectfont\bf}
    {\thechapter.}{1em}{}

% Change colour of chapter heading
  \titleformat{\chapter}
    {\fontsize{20pt}{0em}\selectfont\bf\color{namecolor}}
    {\thechapter.}{1em}{}

% Redefine spacing above chapter heading
  \titlespacing*{\chapter}{0pt}{-30pt}{20pt}

% Indent Section label
  \titlelabel{\llap{\makebox[1cm][l]{\thetitle}}\hspace*{25.4mm}}

Section definitions

1
2
3
4
5
6
7
% Redefine font size of Section heading
  \titleformat{\section}[hang]
    {\normalfont\Large\bfseries\color{namecolor}}
    {\thesection.}{1em}{}

% Redefine spacing above Section heading
  \titlespacing{\section}{0pt}{*4}{*1.5}      

Subsection redefinition

1
2
3
4
% Redefine size and position of Subsection heading
  \titleformat{\subsection}[hang]
    {\normalfont\Normalsize\bfseries\color{namecolor}}
    {\thesubsection.}{1em}{}

Subsubsection redefinition

1
2
3
4
% Redefine size and position of subsubsection heading
  \titleformat{\subsubsection}[hang]
    {\normalfont\Normalsize\bfseries\color{namecolor}}
    {\thesubsubsection.}{1em}{}

Page size, margin and layout definitions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
% Package
  \usepackage[vcentering,dvips]{geometry}
  \geometry{papersize={210mm,295mm},total={160mm,185mm}}

% Defining page layout (geometry)
   \setlength\voffset{-1in}
   \setlength\hoffset{-1in}
   \setlength\topmargin{1cm}
   \setlength\oddsidemargin{3cm}
   \setlength{\evensidemargin}{3cm}

% Text height original: 24.5, new: 23.5cm
  \setlength\textheight{23.5cm}

% Text width original: 16cm, new 15 cm
  \setlength\textwidth{15cm}
  \setlength\footskip{1.0cm}
  \setlength\headheight{1cm}

  \setlength\headsep{1cm}

  \setlength{\parindent}{0cm}
  \setlength{\parskip}{3pt}