LaTeX Tips & Tricks
1 Introduction
I’ve recently started using LaTeX more often, mostly for typesetting complex academic papers. LaTeX has a steep learning curve, but it has some great features - notably automatic placement of figures and tables, which in my opinion is works vastly better than having to place them manually in Word. In addition, the support for equations and equation numbering is excellent, and you can extend it via packages to do almost anything you want. While learning LaTeX I’ve run into some niche problems, the solutions to which I’ll collate here so that I & others can reference them.
2 Automatic figure resizing
A common LaTeX issue is floats (figures, tables, algorithms, etc.) or their captions running over the margins of the page. Of course it’s possible to expand the margins of your page, but that might not be aesthetically pleasing, might not meet journal submission requirements, etc. Including the following lines in your document’s preamble will use the graphicx
package to automatically resize your figures to fit the margins of the page while maintaining the aspect ratio of the original figure.
This code was modified from the LaTeX preamble created when rendering a Quarto document to PDF.
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\makeatletter
\def\fps@figure{htbp}
\makeatother
5 Tables
Most papers have at least one or two tables. The default table style in LaTeX is pretty good, but the lines are a bit cramped. To add better spacing, use the following snippet in your preamble:
\renewcommand{\arraystretch}{1.5}