Skip to content

Instantly share code, notes, and snippets.

@lorenzhs
Created March 4, 2015 11:50
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 lorenzhs/d76c2e65bf5150f0bb70 to your computer and use it in GitHub Desktop.
Save lorenzhs/d76c2e65bf5150f0bb70 to your computer and use it in GitHub Desktop.
LaTeX newcommand minimizer
#!/bin/sh
#
# by Lorenz Hübschle-Schneider and Jori Mäntysalo, 2013-2015
# license: cc-by-sa 3.0
# Based on http://tex.stackexchange.com/a/113156
#
# Remove any unnecessary \newcommand and \renewcommand from a LaTeX file
# and ensure that it still compiles
> notneeded
> needed
out=${2:-tmp}
# determine which commands are not in use
# if you have too many of them you might want to remove the & in line 13 :>
fgrep '\newcommand' $1 | cut -f 2 -d '{' | cut -f 1 -d '}' |
while read a; do
echo "checking whether command $a is needed";
egrep -v "\\newcommand\{\\\\$a\}" $1 > ${out}_${a}.tex
pdflatex -halt-on-error ${out}_${a}.tex &&
echo $a >> notneeded || echo $a >> needed &
done
wait
# remove unneeded commands
cp $1 ${out}.tex
for a in $(cat notneeded); do
echo "removing command $a"
egrep -v "\\(re)?newcommand\{\\\\$a\}" ${out}.tex > ${out}.tmp
mv ${out}.tmp ${out}.tex
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment