Skip to content

Instantly share code, notes, and snippets.

@JCGoran
Created February 11, 2021 19:59
Show Gist options
  • Save JCGoran/c553c472fc4a6317bf4f9a2583af538a to your computer and use it in GitHub Desktop.
Save JCGoran/c553c472fc4a6317bf4f9a2583af538a to your computer and use it in GitHub Desktop.
Latex <--> Gitlab markdown converter
latex_to_gitlab(){
if [ $# -ne 1 ]
then
printf "usage: latex_to_gitlab [FILE]\n"
return 1
fi
perl -077pi.bak -e 's/(\s)\$\$(.+?)\$\$/\1```math\2```/sg' "${1}"
perl -077pi.bak -e 's/(\s)\$(.+?)\$/\1\$`\2`\$/sg' "${1}"
return 0
}
# inverse of the above
gitlab_to_latex(){
if [ $# -ne 1 ]
then
printf "usage: gitlab_to_latex [FILE]\n"
return 1
fi
perl -077pi.bak -e 's/(\s)```\math(.+?)```/\1\$\$\2\$\$/sg' "${1}"
perl -077pi.bak -e 's/(\s)\$`(.+?)`\$/\1\$\2\$/sg' "${1}"
return 0
}

Simple Perl scripts which use regex search and replace to in-place convert Latex markdown files (which use $[MATH]$ and $$[MATH]$$) to Gitlab markdown (which uses $`[MATH]`$ and ```math [MATH]``` and vice versa.

One caveat: the $$[MATH]$$ style should have the math environment delimiters on separate newlines, so this works:

$$
[MATH]
$$

but the below and similar variations don't:

$$[MATH]$$

Additionally, the very first character in the file should not be $.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment