From d6b58e7e8301e22e6f72ced7b87101e2e7a20292 Mon Sep 17 00:00:00 2001 From: Yan Lin Date: Sat, 29 Nov 2025 00:02:06 +0100 Subject: [PATCH] add mksvg function --- modules/homebrew.nix | 1 + modules/tex.nix | 70 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/modules/homebrew.nix b/modules/homebrew.nix index eaeaee6..48ce6c3 100644 --- a/modules/homebrew.nix +++ b/modules/homebrew.nix @@ -36,6 +36,7 @@ "localsend" "calibre" "linearmouse" + "omnigraffle" ]; taps = [ # Additional repositories if needed diff --git a/modules/tex.nix b/modules/tex.nix index b0a723c..e2ff0c5 100644 --- a/modules/tex.nix +++ b/modules/tex.nix @@ -23,24 +23,23 @@ if [[ -z "$tex_file" ]]; then # Build all .tex files in current directory - local tex_files=(*.tex) - if [[ ''${#tex_files[@]} -eq 0 ]] || [[ ! -f "''${tex_files[0]}" ]]; then - echo "No .tex files found in current directory" - return 1 - fi - - for file in "''${tex_files[@]}"; do + local found=0 + for file in *.tex(N); do + found=1 echo "Building $file..." latexmk -pdf -bibtex -shell-escape -interaction=nonstopmode \ -output-directory="$output_dir" -f "$file" - # Copy PDF to current directory local basename="''${file%.tex}" if [[ -f "$output_dir/$basename.pdf" ]]; then cp "$output_dir/$basename.pdf" "./" echo "Copied $basename.pdf to current directory" fi done + if [[ $found -eq 0 ]]; then + echo "No .tex files found in current directory" + return 1 + fi else if [[ ! -f "$tex_file" ]]; then echo "File not found: $tex_file" @@ -59,6 +58,61 @@ fi } + # Generate standalone PDF from LaTeX equation + # Usage: mksvg + # Example: mksvg 'E = mc^2' energy + function mksvg() { + local equation="''${1}" + local filename="''${2}" + + if [[ -z "$equation" ]] || [[ -z "$filename" ]]; then + echo "Usage: mksvg " + echo "Example: mksvg 'E = mc^2' energy" + return 1 + fi + + printf '%s\n' "$equation" > "''${filename}.texeq" + + local temp_tex="''${filename}_temp.tex" + { + printf '%s\n' '\documentclass{standalone}' + printf '%s\n' '\usepackage{amsmath}' + printf '%s\n' '\usepackage{amssymb}' + printf '%s\n' '\begin{document}' + printf '%s%s%s\n' '$' "$equation" '$' + printf '%s\n' '\end{document}' + } > "$temp_tex" + + pdflatex -interaction=nonstopmode "$temp_tex" > /dev/null 2>&1 + + if [[ -f "''${filename}_temp.pdf" ]]; then + mv "''${filename}_temp.pdf" "''${filename}.pdf" + echo "Generated ''${filename}.pdf" + else + echo "Failed to generate PDF" + rm -f "$temp_tex" + return 1 + fi + + rm -f "$temp_tex" "''${filename}_temp.aux" "''${filename}_temp.log" + } + + # Regenerate PDFs from all .texeq files in current directory + function mksvg-all() { + local found=0 + for file in *.texeq(N); do + found=1 + local filename="''${file%.texeq}" + local equation="$(cat "$file")" + echo "Regenerating ''${filename}.pdf..." + mksvg "$equation" "$filename" + done + if [[ $found -eq 0 ]]; then + echo "No .texeq files found in current directory" + return 1 + fi + } + # Continuous compilation mode - watch and auto-rebuild # Usage: mkpdf-watch function mkpdf-watch() {