ClaudeMods
☰
JA
● 0 人がオンライン ・閲覧 0 回
スポンサー作品を投稿
GitHub リポジトリ · 投稿者 natsukium

figures

kitty-graphics 対応ターミナルで図、LaTeX 数式、ツール結果画像をインライン描画

natsukium@natsukium

natsukium/claude-code-figures-plugin

元の投稿の画像1
翻訳済み

この mod について

figures

kitty graphics プロトコルを使い、ターミナルのトランスクリプト内に図、LaTeX 数式、ツール結果画像をインライン描画する Claude Code mod です。

mermaid フローチャートと LaTeX 式を、それぞれ元の内容の下に画像として描画した返信

描画されるもの

  • 図。 Claude の返信にある mermaid、dot(または graphviz)、d2、svg タグ付きコードブロックをレンダリングし、返信の下に描画します。コードは画像の上に表示されたままです。
  • 数式。 LaTeX のディスプレイ数式、$$ … $$、\[ … \]、または math、latex、tex コードブロックは MathJax で組版します。インラインの $…$ はテキストのままです。画像を行の途中に置くことはできないためです。完全な文書(\documentclass)を含む latex ブロックもコードのまま残ります。
  • ツール結果画像。 PNG、JPEG、GIF、WebP、SVG ファイルを Read したとき、または MCP ツールが返したスクリーンショットを、そのツール行の下に描画します。Claude Code は通常ここにサイズだけを表示します。部分的にしか読まれていない SVG はそのままにします。折りたたまれたツールグループではサムネイルになり、ctrl+o で展開すると実寸で描画されます。Claude が SendUserFile で別のデバイスに送る PNG、JPEG、GIF、WebP、SVG も添付行の下に描画されるため、ターミナルに戻ったとき画面に残ります。行を初めて描画するときにディスクから読み込みます。後からファイルを上書きしても、表示済みの画像は変わりません。

サイズ上限により自然なサイズの 60% 未満に縮小される画像は、ターミナルの高さまで拡大します。それでも小さい場合は、shown at 43% · /figures to enlarge のように画像の下に表示します。

/figures はセッションの画像を表示するペインを開きます。最初は、直近に描画された読みにくいサイズの画像を表示し、なければ最新の画像を表示します。p と n で画像を移動し、o は open(macOS)または xdg-open で現在の画像を開きます。Escape または次のプロンプトでペインを閉じます。

トランスクリプトの横にある /figures ペイン。返信の数式を表示している

このプラグインには diagrams skill も含まれています。どの言語が描画されるか、レンダリングできる形でどう書くかを Claude に伝えるものです。

レンダリングされた各 PNG は $TMPDIR/claude-figures/ にキャッシュされます。ソースとテーマのハッシュを名前に使うため、再開したセッションでも再レンダリングせずに画像を描画できます。セッション開始時に七日より古いファイルを削除します。

要件

Claude Code 2.1.287 以降で、mods(function-hook plugins)を有効にし、kitty graphics 対応として扱われるターミナルで実行してください。Terminal support を参照してください。画像の種類ごとに PATH 上のプログラムも必要です。

| 画像 | 必要なもの | | -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | mermaid | mmdr または mmdc。resvg 推奨 | | dot、graphviz | Graphviz の dot と resvg | | d2 | d2 と resvg | | svg、数式 | resvg | | PNG 以外のツール画像 | magick(ImageMagick)または sips(macOS 組み込み) |

  • mmdc がヘッドレスブラウザーを起動するのに対し、mmdr は数ミリ秒で描画できるため、先に mmdr を試します。mmdc は mermaid.js 自体を実行するので、mmdr で処理できないブロックは、インストールされていれば mmdc に渡します。
  • resvg がない場合、mmdr は PNG を 1x で書き出すため、高密度ディスプレイではぼやけて見えます。resvg があれば、図を SVG にレンダリングして scale オプションの倍率でラスタライズします。
  • MathJax 4 と New Computer Modern フォントのすべての字形範囲は同梱され、Claude Code 内で実行されるため、数式に Node.js は不要です。
  • レンダラーが 1 つもインストールされていないブロックは、テキストとして読めるため、エラーを出さずコードのまま残します。エラーが表示されるのは、インストール済みのレンダラーが失敗した場合だけです。

インストール

claude plugin marketplace add natsukium/claude-code-figures-plugin
claude plugin install figures@figures

cargo install mermaid-rs-renderer で mmdr をインストールします。

オプション

/config または claude plugin configure figures で設定します。

| オプション | デフォルト | 意味 | | --------------------- | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | theme | auto | auto は Claude Code のテーマに従います。または dark、default、forest、neutral、modern を指定できます(mmdr のみ。mmdc は default として描画します)。dark では数式を明るく、それ以外では暗く描画します | | background | transparent | 図の背景。transparent ならターミナルの背景が透けます | | scale | 2 | 画像のピクセル密度。1 より大きくするには resvg が必要です | | cell_width_px | 8 | ターミナルの 1 セルの幅として仮定する値。図のレイアウトに使うピクセル単位 | | cell_aspect | 2.1 | ターミナルの 1 セルの高さと幅の比率として仮定する値 | | max_columns | 120 | 画像の最大幅。単位はセルで、ターミナル内にも収めます | | max_rows | 30 | 読みやすさのために拡大する前の画像の最大高さ。単位はセルです | | renderers | (空) | 優先順に並べたレンダラー id。カンマ区切りで指定します。下記参照 | | tool_images | true | ツール結果から見つかった画像を描画するか | | tool_image_max_rows | 12 | ツール結果のサムネイルに許す最大の高さ。単位はセルです |

renderers は各言語を描画するプログラムを選び、順序付けます。id は mmdr と mmdc(mermaid)、dot、d2、svg、mathjax です。ある言語にレンダラーが 1 つでも列挙されていれば、それらだけを列挙順に使います。それ以外はデフォルトのままです。つまり mmdc なら mermaid を mmdc だけで描画し、mmdc,mmdr なら mmdc を先に試します。レンダラーが存在しない id はセッション開始時に報告されます。

ターミナル対応

Claude Code は、ターミナルの XTVERSION 応答が kitty(0.28.0 以降)または Ghostty を名指しした場合だけ画像を有効にします。他の名前で kitty graphics クエリに正しく応答するターミナルでも、[mermaid diagram 1] のような alt テキストは表示されます。TERM や TERM_PROGRAM を設定しても効果はありません。

そのようなターミナルでこのプラグインを使うには、Claude Code が読むオーバーライドを設定します。

CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1 claude

tmux のチェックもスキップされます。tmux 内では画像に加えて set -g allow-passthrough on が必要です。

Claude Code の判断を確認するには、--debug で起動し、デバッグログの Terminal capabilities: 行を探します。

制限事項

  • mods にはターミナルのセルのピクセル寸法が渡されないため、画像のセル単位のサイズは cell_width_px と cell_aspect から推定します。画像が引き伸ばされて見える場合は調整してください。
  • mods はターミナルの背景色を読めないため、theme=auto は Claude Code 自身のテーマ(/config)に従います。light バリアントは明るい画像、それ以外は暗い画像を描画します。Claude Code のテーマ自体が auto の場合、解決済みの値は公開されないため、ターミナルが値を出力する場所では COLORFGBG が判断し、それ以外では暗いテーマになります。
  • mmdr は Mermaid を独自に解析するため、すべてのケースで mermaid.js と一致するわけではありません。たとえばラベル付きの連結エッジ(A --> B -->|ok| C)は誤解析されます。1 行に 1 本のエッジを書いてください。このようなブロックはエラーなしで描画されるため mmdc に渡されません。mmdr を避けるには renderers を mmdc に設定します。
  • 数式フォントにないスクリプトの文字、たとえば \text{日本語} は、resvg がシステムフォントで描画します。
  • /figures ペインはプロンプトと画面を共有するため、短いターミナルでは数行しかありません。o でターミナルの外に画像を実寸で開けます。

開発

tsconfig.json は .claude-plugin/types/ にあるエンジンの型定義を拡張し、Vite、Vitest、tsc が読み取ります。Claude Code はこのフォルダーからプラグインを読み込んだときだけ型定義を書き込み、他のコマンドが単独で書き込むことはありません。そのため pnpm types はダミー API key で claude -p --plugin-dir . を起動し、型定義が存在すれば失敗したリクエストを無視します。build、test、typecheck は最初にこれを実行します。Claude Code は開発依存関係なので、スクリプトと pnpm exec claude は PATH 上の claude ではなく、pnpm-lock.yaml に固定されたバージョンを使います。

pnpm install
pnpm build              # regenerate hooks/vendor/
pnpm test               # unit tests (Vitest, tests/unit/*.spec.ts)
pnpm typecheck
pnpm exec claude plugin validate .
pnpm exec claude plugin test .    # hooks through the engine (tests/engine/*.test.ts)

Claude Code は hooks/ を TypeScript ソースとして読み込むため、ビルドされるのはサードパーティーコードだけです。pnpm build は Vite で MathJax と @noble/hashes を hooks/vendor/ にバンドルし、リポジトリをそのままインストールできるようコミットします。Hooks モジュールは 1 MiB を超えるファイルをインポートできず、実行時にモジュールをインポートすることもできません。そのためビルドではバンドルをチャンクに分割し、フォントの各字形範囲を JSON として書き出します。プラグインは数式でその範囲が初めて使われたときに読み込みます。

エンジンはインポート間で $ を渡すモジュールを拒否します。そのため hooks/register.tsx はプラグインが $ から必要とするものを Io オブジェクト(hooks/io.ts)に包み、他のすべてのモジュールがそれを受け取ります。

ライセンス

Apache-2.0。hooks/vendor/ は MathJax とその New Computer Modern font(いずれも Apache-2.0)、および noble-hashes(MIT)から構築されています。

インストール

まず作者の README で marketplace とプラグイン名を確認してください。コマンドはリポジトリの構成によって変わる場合があります。

claude plugin marketplace add natsukium/claude-code-figures-plugin
claude plugin install figures
原文 / README

figures

A Claude Code mod that draws diagrams, LaTeX math, and tool-result images inline in the terminal transcript, using the kitty graphics protocol.

A reply with a mermaid flowchart and a LaTeX formula, each drawn as a picture under its source

What it draws

  • Diagrams. A code block tagged mermaid, dot (or graphviz), d2, or svg in Claude's reply is rendered and drawn under the reply. The code stays visible above the picture.
  • Math. LaTeX display math, $$ … $$, \[ … \], or a math, latex, or tex code block, is typeset with MathJax. Inline $…$ stays text, since a picture cannot sit inside a line, and a latex block holding a whole document (\documentclass) stays code.
  • Tool-result images. Read on a PNG, JPEG, GIF, WebP, or SVG file, or a screenshot an MCP tool returns, is drawn under the tool's row, where Claude Code otherwise shows only a size line; an SVG read only in part is left alone. In a collapsed tool group they are thumbnails; ctrl+o unfolds the group and draws them full size. A PNG, JPEG, GIF, WebP, or SVG Claude sends to another device with SendUserFile is drawn under its attachment line too, so it is on screen when you come back to the terminal. It is read from disk when the row is first drawn; overwriting the file later leaves that picture as it was.

A picture the size caps would shrink below 60% of its natural size grows up to the terminal's height, and one still smaller says so under it, such as shown at 43% · /figures to enlarge.

/figures opens a pane with the session's pictures, starting on the newest one drawn too small to read, or else the newest. p and n step through them, o opens the current one with open (macOS) or xdg-open, and Escape or your next prompt closes the pane.

The /figures pane beside the transcript, showing the formula from the reply

The plugin also ships a diagrams skill, which tells Claude which languages are drawn and how to write them so they render.

Each rendered PNG is cached under $TMPDIR/claude-figures/, named by a hash of the source and the theme, so a resumed session draws its pictures again without re-rendering. Files older than seven days are removed when a session starts.

Requirements

Claude Code 2.1.287 or later, with mods (function-hook plugins) enabled, in a terminal it treats as kitty-graphics capable; see Terminal support. Each kind of picture also needs programs on PATH:

| Picture | Needs | | -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | mermaid | mmdr or mmdc; resvg recommended | | dot, graphviz | Graphviz's dot and resvg | | d2 | d2 and resvg | | svg, math | resvg | | Tool images other than PNG | magick (ImageMagick) or sips (built into macOS) |

  • mmdr is tried first, since it renders in milliseconds where mmdc starts a headless browser. mmdc runs mermaid.js itself, so a block mmdr fails on is handed to it when it is installed.
  • Without resvg, mmdr writes its PNG at 1x, which looks soft on a high-density display; with it, the diagram is rendered to SVG and rasterized at the scale option.
  • MathJax 4 and every glyph range of its New Computer Modern font are bundled, and run inside Claude Code, so math needs no Node.js.
  • A block none of whose renderers is installed is left as code without an error, since it still reads as text. An error is shown only when an installed renderer fails.

Install

claude plugin marketplace add natsukium/claude-code-figures-plugin
claude plugin install figures@figures

Install mmdr with cargo install mermaid-rs-renderer.

Options

Set these with /config or claude plugin configure figures.

| Option | Default | Meaning | | --------------------- | ------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | theme | auto | auto follows Claude Code's theme; or dark, default, forest, neutral, modern (mmdr only; mmdc draws it as default). Math is drawn light on dark and dark otherwise | | background | transparent | Diagram background; transparent lets the terminal's background show through | | scale | 2 | Pixel density of the picture; above 1 needs resvg | | cell_width_px | 8 | Assumed width of one terminal cell, in the pixels diagrams are laid out in | | cell_aspect | 2.1 | Assumed height-to-width ratio of one terminal cell | | max_columns | 120 | Widest a picture may be, in cells; it is also kept inside the terminal | | max_rows | 30 | Tallest a picture may be before it is grown for legibility, in cells | | renderers | (empty) | Renderer ids in preference order, comma-separated; see below | | tool_images | true | Draw images found in tool results | | tool_image_max_rows | 12 | Tallest a tool-result thumbnail may be, in cells |

renderers picks and orders the programs that draw each language. The ids are mmdr and mmdc (mermaid), dot, d2, svg, and mathjax. A language with any of its renderers listed uses only those, in the listed order; the others keep the default. So mmdc draws mermaid with mmdc alone, and mmdc,mmdr tries mmdc first. An id no renderer has is reported when a session starts.

Terminal support

Claude Code enables pictures only when the terminal's XTVERSION reply names kitty (0.28.0 or later) or Ghostty. A terminal that answers the kitty graphics query correctly under any other name still gets the alt text, such as [mermaid diagram 1]. Setting TERM or TERM_PROGRAM has no effect.

To use the plugin on such a terminal, set the override Claude Code reads:

CLAUDE_CODE_FORCE_TERMINAL_IMAGES=1 claude

This also skips Claude Code's tmux check. Inside tmux, the pictures additionally need set -g allow-passthrough on.

To see what Claude Code decided, start it with --debug and look for the Terminal capabilities: line in the debug log.

Limitations

  • The picture's size in cells is estimated from cell_width_px and cell_aspect, since mods are not told the terminal's cell size in pixels. Adjust them if pictures look stretched.
  • Mods cannot read the terminal's background color, so theme=auto follows Claude Code's own theme (/config): a light variant draws light pictures, the others dark. When Claude Code's theme is itself auto, its resolved value is not exposed, so COLORFGBG decides where the terminal exports it, and dark otherwise.
  • mmdr parses Mermaid on its own and does not match mermaid.js in every case. For example, a chained edge with labels (A --> B -->|ok| C) is misparsed; write one edge per line. Such a block renders without an error, so it is not handed to mmdc; set renderers to mmdc to avoid mmdr.
  • Text in a script the math font lacks, such as \text{日本語}, is drawn by resvg with a system font.
  • The /figures pane shares the screen with the prompt, so in a short terminal it has only a few rows; o opens the picture at full size outside the terminal.

Development

tsconfig.json extends the engine's typings in .claude-plugin/types/, which Vite and Vitest read as well as tsc. Claude Code writes them only when it loads the plugin from this folder, and no command writes them alone, so pnpm types starts claude -p --plugin-dir . with a dummy API key and ignores the failed request once the typings exist. build, test, and typecheck run it first. Claude Code is a dev dependency, so the scripts and pnpm exec claude use the version pinned in pnpm-lock.yaml, whatever claude is on PATH.

pnpm install
pnpm build              # regenerate hooks/vendor/
pnpm test               # unit tests (Vitest, tests/unit/*.spec.ts)
pnpm typecheck
pnpm exec claude plugin validate .
pnpm exec claude plugin test .    # hooks through the engine (tests/engine/*.test.ts)

Claude Code loads hooks/ as TypeScript source, so only third-party code is built. pnpm build bundles MathJax and @noble/hashes with Vite into hooks/vendor/, which is committed so the repository stays installable as is. A hooks module imports no file over 1 MiB and cannot import modules at run time, so the build splits the bundle into chunks and writes each of the font's glyph ranges as JSON, which the plugin reads the first time a formula uses it.

The engine refuses a module that passes $ across an import, so hooks/register.tsx wraps what the plugin needs from $ in an Io object (hooks/io.ts), and every other module takes that.

License

Apache-2.0. hooks/vendor/ is built from MathJax and its New Computer Modern font, both Apache-2.0, and @noble/hashes, MIT.

関連作品