Skip to content

Instantly share code, notes, and snippets.

@paolobrasolin
Last active February 2, 2022 12:15
Show Gist options
  • Save paolobrasolin/67be84135ed544c445fb2c9ddc62df68 to your computer and use it in GitHub Desktop.
Save paolobrasolin/67be84135ed544c445fb2c9ddc62df68 to your computer and use it in GitHub Desktop.
Scripts to embed LaTeX in Markdown documents

Hi! This is a pair of scripts to easily embed LaTeX figures in Markdown documents.

Before using them, please

  1. modify TEX_DIR to be the folder where you want to keep the LaTeX stuff (./attachments/diagrams will be fine if you're using Obsidian);
  2. modify PREAMBLE and POSTAMBLE the LaTeX code to sandwitch the code of your figures before compilation;
  3. carefully read the scripts, as you're running Bash given to you by a stranger.

Then, you can use them like this:

# Extract figures from all your source files and update them
for file in **/*.md; do
  # This is overwriting the old files!
  cat file | ./diag-pluck.sh > diag-temp.md
  mv diag-temp.md file
done

# Check into your TEX_DIR
cd "./attachments/diagrams"

# Compile everything
latexmk

From time to time, you'll want to unclog your TEX_DIR by running something like

./diag-flush.sh **/*.md

Enjoy!

#!/usr/bin/env bash
# USAGE:
# ./diag-flush.sh **/*.md
# DESCRIPTION:
# This utility gets a list of Markdown files,
# gets the filenames of images with multiline alt text
# which are in TEX_DIR, and deletes all files with
# names that have no match in TEX_DIR.
TEX_DIR="./attachments/diagrams"
# NOTE: we're not regexp-escaping the leading dot in TEX_DIR,
# so there are some icky edge cases
readarray -d '' -t HASHES_TO_KEEP < <(grep --null -ohP "(?<=\]\(${TEX_DIR})[a-z0-9]{32}(?=.png\))" "$@")
echo $HASHES_TO_KEEP
shopt -s nullglob
for filename in "$TEX_DIR"/*; do
echo $filename
hash=${filename##*/}
hash=${hash%%.*}
if [[ ! ${HASHES_TO_KEEP[*]} =~ $hash ]]
then
echo Deleting $filename
rm $filename
fi
done
#!/usr/bin/env bash
# USAGE:
# cat input.md | ./diag-pluck.sh > output.md
# DESCRIPTION:
# This utility reads a Markdown file from stdin
# and outputs a Markdown file to stdout.
# All images with multiline alt text are detected, e.g.
#
# ![
# % Write any LaTeX code here.
# %
# % Leave no blank lines! Use a single if you need to.
# ](./some/path/to/file.png)
#
# and their alt text is enveloped in a template, hashed
# and saved to a file in TEX_DIR.
# The image tag url in the output is modified and points
# to the homonimous image file in TEX_DIR.
TEX_DIR="./attachments/diagrams"
PREAMBLE="\documentclass{standalone}
\begin{document}
"
POSTAMBLE="\end{document}
"
reading=0
code=()
while IFS= read -r line; do
if [[ "$line" =~ ^!\[$ ]]; then
reading=1
code=()
echo "$line"
elif [[ "$line" =~ ^\]\(.*\)$ ]]; then
reading=0
code="${PREAMBLE}${code}${POSTAMBLE}"
md5=$(echo "$code" | md5sum | cut -d ' ' -f 1)
echo -n "$code" > "${TEX_DIR}/${md5}.tex"
echo "](${TEX_DIR}/${md5}.png)"
elif [ "$reading" = 1 ]; then
code+="${line}
" # this newline is not a typo!
echo "$line"
else
echo "$line"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment