Skip to content

Instantly share code, notes, and snippets.

@stormvirux
Forked from aliparsai/comment-cleanup.sh
Last active May 30, 2022 12:03
Show Gist options
  • Select an option

  • Save stormvirux/1ae3dc19a116964f03abdf6ee0934738 to your computer and use it in GitHub Desktop.

Select an option

Save stormvirux/1ae3dc19a116964f03abdf6ee0934738 to your computer and use it in GitHub Desktop.
Remove comments from Latex files
#!/bin/bash
## Clean-up Comments from Latex Source Files
## Copyright (c) 2020 Ali Parsai [email protected]
## Copyright (c) 2021 Thaha Mohammed
##
## Dependencies: sponge from moreutils, latexpand, sed, cat, mv, date, cp
##
## CAUTION: Use version control or backup your files before using this script.
##
## How to use: comment-cleanup.sh TEX_FILE(S)
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <https://www.gnu.org/licenses/>.
##
check_dependency () {
if type $1 ; then
echo "==> $1 found."
else
echo "==> $1 not found. Exitting."
exit 1
fi
}
check_dependency mv
check_dependency cp
check_dependency date
check_dependency cat
check_dependency sed
check_dependency sponge
check_dependency latexpand
#dt=$(date '+%d%m%Y-%H%M%S')
#for src in "$@"
# do
# backup="${src}.bak.${dt}"
# cp -v "$src" "$backup"
# done
for src in "$@"
do
dest="${src/.tex/-stripped.tex}"
latexpand --empty-comments --keep-includes "$src" > "$dest"
sed -i '/^\s*%/d' "$dest"
cat -s "$dest" | sponge "$dest"
mv "$dest" "$src"
echo "==> Processed: $src"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment