Monday, January 2, 2012

A script to compile a latex-beamer presentation into all themes

I always wondered how great and handy would it be had I had a nifty script to compile my latex presentation using all the themes present in my current tex installation so that I can have a look at all the output files and choose the one which looks best.

And, this is the little bash shell script I've written to fulfill my need.
  1. Save this script into your Latex project directory
  2. Adjust the variables listed in the script to your liking ($themes_path and $output_directory)
  3. Give executable permission using "chmod +x $filename" or otherwise
  4. Execute it! It takes your ".tex" as the command-line argument and outputs the pdfs into a directory named "change_themes". There will be as many themes as your Beamer installation could possibly output.
PS: It'll not disturb/write to your original .tex file though. It just reads it.

#!/bin/bash

themes_path="/usr/share/texmf/tex/latex/beamer/base/themes/theme"
filename="$1"
output_directory="./change_themes"

# Needs the .tex file as the command-line argument
if [[ -z "$filename" ]]; then
    echo "Needs a command-line argument";
    exit;
fi

# Create the output directory if needed
if ! [[ -d "$output_directory" ]]; then
    mkdir -p "$output_directory";
fi

for theme in $themes_path/*.sty; do
    theme_name="$(echo "$(basename "$theme")" | sed -re 's/beamertheme(.*)\.sty/\1/')"
    sed -r "
s/usetheme\{(.*)\}/usetheme{$theme_name}/" "$filename" > "$output_directory/$filename"
    pdflatex -output-directory="
$output_directory" "$output_directory/$filename"
    pdf_fname="
$(basename "$filename" ".tex").pdf"
    pdf_fname_new="
${theme_name}_${pdf_fname}"
    mv "
$output_directory/$pdf_fname" "$output_directory/$pdf_fname_new"
done

No comments: