TITLE: Custom .sty files in LaTeX DATE: 2019-10-05 AUTHOR: John L. Godlee ==================================================================== 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 % Define page geometry \usepackage{geometry} \geometry{left=2.2cm, right=2.2cm, top=2.2cm, bottom=2cm} \parskip 0.15cm \setlength{\parindent}{0cm} % Set font \usepackage[T1]{fontenc} % Image handling \usepackage{graphics} % Insert images easily \usepackage{graphicx} % Extended image support \makeatletter \g@addto@macro\@floatboxreset\centering % Automatically centre images (floats) \makeatother \graphicspath{ {img/} } \usepackage{float} % Graphics placement [H] [H!] arguments \usepackage{subfig} % Compound figures % Bibliography management \usepackage{natbib} % Bibliography management - Use author/date citations \bibliographystyle{agsmnourl} % Use custom agsm bibliography template with no URL \usepackage{cite} % Citation options % Text formatting \usepackage{url} % Allow nice formatting of URLs in text \usepackage{enumerate} % Enumerated lists \usepackage{lineno} % Line numbers \usepackage{textcomp} \newcommand{\textapprox}{\raisebox{0.5ex}{\texttildelow}} % Command for a good tilde \usepackage{xcolor} \newcommand{\todo}[1]{\textcolor{red}{#1}} % \todo{NOTE TO SELF WRITTEN IN RED} % Define code chunk aesthetics \usepackage{listings} \definecolor{codegreen}{rgb}{0,0.6,0} \definecolor{codegray}{rgb}{0.5,0.5,0.5} \definecolor{codepurple}{rgb}{0.58,0,0.82} \definecolor{backcolour}{RGB}{212,212,212} \lstdefinestyle{mystyle}{ backgroundcolor=\color{backcolour}, commentstyle=\color{codegreen}, keywordstyle=\color{magenta}, numberstyle=\tiny\color{codegray}, stringstyle=\color{codepurple}, basicstyle=\ttfamily\footnotesize, breakatwhitespace=false, breaklines=true, captionpos=b, keepspaces=true, numbers=left, numbersep=5pt, showspaces=false, showstringspaces=false, showtabs=false, tabsize=2 } \lstset{style=mystyle} % Custom title formatting \let\oldtitle\title \renewcommand{\title}[1]{\oldtitle{\vspace{-1.5cm}#1}} Some potentially non-obvious bits of the code: - \Providespackage{mynotes} gives the name of the package, which can then be called in any document with \usepackage{mynotes}. - \usepackage[T1]{fontenc} is a little tip I learned while looking up LaTeX typefaces. It forces the use of an 8-bit font which means that words with special characters are properly copyable from the PDF output. I learned it on [this SO question] - \g@addto@macro\@floatboxreset\centering globally centres images - \bibliographystyle{agsmnourl} uses a custom bibliography template which I made. agsmnourl.bst resides in the same directory as mynotes.sty - \newcommand{\textapprox}{\raisebox{0.5ex}{\texttildelow}} provides a sensible tilde in text. - The code in \lstdefinestyle{... defines the colour scheme for code chunks inserted with \begin{lstlisting}. - \renewcommand{\title}[1]{\oldtitle{\vspace{-1.5cm}#1}} moves the title created by \maketitle up a bit, to make the document more compact. [this SO question]: https://tex.stackexchange.com/questions/664/why-should-i-use-usepackaget1fontenc 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//' ); An example document made with mynotes.sty can be found [here] [here]: https://johngodlee.github.io/files/sty_latex/example.zip