Introduction

LaTeX has long been my main typesetting environment. This post has some handy dandy codes that I daily use.

Compression of Existing PDFs

Typical output as a result of LaTeX compilations is in PDF format. PDF is perfect for cross-platform usability. LaTeX don’t compress images in the PDF outputs. Thus, depending on the number of images and their sizes, one might get a large PDF file.

A common approach is the let Ghostscript (gs) optimize and compress the PDF after its generation with pdflatex.

The following command can be invoked on a LINUX like machine. It takes foo.pdf and saves its compressed version as compressed.pdf with the parameters specified. I mostly use these settings1 for Beamer presentations for about 70% reductions in file sizes without any noticiable loss of quality on screen views.

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.5 -dPDFSETTINGS=/prepress -dPrinted=false -dNOPAUSE -dQUIET -dBATCH -sOutputFile=compressed.pdf foo.pdf

Batch Resize Images

Overly large image files can significantly prolong the compilation process. The following command will raster images with a .jpg extension to 600 px width or height in a folder. This gives a decent image quality for Beamer presentations, while making the PDF files significantly smaller.

sips -Z 600 *.jpg

Automator on Mac OS can be used to make and run this command as a program with decent GUI interaction but I like Terminal as the whole process is much simpler.

Automator is based on AppleScript Language however many daily tasks can be done easily without any coding. I thought it might be useful to mention Automator here for more complex manipulations.

Photographic images saved as .png can also result in very large file sizes: .jpg is usually a much more space-efficient format for photographs. Line drawings, plots and diagrams are better saved as .png or .pdf, in general. .pdf images can also compile faster compared to .png files if their file sizes are comparable, as the compile process then doesn’t need to call libpng2.


  1. -dPrinted=false preserves the cross-references. 

  2. See https://www.overleaf.com for more information.