Skip to content

Instantly share code, notes, and snippets.

@paxperscientiam
Last active December 24, 2015 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paxperscientiam/bee00d09a00e219ebbbb to your computer and use it in GitHub Desktop.
Save paxperscientiam/bee00d09a00e219ebbbb to your computer and use it in GitHub Desktop.
TeXShop Engine for maintaining a sweet directory structure and LaTeX'ing and BibTeX'ing the correct number of times.
#!/opt/local/bin/bash
# WARNING -- AT ANY GIVEN TIME, THIS SCRIPT MAY NOT WORK DO TO CONSTANT UPDATES -- USE AT YOUR OWN PERIL!
## Added stand-alone support; may be used from the command line and from any directory.
## Added support for multiple image types
## Streamlined code to ensure minimal file shuffling.
## Corrected problem of TeXShop not auto-opening final .pdf product.
# runLBLL.engine is an .engine file for TeXShop and should be stored in ~/Library/TeXShop/Engine -- be sure to restart TeXShop
# You could store your .bib and .bst files in the /Misc directory or use TeXLive's ~/Library/texmf/bibtex/bib and ~/Library/texmf/bibtex/bib/bst, respectively, for global access
# This script will LaTeX-->BibTeX-->LaTeX-->LaTeX
# Credit:
## Jesper Kristensen for created the bulk of this code and inspiring me to take it further:
### http://jespertoftkristensen.com/JTK/Blog/Entries/2014/1/13_Organize_your_LaTeX_Project.html
## Karthick Sundararajan for the awesome pictorial directory tree formula
### http://karthicks.blogspot.com/2013/09/bash-sed-display-unix-directory.html
########################################
# SECTION: Dependencies.
########################################
# readlink or greadlink
# Bash 4
# Required directory structure (template)
## Project
## +--Figures
## | +--figure1.{eps|jpb|jpeg|pdf|png}
## | +--figure2.eps
## | +--figureN.eps
## +--LaTeX
## | +--main.tex
## | +--include1.tex
## | +--include2.tex
## | +--includeN.tex
## +--Misc
########################################
# SECTION: Prep the environment.
########################################
shopt -s nocasematch; # case of string match is irrelevant
# array to hold directory names (relative to projec dir)
declare -a dirs=(
"Misc"
"Figures"
)
# shitting the bed on paths with spaces?
texfile="$(greadlink -nf ${@})"
if [[ -e "${texfile}" && -f "${texfile}" && -s "${texfile}" ]]
then
echo "Exists. Is regular. Is non-zero."
else
echo "Someone setup us the bomb!!!!!!1"
exit 1
fi
dir_project="$(dirname "$(dirname "${texfile}")")"
dir_latex="$(basename "$(dirname "${texfile}")")"
# echo "${dir_latex}"
# echo -e "Absolute path of master tex file is ${texfile}"
# echo -e "Absolute path of dirbase is ${dir_project}"
# exit
########################################
# SECTION: Primary
########################################
# Check if the texfile, which is passed into this script, has a PWD of /LaTeX
if [[ "${dir_latex}" == "LaTeX" ]]; then
echo -e '\e[32mParent directory is /LaTeX and that is correct!\e[39m'
else
echo -e "\e[31mParent directory of ${texfile} must be /LaTeX. Perhaps you are running from the wrong directory?\e[39m"
echo -e "\e[31m${@: -1}\e[39m"
exit 1 # non-specific error code
fi
# Check if the directory ../Misc/ and ../Figures exist.
for i in "${dirs[@]}"
do
if [ -d "${dir_project}"/"$i" ]
then
echo -e "\e[32m../${i} exists!\e[39m"
else
echo -e "\e[31m../${i} does not exist! Making...\e[39m"
mkdir "${dir_project}"/"$i"
fi
done
texfile="${texfile%.*}" #This strips the extension
# PATHS MUST BE MADE ABSOLUTE!
function dolatex {
mv "${dir_project}"/misc/* "${dir_project}/latex/"
cp "${dir_project}"/misc/*{.eps,.jpg,.jpeg,.pdf,.png} "${dir_project}/latex/" # should try to copy any images with listed extensions
pdflatex --file-line-error --synctex=1 "${texfile}"
exit
}
function dobibtex {
bibtex "${texfile}"
}
function cleanup {
mv "${texfile}".pdf "${texfile}".pdf.keep
mv *{.eps,.jpg,.jpeg,.pdf,.png} ../Figures
mv "${texfile}".pdf.keep "${texfile}".pdf
ls -1 | grep -Ev ".*.(tex|pdf)$" | xargs -I {} mv {} ../Misc
mv "${texfile%.*}.pdf" ../
rm -f *-eps-converted-to.pdf
}
########################################
# SECTION: Auxiliary
########################################
# FUNCTION
#
# function tree {
# find .. -name '*' | sed -e 's/^/|-/' -e 's/[^-][^\/]*\//| /g' -e 's/| \([A-Za-z0-9_.]\)/| +--\1/'
# }
# This block of code is where the magic happens!
dolatex
echo -e "\e[32mLaTeX...complete.\e[39m"
dobibtex
echo -e "\e[32mBibTeX...complete.\e[39m"
dolatex
echo -e "\e[32mLaTeX...complete.\e[39m"
dolatex
echo -e "\e[32mLaTeX...complete.\e[39m"
cleanup
echo -e "\e[32mCleaning up...complete.\e[39m"
echo -e "\e[32mLooks like everything went off without a hitch. Please find your hot steaming .pdf in...\e[39m"
# This will spit out a pictorial version of the project directory tree
tree "${dir_project}" -CF -L 3 --noreport
# This is a fix to get texshop to open the new pdf file.
echo "Opening "${dir_project}/${texfile}".pdf"
open -a TeXShop -F "${dir_project}/${texfile}".pdf
###################################
# SECTION: To-do
###################################
# Currently, the assumption is that the script is run from the directory containing the tex files. This should be generalized.
# check for files outside their proper place
# need to address assumption about location of bib files
# if 'tree' does not exist, then use custom function
###################################
# SECTION: Meta...
###################################
# Local Variables:
# firestarter: "gist -u bee00d09a00e219ebbbb %p"
# End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment