From 468d8c34bba6afca2cc1f86fca5a399f8220d492 Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Thu, 16 Jan 2025 08:13:06 -0500 Subject: [PATCH] add k-space.typ auto-add tags cetz/tikz to diagram.metadata.tags based on file extension --- .../bose-einstein-distribution.typ | 1 - .../fermi-dirac-vs-temp.typ | 1 - assets/isotherms/isotherms.typ | 1 - assets/k-space/k-space.typ | 61 +++++++++++++++ assets/light/light.typ | 2 +- site/src/lib/Card.svelte | 3 +- site/src/lib/Tags.svelte | 7 +- site/src/lib/index.ts | 76 +++++++++++++++++++ site/src/lib/stores.ts | 72 +----------------- site/src/routes/+layout.svelte | 2 +- site/src/routes/+page.svelte | 19 ++--- 11 files changed, 153 insertions(+), 92 deletions(-) create mode 100644 assets/k-space/k-space.typ diff --git a/assets/bose-einstein-distribution/bose-einstein-distribution.typ b/assets/bose-einstein-distribution/bose-einstein-distribution.typ index 78cdaab5..d1d2550a 100644 --- a/assets/bose-einstein-distribution/bose-einstein-distribution.typ +++ b/assets/bose-einstein-distribution/bose-einstein-distribution.typ @@ -12,7 +12,6 @@ } #canvas({ - // Set up the plot draw.set-style(axes: (y: (label: (anchor: "south-east", angle: 90deg)), x: (label: (anchor: "north-east")))) plot.plot( diff --git a/assets/fermi-dirac-vs-temp/fermi-dirac-vs-temp.typ b/assets/fermi-dirac-vs-temp/fermi-dirac-vs-temp.typ index f8441712..95dfb323 100644 --- a/assets/fermi-dirac-vs-temp/fermi-dirac-vs-temp.typ +++ b/assets/fermi-dirac-vs-temp/fermi-dirac-vs-temp.typ @@ -10,7 +10,6 @@ } #canvas({ - // Set up the plot draw.set-style( axes: ( y: (label: (anchor: "south-west")), diff --git a/assets/isotherms/isotherms.typ b/assets/isotherms/isotherms.typ index 77a354f9..fdaee22b 100644 --- a/assets/isotherms/isotherms.typ +++ b/assets/isotherms/isotherms.typ @@ -16,7 +16,6 @@ #let p2(v) = p1(v) + B2 / calc.pow(v, 3) #canvas({ - // Set up the plot draw.set-style( axes: ( y: (label: (anchor: "north-west", offset: -0.2)), diff --git a/assets/k-space/k-space.typ b/assets/k-space/k-space.typ new file mode 100644 index 00000000..ce30dc8b --- /dev/null +++ b/assets/k-space/k-space.typ @@ -0,0 +1,61 @@ +#import "@preview/cetz:0.3.1": canvas, draw + +#set page(width: auto, height: auto, margin: 8pt) + +#let x-range = 3 +#let y-range = 3 +#let ratio = 3 / 4 + +#canvas({ + // Set up coordinate system + import draw: circle, content, line + + // Draw blue circle with fill in background + circle( + (0, 0), + radius: 2 / 3 * y-range, + stroke: blue, + fill: blue.lighten(80%), + fill-opacity: 0.2, + name: "fermi-circle", + ) + + // Draw dot grid + for x in range(-x-range, x-range + 1) { + for y in range(-y-range, y-range + 1) { + circle((x, ratio * y), radius: 2pt, fill: black) + } + } + + // Draw axes with arrows + let arrow-style = (mark: (end: "stealth", fill: black)) + let x-end = (x-range + 0.5, 0) + let y-end = (0, ratio * y-range + 0.5) + + line((-x-range - 0.5, 0), x-end, ..arrow-style) + line((0, -ratio * y-range - 0.5), y-end, ..arrow-style, name: "y-axis") + + // Add axis labels + content(x-end, $k_x$, anchor: "south-west") + content((rel: (.55, 0), to: "y-axis.end"), $k_y$, anchor: "north-east") + + // Draw lattice spacing indicators with arrows + let spacing-arrow = (mark: (start: "stealth", end: "stealth", fill: black, scale: .3, offset: 0.1), stroke: 0.7pt) + let x-start = (x-range - 1, -ratio * y-range) + let x-end = (x-range, -ratio * y-range) + let x-mid = ((x-start.at(0) + x-end.at(0)) / 2, x-start.at(1)) + + line(x-start, x-end, ..spacing-arrow, name: "x-spacing") + content("x-spacing.mid", $(2pi) / L_x$, anchor: "north", padding: 0.2) + + let y-start = (x-range, -ratio * y-range) + let y-end = (x-range, -ratio * y-range + ratio) + + line(y-start, y-end, ..spacing-arrow, name: "y-spacing") + content("y-spacing.mid", $(2pi) / L_y$, anchor: "west", padding: 0.2) + + // Add N(k) label + let angle = 130deg + let label-pos = (calc.cos(angle) * 2.4, calc.sin(angle) * 2.4) + content(label-pos, text(blue)[$N(k)$]) +}) diff --git a/assets/light/light.typ b/assets/light/light.typ index 2c2b699d..25a5cbf7 100644 --- a/assets/light/light.typ +++ b/assets/light/light.typ @@ -5,7 +5,7 @@ #set page(width: auto, height: auto, margin: .5cm) #canvas({ - import draw: * + import draw: set-transform, scale, content, line, grid, group, rotate // Set up the transformation matrix set-transform(matrix.transform-rotate-dir((1, 1, -1.3), (0, 1, .4))) diff --git a/site/src/lib/Card.svelte b/site/src/lib/Card.svelte index 7d70394c..3f64fced 100644 --- a/site/src/lib/Card.svelte +++ b/site/src/lib/Card.svelte @@ -12,7 +12,7 @@

