Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Text Editing & Markdown

Dear ImGui Bundle includes libraries for syntax-highlighted text editing and markdown rendering.

rich_md - Markdown Rendering

Introduction

imgui_rich_md renders markdown content directly in your ImGui interface: headers, emphasis, links, lists, tables, images, code blocks with syntax highlighting, LaTeX math, admonitions and collapsible sections. Its text can be selected with the mouse and copied (Ctrl+C, Cmd+C on macOS); a right click also copies it as markdown. It is a standalone library that also works on stock Dear ImGui (C++); Dear ImGui Bundle includes it, with Python bindings.

In Python the module is imgui_bundle.rich_md; in C++ the namespace is RichMd. The former names, imgui_md (Python) and ImGuiMd (C++), remain available as aliases.

Quick example:

Python
C++
from imgui_bundle import rich_md, immapp

def gui():
    rich_md.render("""
# Hello Markdown

This is **bold** and this is *italic*.

- List item 1
- List item 2
    """)

immapp.run(gui, with_markdown=True)

rich_md.render(s) removes the common indentation of the string first, so that a string written inside an indented function renders as expected. rich_md.render_raw(s) renders it as is.

Images

rich_md supports images from local assets and from URLs.

Standard markdown images:

![local image](images/world.png)
![online image](https://example.com/photo.jpg)

HTML img tags with explicit size:

<img src="https://example.com/photo.jpg" width="200">
<img src="images/logo.png" width="100" height="50">

LaTeX math

rich_md renders inline and display LaTeX math with MicroTeX (the bundle uses a fork).

Quick example:

Python
C++
from imgui_bundle import rich_md, immapp

def gui():
    rich_md.render(r"""
# Math in markdown

Inline math sits on the text baseline: $E = mc^2$, $\sqrt{a^2 + b^2}$,
$\sum_{i=0}^{n} i = \frac{n(n+1)}{2}$.

Display math is centered on its own line:

$$
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
$$

Sums, integrals, matrices all work:

$$
\int_{-\infty}^{\infty} e^{-x^2}\, dx = \sqrt{\pi}
\qquad
A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}
\qquad
e^{i\pi} + 1 = 0
$$
    """)

immapp.run(gui, with_latex=True)   # implies with_markdown=True

Note the raw string (r"""..."""): without it, Python would interpret sequences like \theta as escape characters and silently corrupt the LaTeX.

Mermaid diagrams

```mermaid blocks are drawn natively, with ImDrawList and the colors of the ImGui style: flowcharts, sequence diagrams and class diagrams (a subset of Mermaid). A diagram that cannot be parsed is shown as code, with the line of the error.

```mermaid
flowchart LR
    A[Markdown] --> B{Mermaid block?}
    B -->|yes| C([Parse]) --> D[Layout]
    B -->|no| E[Code block]
```

To draw a diagram outside of markdown: rich_md.render_mermaid(source) (Python), RichMd::RenderMermaid(source) (C++). The library’s Mermaid tour shows more of them, and what is supported lists the syntax it reads and how its layout differs from mermaid.js.

Documents

A long text reads better as a document: a scroll area of its own, a table of contents beside it, links between its sections, and a search.

Python
C++
from imgui_bundle import imgui, immapp, rich_md


def gui() -> None:
    with rich_md.document("doc"):
        rich_md.render("# Intro\n\nSee [the widgets](#a-section-of-widgets).\n\n## Details\n\nSome text.")
        rich_md.document_heading(2, "A section of widgets")
        imgui.button("A button")
        rich_md.render("## The end\n\nBack to [the top](#intro).")


immapp.run(gui, with_markdown=True)

Demo: Try online | Python | C++

Narrative programming

A program can tell its own story. Its comments and strings hold named markdown sections (::md Name) and code regions (::code), and markdown transcludes them with an embed alone on its line, as in Obsidian: ![[#Square]] for the prose of a section, ![[#Square#code]] for its code, ![[other_file.py#Name]] for another file. The story chooses its order; the program keeps its own.

Python
C++
from imgui_bundle import immapp, rich_md

r"""::md Intro
# A program that tells its own story
The story picks the parts of the file it needs, in its own order:
![[#Square]]
![[#Square#code]]
"""

# ::md Square
# The square of a number, $x^2$:
# ::code
def square(x: float) -> float:
    return x * x
# ::endcode


def gui() -> None:
    rich_md.render_this_file("Intro")


immapp.run(gui, with_latex=True)

render_this_file("Intro") renders the section Intro of the calling file, its transclusions resolved. A section written in line comments ends with its code (::endcode), or with ::endmd when it has none; a section in a string or a block comment ends with it.

Example: the Mandelbrot set as a map of Julia sets, an interactive lesson in a single Python file (source). The idea: narrative programming; the syntax in full: specification.

Full Demo

Try online | Python | C++

The library’s own examples, on stock Dear ImGui (C++): a tour of every feature, an editor, a custom host, fonts. Try online | C++

Documented APIs

ImGuiColorTextEdit - Syntax Highlighting Editor & Diff Viewer

Introduction

ImGuiColorTextEdit is a syntax highlighting text editor and diff viewer for ImGui (originally by BalazsJako, rewritten from scratch by Johan A. Goossens).

Dear ImGui Bundle uses a fork with a few additions for Python bindings.

Features:

Full Demo

Try online | Python | C++

Documented APIs