Custom .sty files in LaTeX

2019-10-05

Note, I updated this post on 2021-07-31, as my LaTeX template has developed.

I’ve been slowly developing a nice looking generic LaTeX template which I can use for notes and short reports. Up to now I’ve been copying and pasting a chunk of preamble between documents and tweaking it if I need to add anything particular for the document. I thought it was time I experimented with modular document building in advance of me writing my thesis next year, so I made a .sty style file which holds the basic configuration. The .sty file doesn’t actually change very much of the formatting of a basic article class document, but it does keep the packages I use consistent. The difficult bit turned out not to be making the file itself, but knowing where to put it and making sure that latexmk knew where to find it. The file mynotes.sty looks like this:

% Declare package name
\ProvidesPackage{mynotes}  % Give same name as .sty file

% Page geometry
\usepackage{geometry}
\geometry{left=2.54cm,
	right=2.54cm,
	top=2.54cm,
	bottom=2.54cm}
\parskip 0.15cm
\setlength{\parindent}{0cm}

\usepackage{pdflscape}

% Font
\usepackage[T1]{fontenc}

% English language
\usepackage[utf8]{inputenc}
\usepackage[UKenglish]{babel}
\usepackage{csquotes}

% Image handling
\usepackage{graphicx}  % Extended image support
\usepackage{float}  %  Graphics placement [H] [H!] arguments
\usepackage{caption}  % Custom captions
\usepackage{subcaption}  % Compound figures

\makeatletter
	\g@addto@macro\@floatboxreset\centering  % Automatically centre images (floats)
\makeatother

% Tables
\usepackage{booktabs}  % Sensible horizontal rules
\usepackage{multirow}  % Tables with cells split over multiple rows
\usepackage{longtable}  % Tables spanning multiple pages

% Bibliography 
\usepackage[natbib, 
	style=authoryear, 
	uniquename=false, 
	uniquelist=false,
	giveninits=true,
	dashed=false,
	maxcitenames=2, 
	mincitenames=1, 
	minbibnames=10, 
	maxbibnames=10, 
	backend=biber]{biblatex}
\renewcommand*\finalnamedelim{\addspace\&\space}

% Text formatting
\usepackage{url} % Allow nice formatting of URLs in text

\usepackage{enumerate}  % Enumerated lists

\usepackage{lineno}  % Line numbers

\newcommand{\textapprox}{\raisebox{0.5ex}{\texttildelow}}  % Better tilde

\usepackage{siunitx}  % Units
\usepackage{amsmath}  % Math symbols

\usepackage[table]{xcolor}  % text colours

\newcommand{\todo}[1]{\textcolor{red}{\textbf{#1}}}   % \todo{NOTE IN RED}

\usepackage{framed}  % Framed boxes

\usepackage{microtype}  % Improved text justification

\usepackage{listings}  % Code input 
\input{code_format}  % Code styling

% Custom title formatting
\let\oldtitle\title
\renewcommand{\title}[1]{\oldtitle{\vspace{-1.5cm}#1}}

% Links
\usepackage[breaklinks]{hyperref}
\definecolor{links}{RGB}{191,59,72}
\hypersetup{
	breaklinks,
	colorlinks,
	allcolors=links,
	linktoc=section,
	pdfauthor={John L. Godlee}
}

% Rename sections when cross-linking
\def\subsectionautorefname{section}
\def\subsubsectionautorefname{section}

Some potentially non-obvious bits of the code:

I found that I could make a custom ~/.latexmkrc with the following contents, to tell latexmk where my custom templates folder was, in this case ~/.texmf/:

ensure_path( 'TEXINPUTS', '~/.texmf//' );