Created
March 4, 2015 11:50
-
-
Save lorenzhs/d76c2e65bf5150f0bb70 to your computer and use it in GitHub Desktop.
LaTeX newcommand minimizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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