commit bf8b38346dd455c8cd43dabb66d23602612c222e Author: wassname Date: Thu Jan 29 20:19:59 2026 +0800 init diff --git a/diagrams/TEMPLATE.typ b/diagrams/TEMPLATE.typ new file mode 100644 index 0000000..e807732 --- /dev/null +++ b/diagrams/TEMPLATE.typ @@ -0,0 +1,70 @@ +// Diagram Name: [Your Diagram Title] +// +// Source: /path/to/source/file.py:LINE_START-LINE_END +// Created: YYYY-MM-DD +// +// Concept: +// Brief explanation of what this diagram visualizes. Describe the key +// mathematical or computational concept being illustrated. +// +// Key Equations: +// L = f(x, y) where... +// +// Visualization Style: +// - Geometric (vectors in subspace) +// - Flowchart (computational steps) +// - Annotated (functions with labels) +// +// References: +// - Paper: [Title] (arxiv.org/abs/...) +// - Documentation: https://... +// - Related diagrams: diagrams/other/file.typ +// +// Notes: +// - Any implementation-specific details +// - Color coding explanations +// - Known limitations or simplifications + +#import "@preview/cetz:0.4.2" + +#set page(width: auto, height: auto, margin: 1.5cm) + +// Optional: Make math equations more readable +#show math.equation: block.with(fill: white, inset: 2pt) + +#align(center)[ + #text(size: 16pt, weight: "bold")[Your Diagram Title] +] +#align(center)[ + #text(size: 11pt)[Subtitle or brief description] +] + +#v(0.5cm) + +#cetz.canvas(length: 1.2cm, { + import cetz.draw: * + + // Centralized styling + set-style( + stroke: (thickness: 1.2pt), + mark: (fill: black, scale: 1.5), + content: (padding: 2pt) + ) + + // Your diagram code here + // Example: + circle((0, 0), radius: 2, stroke: (paint: blue, thickness: 2pt)) + content((0, 0), [Your content]) +}) + +// Optional: Add formula or explanation below +#v(0.5cm) +#align(center)[ + #box(fill: rgb("#F5F5F5"), inset: 10pt, radius: 5pt)[ + #text(size: 11pt)[ + Mathematical formulation or key insights + + $ cal(L) = sum_i f(x_i) $ + ] + ] +] diff --git a/diagrams/examples/example_tikz.tex b/diagrams/examples/example_tikz.tex new file mode 100644 index 0000000..c62326f --- /dev/null +++ b/diagrams/examples/example_tikz.tex @@ -0,0 +1,39 @@ +\documentclass[tikz,border=10pt]{standalone} +\usepackage{tikz} +\usetikzlibrary{arrows.meta,positioning,shapes.geometric,calc} + +\begin{document} +\begin{tikzpicture}[ + node distance=2cm, + box/.style={rectangle, draw, minimum width=2.5cm, minimum height=1cm, align=center}, + arrow/.style={-Stealth, thick} +] + +% Example: Inner Contrastive Loss Flow +\node[box, fill=blue!20] (input) {Hidden States\\$h_s$}; +\node[box, fill=green!20, below left=of input] (pos) {Positive\\$h_{pos}$}; +\node[box, fill=red!20, below right=of input] (neg) {Negative\\$h_{neg}$}; + +\node[box, fill=yellow!20, below=3cm of input] (proj) {Projection\\$\mathbf{V}$}; + +\node[box, fill=purple!20, below left=1.5cm and 1cm of proj] (in) {In-Space\\Energy}; +\node[box, fill=orange!20, below right=1.5cm and 1cm of proj] (null) {Null-Space\\Energy}; + +\node[box, fill=gray!20, below=2cm of proj] (loss) {Combined Loss\\$\mathcal{L}$}; + +% Arrows +\draw[arrow] (input) -- (pos); +\draw[arrow] (input) -- (neg); +\draw[arrow] (pos) -- (proj); +\draw[arrow] (neg) -- (proj); +\draw[arrow] (proj) -- (in); +\draw[arrow] (proj) -- (null); +\draw[arrow] (in) -- (loss); +\draw[arrow] (null) -- (loss); + +% Labels +\node[above right=0.1cm of input, font=\small] {Split}; +\node[left=0.1cm of proj, font=\small, text width=2cm] {Project to\\subspace}; + +\end{tikzpicture} +\end{document} diff --git a/diagrams/examples/example_typst.png b/diagrams/examples/example_typst.png new file mode 100644 index 0000000..bcedae1 Binary files /dev/null and b/diagrams/examples/example_typst.png differ diff --git a/diagrams/examples/example_typst.typ b/diagrams/examples/example_typst.typ new file mode 100644 index 0000000..9f1f626 --- /dev/null +++ b/diagrams/examples/example_typst.typ @@ -0,0 +1,61 @@ +#import "@preview/cetz:0.4.2" + +#set page(width: auto, height: auto, margin: 1cm) + +// Example: Neural Network Loss Visualization +#align(center)[ + #text(size: 14pt, weight: "bold")[Inner Contrastive Loss Flow] +] + +#v(0.5cm) + +#cetz.canvas({ + import cetz.draw: * + + // Set up style + set-style( + stroke: (thickness: 1.5pt), + mark: (end: ">", fill: black), + ) + + // Input layer + rect((-2, 6), (2, 8), fill: rgb("#BBDEFB"), stroke: black, name: "input") + content((0, 7), [Hidden States]) + content((0, 5.5), [$h_s$]) + + // Positive and negative branches + rect((-6, 3), (-2, 5), fill: rgb("#C8E6C9"), stroke: black, name: "pos") + content((-4, 4), [Positive]) + content((-4, 2.5), [$h_"pos"$]) + + rect((2, 3), (6, 5), fill: rgb("#FFCDD2"), stroke: black, name: "neg") + content((4, 4), [Negative]) + content((4, 2.5), [$h_"neg"$]) + + // Projection + rect((-3, -1), (3, 1), fill: rgb("#FFF9C4"), stroke: black, name: "proj") + content((0, 0), [Projection]) + content((0, -1.5), [$bold(V)$]) + + // Energy computation + rect((-7, -5), (-3, -3), fill: rgb("#E1BEE7"), stroke: black, name: "in") + content((-5, -4), [In-Space]) + + rect((3, -5), (7, -3), fill: rgb("#FFCCBC"), stroke: black, name: "null") + content((5, -4), [Null-Space]) + + // Final loss + rect((-2, -9), (2, -7), fill: rgb("#E0E0E0"), stroke: black, name: "loss") + content((0, -8), [Loss]) + content((0, -9.5), [$cal(L)$]) + + // Arrows + line((0, 6), (-4, 5), mark: (end: ">")) + line((0, 6), (4, 5), mark: (end: ">")) + line((-4, 3), (0, 1), mark: (end: ">")) + line((4, 3), (0, 1), mark: (end: ">")) + line((-1.5, -1), (-5, -3), mark: (end: ">")) + line((1.5, -1), (5, -3), mark: (end: ">")) + line((-5, -5), (0, -7), mark: (end: ">")) + line((5, -5), (0, -7), mark: (end: ">")) +}) diff --git a/diagrams/examples/test_simple.png b/diagrams/examples/test_simple.png new file mode 100644 index 0000000..4582294 Binary files /dev/null and b/diagrams/examples/test_simple.png differ diff --git a/diagrams/examples/test_simple.typ b/diagrams/examples/test_simple.typ new file mode 100644 index 0000000..35d4bee --- /dev/null +++ b/diagrams/examples/test_simple.typ @@ -0,0 +1,19 @@ +#import "@preview/cetz:0.4.2" + +#set page(width: auto, height: auto, margin: 1cm) + +#align(center)[*Simple CeTZ Test*] + +#cetz.canvas({ + import cetz.draw: * + + // Simple boxes and arrows + rect((0, 0), (2, 1), fill: blue.lighten(80%), stroke: black, name: "box1") + content((1, 0.5), [Box 1]) + + rect((0, -2), (2, -1), fill: red.lighten(80%), stroke: black, name: "box2") + content((1, -1.5), [Box 2]) + + // Arrow between them + line((1, 0), (1, -1), mark: (end: ">"), stroke: black + 1.5pt) +}) diff --git a/render_diagram.sh b/render_diagram.sh new file mode 100755 index 0000000..403f4a5 --- /dev/null +++ b/render_diagram.sh @@ -0,0 +1,101 @@ +#!/bin/bash +# Diagram rendering script - supports TikZ, Typst/CeTZ, and SVG + +set -e + +USAGE="Usage: $0 [output.png] + +Supported formats: + .tex - TikZ/LaTeX diagrams (requires pdflatex + imagemagick/poppler) + .typ - Typst/CeTZ diagrams (requires typst) + .svg - SVG diagrams (requires inkscape or imagemagick) + +Examples: + $0 diagram.tex # Creates diagram.png + $0 diagram.typ output.png # Creates output.png +" + +if [ $# -lt 1 ]; then + echo "$USAGE" + exit 1 +fi + +INPUT="$1" +EXT="${INPUT##*.}" +BASENAME="${INPUT%.*}" + +# Determine output file +if [ $# -ge 2 ]; then + OUTPUT="$2" +else + OUTPUT="${BASENAME}.png" +fi + +echo "Rendering: $INPUT -> $OUTPUT" + +case "$EXT" in + tex) + # TikZ/LaTeX workflow + if ! command -v pdflatex &> /dev/null; then + echo "Error: pdflatex not found. Install with:" + echo " sudo apt-get install texlive texlive-pictures" + exit 1 + fi + + # Compile to PDF + pdflatex -interaction=nonstopmode -output-directory="$(dirname "$INPUT")" "$INPUT" > /dev/null + PDF="${BASENAME}.pdf" + + # Convert PDF to PNG + if command -v pdftoppm &> /dev/null; then + # Use poppler (cleaner, better quality) + pdftoppm "$PDF" "${BASENAME}" -png -singlefile -r 300 + mv "${BASENAME}.png" "$OUTPUT" + elif command -v convert &> /dev/null; then + # Use ImageMagick + convert -density 300 "$PDF" -quality 90 "$OUTPUT" + else + echo "Error: Need pdftoppm (poppler-utils) or convert (imagemagick)" + echo "Install with: sudo apt-get install poppler-utils" + exit 1 + fi + + # Cleanup LaTeX aux files + rm -f "${BASENAME}.aux" "${BASENAME}.log" "${BASENAME}.pdf" + echo "Created: $OUTPUT" + ;; + + typ) + # Typst workflow + if ! command -v typst &> /dev/null; then + echo "Error: typst not found. Install with:" + echo " curl -fsSL https://typst.community/typst-install/install.sh | sh" + echo "Or: cargo install --git https://github.com/typst/typst" + exit 1 + fi + + # Compile to PNG directly + typst compile "$INPUT" "$OUTPUT" --ppi 300 + echo "Created: $OUTPUT" + ;; + + svg) + # SVG workflow + if command -v inkscape &> /dev/null; then + inkscape "$INPUT" --export-type=png --export-filename="$OUTPUT" --export-dpi=300 + elif command -v convert &> /dev/null; then + convert -density 300 -background white -alpha remove "$INPUT" "$OUTPUT" + else + echo "Error: Need inkscape or imagemagick" + echo "Install with: sudo apt-get install inkscape" + exit 1 + fi + echo "Created: $OUTPUT" + ;; + + *) + echo "Error: Unsupported file extension: .$EXT" + echo "$USAGE" + exit 1 + ;; +esac