Skip to content

Repository files navigation

LaTeX Diagram Generator Logo

🔬 AI-Powered LaTeX Diagram Generator

An IBM watsonx.ai-powered multi-agent framework designed to autonomously design, coordinate, generate, validate, and self-correct LaTeX TikZ diagrams from natural-language descriptions.


🎯 Executive Overview

The LaTeX Diagram Generator is an enterprise-grade agentic workflow solution developed in partnership with the IBM University Engagement initiative. It solves a primary bottleneck in academic publishing and software documentation: the high complexity, syntactic verbosity, and compilation fragility of creating technical diagrams in LaTeX using the native TikZ package.

By utilizing the advanced reasoning capabilities of the IBM watsonx.ai Granite-8b-code-instruct model and an orchestration framework, this system provides a pipeline that guarantees valid, compilable, and overlapping-free vector diagrams.


🏗️ Core Architecture & Agentic Workflow

Instead of relying on a single, long-context LLM call—which typically suffers from layout hallucinations and syntactic decay—the project implements a 5-Agent Pipeline where each node has a strict, single responsibility.

                  User Description (Natural Language)
                                  │
                                  ▼
      ┌────────────────────────────────────────────────────────┐
      │  Agent 1: Parser Agent (Intent Normalization)          │
      │  - Maps description to structured schema (nodes/edges) │
      └──────────────────────────┬─────────────────────────────┘
                                 │ Structured Intent JSON
                                 ▼
      ┌────────────────────────────────────────────────────────┐
      │  Agent 2: Structure Agent (Geometry & Layout Engine)   │
      │  - Computes coordinates and sets edge routing styles   │
      └──────────────────────────┬─────────────────────────────┘
                                 │ Structured Geometry JSON
                                 ▼
      ┌────────────────────────────────────────────────────────┐
      │  Agent 3: Code-Gen Agent (TikZ Syntax Writer)          │
      │  - Translates layout schema to standalone LaTeX/TikZ   │
      └──────────────────────────┬─────────────────────────────┘
                                 │ Standalone TikZ Document
                                 ▼
                                 ├─────────────────────────────┐
                                 │                             │
                                 ▼                             ▼
      ┌────────────────────────────────────┐       ┌──────────────────────┐
      │  Agent 4: Compile-Validate Agent   │ ◄───  │ Agent 5: Refinement  │
      │  - Verifies code via LaTeX Wasm    │  (If  │ Agent                │
      │    compiler & static AST validation│ Fail) │ - Patches specific   │
      └──────────────────┬─────────────────┘       │   reported syntax    │
                         │                         │   errors             │
                         │ Pass                    └──────────────────────┘
                         ▼
             Final Valid compilable output

1. Agent 1: Parser Agent (Intent Normalization)

  • Role: Natural Language Processing (NLP) Parser.
  • Input: User's raw text description.
  • Output: Structured JSON defining node arrays (labels, shapes) and edge connection lists.
  • Safety Guard: Automatically normalizes user shapes to a supported geometric set (rectangle, diamond, circle, ellipse) through localized post-processing.

2. Agent 2: Structure Agent (Geometry & Layout Engine)

  • Role: Spatial Coordinator.
  • Input: Normalized intent JSON.
  • Output: Multi-dimensional layout schema.
  • Tasks: Computes optimal node coordinates (in cm/TikZ units) with a minimum 2.5cm separation limit to prevent overlaps, resolves layouts (horizontal, vertical, radial, or grid), and determines edge routing properties (straight, curved, or orthogonal).

3. Agent 3: Code-Gen Agent (TikZ Syntax Writer)

  • Role: LaTeX Code Generator.
  • Input: Layout schema with coordinates and routing.
  • Output: Compilable standalone LaTeX document.
  • Preamble Injection: Auto-configures required TikZ libraries (arrows.meta, positioning, shapes.geometric, calc) and maps node geometric metadata to correct TikZ properties.

4. Agent 4: Compile-Validate Agent (Compiler Loop)

  • Role: Quality Gatekeeper.
  • Action: Runs double-validation.
    • Primary: Compiles TikZ code in a sandboxed, client-side WebAssembly environment.
    • Secondary/Fallback: Performs Abstract Syntax Tree (AST) checks (brace balancing, environment closure matching, undefined node reference validation).
  • Output: Syntactic validation state and array of specific compiler/parser errors.

5. Agent 5: Refinement Agent (Self-Correction)

  • Role: Auto-Debugger.
  • Trigger: Instantiated automatically only if validation fails.
  • Logic: Receives the broken TikZ code and the specific compiler error logs. Uses Granite to patch only the erroneous segments while strictly preserving the verified coordinates and layout structure, executing up to two passes.

⚡ Key Features & Capabilities

  • Dynamic Pipeline Trace: Frontend displays step-by-step progress bars and status tags as each backend agent completes its task, exposing the internal reasoning trace of the pipeline.
  • Embedded WebAssembly Rendering: Integrates TikzJax directly inside the Streamlit user interface, compiling the diagram in the browser to display a live, high-fidelity interactive SVG without relying on third-party websites or local LaTeX toolchains.
  • Self-Correction: Resolves brace imbalances, missing package dependencies, and broken routing references programmatically.
  • Brutalist Visual Redesign: Premium Neo-Brutalism user interface featuring flat, high-contrast colors, thick borders, hard offset shadows, and clean monospace typography.
  • Multi-Format Export: Generates download links for standalone .tex source files.

🔬 Supported Diagram Scopes

The pipeline is pre-configured and tested for:

  1. Neural Networks: Fully connected layers, deep feedforward topologies, input/hidden/output labeling, and weight connection arrows.
  2. Flowcharts: Binary search trees, decision conditions (diamonds), process blocks (rectangles), and directional flow arrows.
  3. State Machines: Complex transitions (Idle ➔ Running ➔ Paused ➔ Stopped), self-loops, and labeled state nodes.
  4. Directed & Undirected Graphs: Nodes placed in radial, grid, or custom shapes with connected nodes.
  5. Tree Architectures: Recursive node trees (e.g. binary search trees with children and grandchildren).

🏢 IBM Enterprise Technology Stack

  • IBM watsonx.ai: Enterprise studio providing secure inference API access.
  • Granite-8b-code-instruct: Core Large Language Model optimized for code generation, JSON compliance, and syntactic debugging.
  • ibm-watsonx-ai SDK: Official IBM Python client library for API authorization and text generation.
  • IBM Cloud Resources: Deployment environments and Watson Machine Learning (WML) service instances.

📄 License & Collaboration

Developed under the IBM University Engagement Project portfolio. Distributed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages