Author

Colaboraciones

Artículos escritos por los lectores.

Colaboraciones's posts | Porfolio

Table Of Contents

This post is a collaboration by Luis Liñan, @lulivi.

Today we are going to use a simple shell script to autoupdate the pdf preview of our latex files. Then, it will bring the pdf from the metafiles directory to our project directory, keeping it clean from auxiliar files.

Directory structure

Working with the same structure as other article from the blog we will have what follows:

.
|-- figures
|   |-- figure_1.pdf
|   `-- ...
|-- main.pdf
|-- main.tex
|-- ref.bib
|-- style.sty
`-- metafiles
    |-- main.aux
    |-- main.bbl
    |-- main.blg
    |-- main.fdb_latexmk
    |-- main.fls
    |-- main.lof
    |-- main.log
    |-- main.lot
    |-- main.out
    |-- main.pdf
    `-- main.toc

Script execution

#!/bin/bash

# Help function
function help {
  echo "$0 <metafiles_directory>"
}

# Check correct execution of the script
if [ "$#" -ne 1 ]; then
  help
  exit 1
fi

# Create directory if it doesn't exist
if [ ! -d $1 ]; then
  mkdir $1
fi

# Copy the pdf from the temporal directory to the parent directory
function listen_pdf_update {
  pdf_substring='(.*[.]pdf.*)'
  while true; do
    change=$(inotifywait -e close_write $1)
    if [[ $change =~ $pdf_substring ]]; then
      cp $1/*.pdf ./
      echo "============> iNotify <============"
      echo "==>   Updated parent pdf file   <=="
      echo "==================================="
    fi
  done
}

# Set up listener for the target PDF file
listen_pdf_update $1 &

# Set up latex listener for changes in the directory
latexmk -shell-escape -silent -bibtex -view=pdf -xelatex -pdf -pvc -output-directory=$1

# Kill all processes created in this script
kill -9 -$$

The only thing we have to do is execute the script with the metafiles directory as an argument:

$ /PATH/TO/SCRIPT/updatePdfLatex.sh <metafiles_directory>

Check the script repository out for more information.

References

Spot a typo?: Help me fix it by contacting me or commenting below!

Categories:Tags:

Maybe this posts are also worth reading