Miscellaneous LaTeX snippets

2019-09-30

I spent Sunday afternoon researching a short list of things I wanted to fix in LaTeX to streamline my experience.

Define macros for formatted blocks

Sometimes I have to write a document like a CV or a set of handout notes which require reuse of a complex formatted block of text. For example, below, where I want to create a timeline of events with the date of the event on the left of the page, with a description of the event on the right. I can define a macro for this instead of writing the same boilerplate code every time. I stole this from a Luke Smith youtube video .

\newcommand{\entry}[4]{

\begin{minipage}[t]{0.15\textwidth}
\hfill{} \textsc{#1}
\end{minipage}
\vline\hfill{}
\begin{minipage}[t]{0.8\textwidth}
#2

\textit{#3}

\footnotesize{#4}
\end{minipage}\\ \vspace{0.25cm}
}

This macro has four insertion points for user input, defined in the square brackets and called with #n arguments. The macro places a small area to the left of the vertical line, then a larger one to the right.

To call the macro and give the user input:

\entry{2014--2016}{Web dev}{More text}{This is a description}
Formatted block for CV

Globally set image size

\documentclass{article}
\usepackage{graphicx}
\setkeys{Gin}{width=\linewidth, height=10cm, keepaspectratio}

\begin{document}

\begin{figure}
\includegraphics{test.png}
\end{figure}

\end{document}

Globally center images

\documentclass{article}
\usepackage{graphicx}

\makeatletter
\g@addto@macro\@floatboxreset{\centering}
\makeatother

\begin{document}

\begin{figure}
\includegraphics{test.png}
\end{figure}

Figures in wrapped text

This uses the wrapfig package to make a right-aligned figure. The figure itself is 5 cm width and the image inside the figure is also 5 cm.

\documentclass{article}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{blindtext}

\begin{document}

\blindtext

\begin{wrapfigure}{r}{5cm}
\centering{}
\includegraphics[width=5cm]{img}
\caption{This is a caption}
\label{right_img}
\end{wrapfigure}

\blindtext

\end{document}

Custom directory for .bst files

.bst files are used to hold custom BibTeX reference styles. I have one that is based on the agsm style, but I’ve set it to not include URLs in the reference list, it’s called agsmnourl.bst. In my LaTeX document I can use this style file with:

\documentclass{article}

\usepackage{natbib}
\bibliographystyle{agsmnourl}  % Use custom template 
\usepackage{cite}

\begin{document}

\citep{test}

\bibliography{test}

\end{document}

Previously I had kept agsmnourl.bst in the same directory as the project, but my laptop was quickly filling up with duplicates of the style file. I found out that latexmk which I use to compile LaTeX documents, can take a ~/latexmkrc file where certain variables can be defined. I can define a custom directory which holds these custom .bst files:

$ENV{'TEXINPUTS'}='~/.texmf//:' . $ENV{'TEXINPUTS'};

In this case the directory is ~/.texmf.

Then, the next time I run latexmk it will source latexmkrc.