From 01f328be313912e6a77fc942c910aa5aa6c29338 Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Fri, 21 Apr 2023 21:48:27 -0400 Subject: [PATCH] nvim: add things to make Inkscape figures in Typst --- src/.config/nvim/init.vim | 7 +++ src/.config/nvim/ultisnips/typst.snippets | 31 ++++++++++++ src/.local/bin/svg-typst | 51 ++++++++++++++++++++ src/.local/bin/typst-figure | 58 +++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 src/.config/nvim/ultisnips/typst.snippets create mode 100755 src/.local/bin/svg-typst create mode 100755 src/.local/bin/typst-figure diff --git a/src/.config/nvim/init.vim b/src/.config/nvim/init.vim index 5b2e6a3..adfa398 100755 --- a/src/.config/nvim/init.vim +++ b/src/.config/nvim/init.vim @@ -116,6 +116,13 @@ nnoremap ZF :qa " copy URL under cursor to clipboard bind :nnoremap uu :let @+ = expand('') +" edit figure in Inkscape +function EditFig() + let figure_fname = expand('') + exec "silent !typst-figure " .. figure_fname +endfunc + +:nnoremap ff :call EditFig() " Plugins " Run PlugInstall if there are missing plugins diff --git a/src/.config/nvim/ultisnips/typst.snippets b/src/.config/nvim/ultisnips/typst.snippets new file mode 100644 index 0000000..f57bc28 --- /dev/null +++ b/src/.config/nvim/ultisnips/typst.snippets @@ -0,0 +1,31 @@ +snippet problem "template for problem notes" bi +#import "../templates/problems.typ": template, source_code, status +#show: template.with( + title: "$1", + problem_url: "$2", + stat: "${3:incomplete}", +) + +endsnippet + +snippet math_prob "template for math problems" bi +#import "../../../templates/math_prob.typ": template +#show: template.with( + title: "$1", +) + +endsnippet + +snippet ss "superscript" i +^$1 +endsnippet +snippet im "inline math" w +\$${1:${VISUAL}}\$ +endsnippet + +snippet fig "create new figure" bi +#figure( + image("fig/${0:name}.svg"), + caption: [$2], +) <${3:identifier}> +endsnippet diff --git a/src/.local/bin/svg-typst b/src/.local/bin/svg-typst new file mode 100755 index 0000000..a9b1879 --- /dev/null +++ b/src/.local/bin/svg-typst @@ -0,0 +1,51 @@ +#!/bin/sh +# Write Typst markup, then copy it as an SVG. +# This can be used to paste math labels in Inkscape. + +# Requires +# -------- +# pdf2svg +# typst +# xclip +# $EDITOR set to an editor (prefer nvim) + +# create temp files +TMP_SRC="$(mktemp --suffix=.typ)" +TMP_PDF="$(mktemp --suffix=.pdf)" +TMP_SVG="$(mktemp --suffix=.svg)" + +# Typst template +cat <<'EOF' > "$TMP_SRC" +#set page( + width: 10cm, + height: auto, + margin: (x: 1cm, y: 1cm) +) + +#show math.equation: eq => scale(x: 150%, y: 150%, text(font: "Fira Math", size: 17pt, eq)) +#show text: set text(font: "Fira Math", size: 25pt) + +$ $ +EOF + +# edit Typst source +if [ "$EDITOR" = "nvim" ] || [ "$EDITOR" = "vim" ]; then + # if we're using vim we can jump to the end of the template + $EDITOR -c "normal Gll" -c "startinsert" "$TMP_SRC" +else + $EDITOR "$TMP_SRC" +fi + +# compile to pdf +typst c "$TMP_SRC" "$TMP_PDF" + +# convert to svg +pdf2svg "$TMP_PDF" "$TMP_SVG" + +# copy (x-inkscape-svg necessary otherwise it rasterizes the image when pasting) +xclip -selection clipboard -t image/x-inkscape-svg -i "$TMP_SVG" + +# clean up +rm "$TMP_SRC" +rm "$TMP_PDF" +rm "$TMP_SVG" diff --git a/src/.local/bin/typst-figure b/src/.local/bin/typst-figure new file mode 100755 index 0000000..d741a3e --- /dev/null +++ b/src/.local/bin/typst-figure @@ -0,0 +1,58 @@ +#!/bin/sh +# Figure manager for typst. +# Run this from Vim and it will edit a figure with the given filename in Inkscape. + +SVG_PATH="$1" +mkdir -p fig/ + +if [ -e fig/"$1" ]; then + # just edit it + inkscape "$SVG_PATH" & +else + # otherwise, copy this template file and edit it + cat <<'EOF' > "$SVG_PATH" + + + + + + + + +EOF + inkscape "$SVG_PATH" & +fi