Skip to content

Instantly share code, notes, and snippets.

@vadimkantorov
Last active February 11, 2021 16:58
Show Gist options
  • Save vadimkantorov/0f4dce34cc9879958cdf1454be9ea921 to your computer and use it in GitHub Desktop.
Save vadimkantorov/0f4dce34cc9879958cdf1454be9ea921 to your computer and use it in GitHub Desktop.
A Bash function to strip %-style LaTeX comments. The script correctly treats escaped percents, and comments after double backslashes.
# Usage:
# strip_latex_comments file.tex file.nocomments.tex
# strip_latex_comments_inplace file.tex
function strip_latex_comments {cat "$1" | '"sed 's/\([^\\]\|^\)\(\(\\\\\)*\)%.*/\1\2%/g' "'> "$2"; }
function strip_latex_comments_inplace {find . -name '*.tex' -exec sed -i'' 's/\([^\\]\|^\)\(\(\\\\\)*\)%.*/\1\2%/g' {} \; }
alias stip_latex_comments2="sed -e 's/\([^\\]\|^\)\(\(\\\\\)*\)%.*/\1\2%/g'"
# strip_latex_comments2 file.tex > file.nocomments.tex
function strip_latex_comments_inplace2 {sed -i'' -e 's/\([^\\]\|^\)\(\(\\\\\)*\)%.*/\1\2%/g' $(find $1 -name '*.tex') }
# strip_latex_comments_inplace2 mytexdir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment