DSH HUB
HomePlugin StorePlugin PacksCommunityRankingsResourcesPublish Guide
Plugin source
Back to catalog

VictorKozachek /

VictorKozachek/dsh-math

Verified

LaTeX in DeepSeek Harness chat: formulas in user messages, physics macro pack, mhchem, tidy parse errors (KaTeX)

★ 0 Stars0 Forks0 IssuesN/A Community rating0 Confirmed installs
View on GitHub
READMESource: main@26d2632a

dsh-math

tests npm

A plugin for DeepSeek Harness (DSH) that brings LaTeX to the chat:

  • renders formulas in your own messages — $...$, $$...$$, \(...\), \[...\];
  • adds a physics macro pack (\abs, \ket, \dv, \Tr, …) and mhchem (\ce{});
  • turns a formula the built-in renderer could not parse into a tidy badge instead of a wall of red source.

English | Русский

Showcase

The Hamiltonian from the dissertation, rendered by the plugin's own engine (KaTeX 0.18.7 + the macro pack + mhchem) — note \ket, \braket and \ce below, which plain KaTeX would reject:

Hamiltonian rendered by dsh-math

The interactive page is examples/hamiltonian.html, generated by node tools/render-example.mjs.

Compatibility

  • DeepSeek Harness Desktop 0.2.1 — written and tested against it; requires a profile that mounts the ui-chat client package (desktop or web);
  • Node.js 20+ for development and the test suite;
  • no runtime dependencies: KaTeX 0.18.7 and mhchem are vendored into lib/client.js (0.18.7 is the version the shell itself bundles, so the CSS matches exactly).

Why

Two different problems, one cause — the chat shows formulas in two ways:

  1. User messages are plain text. DSH renders them with the UserText primitive (white-space: pre-wrap), not markdown. So $E=mc^2$ typed by you stays literal text with dollar signs visible.
  2. Assistant replies are markdown with KaTeX, but bare KaTeX: \abs{x}, \ket{\psi}, \ce{H2O} are unknown commands. The renderer retries with strict: "ignore" and, failing that, shows the raw source in a red .katex-error span.

The plugin fixes both: user messages get a matcher + renderer, assistant replies get a second chance with a wider macro pack.

What is recognized (user messages)

Delimiter Meaning Notes
$tex$ inline no space right after the opening $; no newline inside; closing $ must not be followed by a digit ($5 stays money)
$$tex$$ display may span several lines
\(tex\) inline works here because user text has no markdown escaping
\[tex\] display same
  • an escaped \$ never opens math;
  • an unclosed delimiter is not math: leftover text never turns into a formula;
  • a formula that KaTeX cannot parse stays plain text and the search continues after it — no surprises, no broken markup.

Macro pack

Available in user messages and in the second chance for assistant replies:

Macro Renders as Macro Renders as
\abs{x} |x| \Tr A Tr A
\norm{x} ‖x‖ \tr A tr A
\ket{\psi} |ψ⟩ \dv{f}{x} df/dx
\bra{\psi} ⟨ψ| \pdv{f}{x} ∂f/∂x
\braket{\phi}{\psi} ⟨φ|ψ⟩ \dv[2]{f}{x} d²f/dx²
\R, \C, \N, \Z, \Q ℝ, ℂ, ℕ, ℤ, ℚ \half ½
\eqdef ≝ \ip{a}{b} ⟨a|b⟩

Also fixed automatically: the common typo \left{ → \left\{ and physics-style derivative orders (\dv[2]{f}{x} → \dfrac{d^2 f}{dx^2}).

Chemistry

mhchem is included: \ce{H2O + CO2 -> H2CO3}, \pu{1.5 mol/L}.

Calculator in your messages

A line that starts with = is evaluated right in the bubble; the result is typeset with KaTeX. Nothing is sent to any service: it is local JavaScript. A line that starts with a known calculator function is evaluated too, even without =: linfit([1,2,3], [2.1,3.9,6.1]), det([[1,2],[3,4]]), diff(x^3, x), solve(x^2 - 4x + 3 = 0). Prose stays prose.

You type You get
= 2 + 2 4
= 2pi 6.283185307
= sin(pi/2) + cos(0) 2
= hbar 1.054572e-34 (SI, CODATA 2018)
= diff(x^3, x) 3x² (symbolic derivative)
= integrate(sin(x), x, 0, pi) 2 (numeric integral)
= solve(x^2 - 4x + 3, x) x = 1, 3
= min(sin(x), x, 0, 2pi) −1 at x = 4.712
  • functions: sin cos tan asin acos atan atan2 sinh cosh tanh exp ln log log10 log2 sqrt abs sign floor ceil round min max hypot;
  • constants (SI): pi tau e phi h hbar c kB NA R me mp eps0 mu0 G g;
  • operators: + - * / ^ % ! with implicit multiplication — 2pi, 2(3+4), 3sin(x), and right-associative powers 2^3^2 = 512;
  • scientific operations: symbolic diff(expr, x); numeric integrate(expr, x, a, b), solve(expr, x[, a, b]) (also written as solve(lhs = rhs, x)), min|max(expr, x, a, b);
  • a line that does not parse stays plain text — a typo never breaks the message, and ordinary equalities like x = 2 are not touched.

Natural commands

You type You get
= x^2 - 4x + 3 = 0 x = 1, 3 — the equation is solved automatically
#solve x^2 - 4x + 3 = 0 x = 1, 3
#diff x^3 3x²
#diff x^3, t derivative over t
#int sin(x), 0, pi 2
#eval 2^10 1024

In #solve the variable is detected from the equation; in #int it is detected from the integrand. #diff defaults to x, #eval is just the value.

Differential equations

You type You get
#ode y' = y, y(0) = 1, x = 0..2 a table y(x): 1 → 7.389 (e²)
#ode y' = -y, y(0) = 1, x = 0..5 decay down to 0.00674
#ode y'' = -y, y(0) = 1, y'(0) = 0, x = 0..6.28 harmonic oscillator, y(π) = −1
#ode y'' = -y - 0.2y', y(0) = 1, y'(0) = 0, x = 0..10 damped oscillations
#ode y' = y, y(0) = 1, x = 0..2, plot the same solution as a chart
#ode y'' = -y - 0.2y', y(0) = 1, y'(0) = 0, x = 0..10 closed form: y(x) = e⁻⁰·¹ˣ(cos 0.995x + 0.1005 sin 0.995x)
#ode y'' = -y + sin(3x), y(0) = 0, y'(0) = 0, x = 0..10 forced: y = 0.375 sin x − 0.125 sin 3x
#ode y'' = -y + sin(x), y(0) = 0, y'(0) = 0, x = 0..20 resonance: y = 0.5 sin x − 0.5x cos x

Syntax: the equation (y' or y''), initial conditions (y(x0) = …, and y'(x0) = … for the second order), and optionally the range x = a..b (defaults to x0..x0+1). Solved numerically by the classic 4th-order Runge–Kutta with 2000 steps; the result is a 5-point table. Wrong or incomplete input stays plain text.

Linear equations with constant coefficients are solved analytically. For y' = ay + b and y'' = ay + by' + c the plugin returns the explicit evolution — exponentials for real roots, sines/cosines for the oscillatory case (so the damped pendulum/oscillator comes out as e^{−γx}(C₁cos ωx + C₂sin ωx)). The solve-style numeric table is still available with the numeric flag, and The solution is always shown when it is found. Flags only add to it: numeric adds the numeric table, plot adds the chart, and numeric, plot gives both (formula on top, table under it, chart at the bottom). Nonlinear equations (e.g. y'' = -sin(y)) have no closed form, so they show the numeric table instead.

Forcing works for F·sin(ωx) and F·cos(ωx) added to a linear second-order equation: the steady-state particular solution is combined with the homogeneous one, and the resonant case (ω = ω₀, no damping) is recognised explicitly, so the amplitude grows linearly in time.

Units and dimensional analysis

You type You get
= 5 eV -> J 8.01088e-19 J
= 1.2 nm -> m 1.2e-9 m
= h*c/(500 nm) -> eV 2.47968 eV
= kB*300 K -> meV 25.852 meV
= 2.5 kg*9.81 m/s^2 -> N 24.525 N

SI units and their decimal multiples are known (m, nm, Å, s, kg, K, mol, Hz, J, eV, N, Pa, bar, W, A, V, T, L, rad, deg, …); physical constants carry their dimensions, so h*c/(500 nm) is an energy. to works as an alias of ->. A dimension mismatch (= 5 eV -> m) leaves the line as plain text.

Inline plots

You type You get
= plot(sin(x), x, 0, 6.2832) an SVG chart with min/max
= plot(1/x, x, -1, 1) the hyperbola, with the gap at 0 cut
#plot x^2, -1, 1 a parabola

The chart is generated locally as SVG (no canvas, no network) and drawn right in the bubble: axes, light grid, zero lines, min/max labels. Discontinuities split the curve instead of drawing a vertical line.

Matrices and linear algebra

You type You get
= det([[1,2],[3,4]]) −2
= inv([[1,2],[3,4]]) the inverse matrix
= trace([[1,2],[3,4]]) 5
= transpose([[1,2],[3,4]]) the transpose
= eig([[2,1],[1,2]]) λ = 1, 3
= linsolve([[2,1],[1,-1]], [5,1]) x = 2, y = 1

Matrices are written [[a, b], [c, d]], vectors [a, b]. det, inv, trace and transpose work for square matrices; eig supports 2×2. A singular matrix or complex eigenvalues leave the line as plain text instead of printing nonsense.

Composer preview

While you type, a strip above the input renders the formula you are writing ($...$, \(...\)) or the result of a = … calculation, before you send the message. With no math in the draft the strip does not exist at all.

Statistics

You type You get
= mean([1,2,3,4]) 2.5
= median([1,2,3,4]) 2.5
= std([1,2,3,4]) 1.118033989
= var([1,2,3,4]) 1.25
= sum([1,2,3,4]) 10
= linfit([1,2,3], [2.1,3.9,6.1]) y = 2x + 0.0333, r² = 0.9967
= corr([1,2,3], [2.1,3.9,6.1]) 0.9983

Data is written as a vector [1,2,3]; linfit returns slope, intercept and R².

Assistant replies

  • a formula shown as red raw source (.katex-error) is re-rendered with the plugin's KaTeX and the macro pack — if it succeeds, the red text is replaced by the formula and the click-to-copy works as usual;
  • if it still cannot be parsed, the raw source is replaced by a compact formula badge: the error is in the tooltip, the source expands on click;
  • click on any rendered formula copies its TeX to the clipboard.

DSH extension points

User messages ride the documented mechanisms (see the ui-chat and ui-conversation package docs, section “Sent user text”):

  • ctx.uiConversation.userText.register({ id, order, match(text, from) }) — a synchronous matcher returning a candidate { id, start, end, data };
  • the conversation.user.text.node slot — the node renderer keyed by the same id.

Assistant markdown has no documented node-level extension point, and DSH documentation forbids plugins to import client primitives as modules. So the second half of the plugin is a careful DOM post-processing pass: it touches only .katex-error and already rendered .katex markup, marks everything it changes with data-dsh-math-* attributes and never processes a node twice.

The host half is empty: lib/index.js exports apply() {}. It exists only so the row appears in the profile's cordis tree.

Installation

The plugin installs into a DSH profile (desktop or web). Tested on DSH Desktop 0.2.1.

From npm

dsh plugin --profile desktop add @vkozachek/dsh-math

The plugin manager installs the package and records it in the profile's package.json and dsh.profile.bundles; the plugin loads on the next DSH start.

From GitHub (available now, before the npm release)

dsh plugin --profile desktop add github:VictorKozachek/dsh-math

The plugin manager forwards add to pnpm, which understands github: specifiers: the repository is fetched, built files ship as committed, and the bundle row is recorded in the profile.

From source

git clone https://github.com/VictorKozachek/dsh-math.git
cd dsh-math
./install.sh          # default profile: ~/.dsh/profiles/desktop

The script backs up the profile manifest, adds the package to dependencies (link:) and to dsh.profile.bundles, and creates the node_modules symlink. Another profile is selected with a variable:

DSH_PROFILE_DIR="$HOME/.dsh/profiles/web" ./install.sh

With the CLI from a local checkout

dsh plugin --profile desktop add /path/to/dsh-math

Verification

  1. Send yourself a message with a formula: энергия $E=mc^2$, состояние \(\ket{\psi}\) — both should render as math.
  2. Ask the assistant for a formula with a physics macro: \abs{x}, \ket{\psi} or \ce{H2O} — it should render instead of showing red source.
  3. Ask for a broken formula — it should appear as a formula badge, not as a wall of TeX.

Development

KaTeX must be vendored: client plugins cannot require npm packages, so lib/client.js is a single built file.

npm install          # dev dependency: katex 0.18.7
npm run build        # src/pure.js + src/client.js + KaTeX + mhchem → lib/client.js
npm test             # node --test test/*.test.mjs
  • src/pure.js — matching and rendering logic, no DOM, no React;
  • src/client.js — the browser half: userText rule, slot renderer, DOM pass;
  • lib/client.js — built file, committed; CI rebuilds it and fails if the committed copy is stale;
  • tests: test/matcher.test.mjs (rules, macros, mhchem) and test/plugin.test.mjs (manifest, patch layer, and the built bundle executed in an emulated browser with a React stub).

CI runs on GitHub Actions, Node 20/22/24 on Ubuntu and macOS (.github/workflows/tests.yml).

Limitations

  • \(...\) and \[...\] work in user messages only: in assistant replies markdown eats the backslash before the plugin ever sees the text;
  • assistant replies get the macro pack only as a second chance for formulas the shell failed to parse; formulas the shell rendered fine are left as they are (but click-to-copy is added);
  • no equation numbering or references;
  • no \ce support in the shell itself: only formulas repaired by the plugin get it.

How it differs from dsh-math-render

dsh-math-render by huanglianqi also renders TeX in your own bubble (it uses the same documented seats) and adds a composer draft preview. dsh-math goes further where scientific writing actually needs it:

dsh-math-render dsh-math
formulas in your messages yes yes
composer draft preview yes no (on the roadmap)
physics macro pack (\abs, \ket, \dv, …) no yes
mhchem \ce{} / \pu{} no yes
repairs broken formulas in assistant replies no (by design) yes
tidy formula badge instead of raw source no yes
calculator = expr (symbolic + numeric) no yes
click-to-copy TeX — yes

Do not install both: each registers a rule for the same conversation.user.text.node seat, and only one of them wins per formula span.

Roadmap

Next steps for the "math desk in chat" idea:

  • statistics over a pasted table (mean, std, linear fit);
  • composer draft preview (parity with dsh-math-render);
  • export a formula as SVG/PNG for slides and papers.

Uninstall

./uninstall.sh

Or manually: drop dsh-math from dsh.profile.bundles, remove the symlink from the profile's node_modules, and restart the application.

License

MIT — see LICENSE. KaTeX © Khan Academy (MIT) and mhchem (MIT) are vendored into the built file.

—/ 5

No ratings yet

Verified DSH bundle

Commit 26d2632a2b28

Community comments

No comments yet. Be the first to write one.

DSH HUB

A community index for DSH plugins. Not an official GitHub or DeepSeek AI product.

CommunityResourcesAPIAbout