Skip to content

Instantly share code, notes, and snippets.

@meereeum
Last active December 4, 2019 23:39
Show Gist options
  • Save meereeum/d14cfd9c17e8abda5d0a09eed477bd27 to your computer and use it in GitHub Desktop.
Save meereeum/d14cfd9c17e8abda5d0a09eed477bd27 to your computer and use it in GitHub Desktop.
bash util for making your tex arXivable
#!/usr/bin/env bash
# this script gets your latex ready for arxiv submission by
# (1) sanitizing your *.tex of embarrassing comments,
# (2) arxiv-proofing filenames, &
# (3) gzipping everything needed into a convenient .tar.gz
HELPFLAG="-h"
FIGFLAG="-d"
OUTFLAG="-o"
usage () {
cat <<HELP_USAGE
Usage: $( basename $0 ) [ $HELPFLAG ] [ $FIGFLAG <figdirname> ] [ $OUTFLAG <myfavzipdir> ] [ <indir> ]
args:
indir optional path/to/input/dir (default: ".")
kwargs:
$FIGFLAG figdirname optional name of figs dir, i.e. under indir (default: "figs")
$OUTFLAG myfavzipdir optional path/to/zipfile (default: "./arxiv<datetoday>")
flags:
$HELPFLAG print this docstring & exit
HELP_USAGE
}
# stay OR help & leave
do_help=$( echo "$@" | grep -Ec "(^|[[:space:]])${HELPFLAG/-/\\-}\b" )
(( "$do_help" )) && { usage; exit 1; }
DATE=$( date +%y%m%d ) # yymmdd
DEFAULTDIR="arxiv$DATE" # default directory name
DEFAULTFIGS="figs" # default figs (or other additional) directory
DEFAULTINDIR="." # default input directory
ARGS="$@"
extract_flag(){
FLAG="$1" # with optional space
echo "$ARGS" | sed -rn "s/^.*$FLAG ?(\w*).*/\1/p" # extract $FLAG
}
# allow user to specify figs directory (or fall back to default)
FIGS=$( extract_flag "$FIGFLAG" )
[[ "$FIGS" ]] || FIGS="$DEFAULTFIGS"
# " " " " outdir/outzipfile
DIR=$( extract_flag "$OUTFLAG" )
[[ "$DIR" ]] || DIR="$DEFAULTDIR"
# " " " " path/to/input (directory of texfiles)
# extract positional arg & remove trailing "/"
INDIR=$( echo "$@" | sed -re"s/ ?($FIGFLAG|$OUTFLAG) ?\w* ?//g" -e"s/\/$//" )
[[ "$INDIR" ]] || INDIR="$DEFAULTINDIR"
[[ -d "$INDIR" ]] || { echo "Not a valid path.. try again"; exit 0; }
mkdir -p $DIR
for f in "$INDIR/"*tex; do
fsafe=$( basename "$f" |
sed -E 's/[^a-zA-Z0-9_+-.,=]/_/g' ) # disallowed chars -> "_"
grep -v '^ *%' "$f" | cat -s | # uncomment #1: whole lines (& rm extra blank lines)
sed 's/%.*//g' > "$DIR/$fsafe" # uncomment #2: end-of-line
done
cp -r "$INDIR/"{*sty,*bbl,$FIGS} "$DIR"
tar -czvf "$DIR".tar.gz "$DIR" # compress ze vucking files
echo -e "\nprocessed files from $INDIR (including $FIGS) --> $DIR\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment