A shell script to indent LaTeX documents

I write a shell script to simplify the operation of indenting LaTeX documents with the Perl package latexindent. The source code is hosted at GitHub, here.

1. How to use

Download the shell script latexindent.sh and the perl package latexindex from here. Then use the following command to indent your LaTeX document:

./latexindent.sh path/filename.tex

What the shell script does is to:

  • generate the indented tex documents which is still named filename.tex
  • backup the original tex file filename.tex as filename.tex.old

The source code of latexindent.sh is below:

#!/usr/bin/env bash

if [ -z $1 ]; 
then
    echo "Usage: $0 path/filename.tex" 
    exit
fi

tex_file_origin=$1
tex_file_old="${tex_file_origin}.old" # backup the original tex file

# create a tmp file and delete while exiting
tmp_file="/tmp/tmp_file.$$"
trap "rm -f $tmp_file" EXIT

# indent a tex document
perl latexindent.pl -s -o $tex_file_origin $tmp_file

mv $tex_file_origin $tex_file_old
cp $tmp_file $tex_file_origin

2. The usage of latexindent

latexindent.pl version 2.1R
usage: latexindent.pl [options] [file][.tex]
      -h  help (see the documentation for detailed instructions and examples)
      -o  output to another file; sample usage
                latexindent.pl -o myfile.tex outputfile.tex
      -w  overwrite the current file- a backup will be made, but still be careful
      -s  silent mode- no output will be given to the terminal
      -t  tracing mode- verbose information given to the log file
      -l  use localSettings.yaml (assuming it exists in the directory of your file)
      -d  ONLY use defaultSettings.yaml, ignore ALL user files
      -c=cruft directory used to specify the location of backup files and indent.log

Leave a Reply

Your email address will not be published. Required fields are marked *