{title}

- + {#if description && format === `full`}

{@html description}

@@ -46,6 +46,7 @@ height: auto; } p.description { + color: var(--text-color); padding: 1ex 1em; position: absolute; bottom: 0; diff --git a/site/src/lib/Tags.svelte b/site/src/lib/Tags.svelte index bdbd4126..9835eb86 100644 --- a/site/src/lib/Tags.svelte +++ b/site/src/lib/Tags.svelte @@ -1,10 +1,11 @@ -

+

{#each tags ?? [] as tag} {tag} {/each} diff --git a/site/src/lib/index.ts b/site/src/lib/index.ts index 858164f4..1c4a7ff3 100644 --- a/site/src/lib/index.ts +++ b/site/src/lib/index.ts @@ -1,3 +1,10 @@ +import rehype_katex from 'rehype-katex' +import rehype_stringify from 'rehype-stringify' +import remark_math from 'remark-math' +import remark_parse from 'remark-parse' +import remark_rehype from 'remark-rehype' +import { unified } from 'unified' + export { default as Card } from './Card.svelte' export { default as CodeBlock } from './CodeBlock.svelte' export { default as Tags } from './Tags.svelte' @@ -21,3 +28,72 @@ export type YamlMetadata = { url?: string date?: string } + +// Load all YAML files from assets directory +const yaml_data = import.meta.glob(`$assets/**/*.yml`, { + eager: true, + import: `default`, +}) as Record +const code_files = import.meta.glob([`$assets/**/*.tex`, `$assets/**/*.typ`], { + eager: true, + query: `?raw`, + import: `default`, +}) +const asset_files = import.meta.glob( + [`$assets/**/*.png`, `$assets/**/*.pdf`, `$assets/**/*.svg`], + { eager: true, query: `?url`, import: `default` }, +) +const image_files = import.meta.glob(`$assets/**/*.png`, { + eager: true, + query: { enhanced: true }, + import: `default`, +}) + +// Process YAML files to create figure data +export const diagrams = Object.entries(yaml_data).map(([path, metadata]) => { + const slug = path.split(`/`)[2] // get directory name + const figure_basename = `../assets/${slug}/${slug}` + + // Check if .tex or .typ file exists and get its content + const tex_path = `${figure_basename}.tex` + const typ_path = `${figure_basename}.typ` + const code = code_files[tex_path] ?? code_files[typ_path] ?? `` + + let { tags = [] } = metadata + // Add typst and cetz tags if applicable + if (typ_path in code_files) { + tags = [...new Set([...tags, `cetz`])] + } + if (tex_path in code_files) { + tags = [...new Set([...tags, `tikz`])] + } + + // Create new metadata object with updated tags + metadata.tags = tags + + // Process description with remark/rehype if needed + if (metadata.description) { + metadata.description = unified() + .use(remark_parse) + .use(remark_math) + .use(remark_rehype) + .use(rehype_katex) + .use(rehype_stringify) + .processSync(metadata.description) + .toString() + } + + const downloads = [ + asset_files[`${figure_basename}-hd.png`], + asset_files[`${figure_basename}.png`], + asset_files[`${figure_basename}.pdf`], + asset_files[`${figure_basename}.svg`], + ].filter(Boolean) + + const images = { + hd: image_files[`${figure_basename}-hd.png`], + sd: image_files[`${figure_basename}.png`], + } + + return { ...metadata, slug, code, downloads, images } +}) diff --git a/site/src/lib/stores.ts b/site/src/lib/stores.ts index 755e0519..5d265c53 100644 --- a/site/src/lib/stores.ts +++ b/site/src/lib/stores.ts @@ -1,12 +1,6 @@ -import rehype_katex from 'rehype-katex' -import rehype_stringify from 'rehype-stringify' -import remark_math from 'remark-math' -import remark_parse from 'remark-parse' -import remark_rehype from 'remark-rehype' +import { diagrams } from '$lib' import { session_store, url_param_store } from 'svelte-zoo/stores' import { writable } from 'svelte/store' -import { unified } from 'unified' -import type { YamlMetadata } from '.' export const search = url_param_store(`search`, ``) @@ -20,68 +14,4 @@ export const filter_tags = session_store<{ label: string; count: number }[]>( [], ) -// Load all YAML files from assets directory -const yaml_data = import.meta.glob(`$assets/**/*.yml`, { - eager: true, - import: `default`, -}) as Record -const code_files = import.meta.glob([`$assets/**/*.tex`, `$assets/**/*.typ`], { - eager: true, - query: `?raw`, - import: `default`, -}) -const asset_files = import.meta.glob( - [`$assets/**/*.png`, `$assets/**/*.pdf`, `$assets/**/*.svg`], - { eager: true, query: `?url`, import: `default` }, -) -const image_files = import.meta.glob(`$assets/**/*.png`, { - eager: true, - query: { enhanced: true }, - import: `default`, -}) - -// Process YAML files to create figure data -export const diagrams = Object.entries(yaml_data).map(([path, metadata]) => { - const slug = path.split(`/`)[2] // get directory name - const figure_basename = `../assets/${slug}/${slug}` - - // Check if .tex or .typ file exists and get its content - const tex_path = `${figure_basename}.tex` - const typ_path = `${figure_basename}.typ` - const code = code_files[tex_path] ?? code_files[typ_path] ?? `` - - // Add typst and cetz tags if applicable - if (!metadata.tags) metadata.tags = [] - - if (code_files[typ_path]) { - if (!metadata.tags.includes(`typst`)) metadata.tags.push(`typst`) - } - - // Process description with remark/rehype if needed - if (metadata.description) { - metadata.description = unified() - .use(remark_parse) - .use(remark_math) - .use(remark_rehype) - .use(rehype_katex) - .use(rehype_stringify) - .processSync(metadata.description) - .toString() - } - - const downloads = [ - asset_files[`${figure_basename}-hd.png`], - asset_files[`${figure_basename}.png`], - asset_files[`${figure_basename}.pdf`], - asset_files[`${figure_basename}.svg`], - ].filter(Boolean) - - const images = { - hd: image_files[`${figure_basename}-hd.png`], - sd: image_files[`${figure_basename}.png`], - } - - return { ...metadata, slug, code, downloads, images } -}) - export const filtered_diagrams = writable(diagrams) diff --git a/site/src/routes/+layout.svelte b/site/src/routes/+layout.svelte index bf491c49..469bf4d2 100644 --- a/site/src/routes/+layout.svelte +++ b/site/src/routes/+layout.svelte @@ -1,6 +1,6 @@