Skip to content

Instantly share code, notes, and snippets.

@aliparsai
Last active May 30, 2022 12:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aliparsai/46aad35544e128d72dee6cff44b2af21 to your computer and use it in GitHub Desktop.
Save aliparsai/46aad35544e128d72dee6cff44b2af21 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 ali@parsai.net
##
## 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