This note summarizes the steps to create a robust LaTeX compile chain command with Zsh autocompletion.
1. The Function
Instead of a complex alias, define a Zsh function in your ~/.zshrc file. This is more reliable for the completion system to detect.
# LaTeX compile chain function
function pdflatexchain() {
# Check if an argument was provided
if [[ -z "$1" ]]; then
echo "Usage: pdflatexchain <filename_without_extension>"
return 1
fi
pdflatex "$1.tex" && bibtex "$1" && pdflatex "$1.tex" && pdflatex "$1.tex"
}2. The Completion Script
This script enables Tab completion for the pdflatexchain function, suggesting any .tex files in the current directory.
- Create the file: Save the following code in a file named
_pdflatexchain.
#compdef pdflatexchain
_pdflatexchain() {
# Find all *.tex files and suggest their basenames (without extension)
compadd -- *.tex(N:r)
}
# Associate the completion function with the command
compdef _pdflatexchain pdflatexchain3. Installation and Setup
- Create a completions directory if you don’t have one:
mkdir -p ~/.zsh/completions - Move the script into that directory:
mv _pdflatexchain ~/.zsh/completions/ - Update your
.zshrc: Add your new completions directory to your Zshfpath. This line must come beforecompinitis called (which is often done by frameworks like Oh My Zsh).fpath=(~/.zsh/completions $fpath) - Reload your shell to apply the changes:
exec zsh
Now, typing pdflatexchain and pressing Tab will autocomplete with the names of your .tex files.