Feature: Add command-driven plot(...) that renders to PNG and inserts into Word#2
Draft
Feature: Add command-driven plot(...) that renders to PNG and inserts into Word#2
Conversation
…uplot backends Co-authored-by: JohnnyTheCoder1 <111908128+JohnnyTheCoder1@users.noreply.github.com>
…ocumentation Co-authored-by: JohnnyTheCoder1 <111908128+JohnnyTheCoder1@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Feature: Add command-driven plot(...) that renders to PNG and inserts into Word
Feature: Add command-driven plot(...) that renders to PNG and inserts into Word
Sep 26, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR implements a new command-driven plotting feature for WordMat that allows users to create mathematical plots using simple text commands directly in Word documents.
Overview
Users can now type plot commands like:
When the user selects the text and clicks Plot Selection (or uses a keyboard shortcut), WordMat:
Key Features
sin,cos,exp, etc.) with no arbitrary code executionImplementation
New VBA Modules
Modified VBA Modules
Rib_PlotSelectionribbon callbackPlotSelectionto keyboard shortcut enumDocumentation
Usage
Testing
The implementation includes comprehensive tests:
Dependencies
matplotlibandnumpypackagesgnuplotexecutable in system PATHBoth backends are optional - users can choose their preferred one based on their setup.
Motivation
This feature addresses the need for:
Resolves #[issue-number] by providing the requested command-driven plotting workflow with both Matplotlib and Gnuplot backend support.
Original prompt
This section details on the original issue you should resolve
<issue_title>Feature: Add command-driven plot(...) that renders to PNG and inserts into Word</issue_title>
<issue_description>Add a text-based plotting workflow to WordMat so users can type commands like:
plot(sin(x), -2π, 2π)
plot(x^2, -5, 5; title="Parabola", grid=true, width=600, height=400, dpi=150)
plot(sin(x), -10, 10; backend="gnuplot")
When the user runs WordMat → Plot selection (or a keyboard shortcut), WordMat:
Parses the selected text for a plot(...) command.
Calls an external backend (Python/Matplotlib or Gnuplot) to render a PNG.
Inserts the PNG at the cursor (inline image) with optional caption/alt text.
This provides a command-driven experience (like MATLAB/Octave/Python) while keeping deliverables inside Word.
Motivation
Current plotting is GUI-driven (dialogs/GeoGebra). Some users prefer commands over UI.
Reproducibility: the plot(...) text in the doc becomes a source of truth you can re-plot at any time.
Faster iteration for assignments and reports that require many simple function plots.
User stories
As a student, I want to type plot(sin(x), -2π, 2π) and get a clean plot in my document without leaving Word.
As a power user, I want to choose Matplotlib or Gnuplot as the rendering backend and control size/DPI/grid/title via keywords.
As a grader/teacher, I want to re-generate plots from the original plot(...) commands embedded in the doc for verification.
Non-goals (v1)
No 3D plots in v1.
No multi-axis or subplots (one axes per command).
No inline live previews; only generated PNGs.
Not parsing arbitrary OMath/Equation objects (v1 expects plain text selection).
UX / UI
Ribbon: Add Plot selection button under WordMat → Graphs (or new Commands group).
Keyboard shortcut: Alt+W, P (or configurable).
Options dialog (new “Plotting” tab):
Backend: Matplotlib (Python) | Gnuplot
Backend path(s): Python executable path, Gnuplot executable path
Defaults: width (px), height (px), dpi, grid on/off, line width
Timeout (seconds)
Keep temp images (debug) on/off
Context menu: (optional) “Plot selection with WordMat”.
Command syntax (v1)
EBNF (minimal)
plot = "plot" "(" expr "," xmin "," xmax [ ";" options ] ")"
expr = function of x using allowed tokens (see whitelist)
xmin = number | constant | expression using pi
xmax = number | constant | expression using pi
options = option { "," option }
option = key "=" value
key = "title" | "grid" | "width" | "height" | "dpi" | "backend" | "linewidth"
value = string | number | boolean
Examples
plot(sin(x), -2π, 2π)
plot(x^2 + 2x + 1, -10, 10; title="Quadratic", grid=true, dpi=200)
plot(sin(x)/x, -20, 20; linewidth=2)
plot(cos(3x), 0, 10; backend="gnuplot", width=800, height=400)
Allowed functions/tokens (v1)
Constants: pi (and π), e
Functions: sin, cos, tan, asin, acos, atan, exp, log, ln, sqrt, abs
Operators: + - * / ^
Parentheses and whitespace
Security: No raw eval on user input. Map tokens to a whitelist of math functions and parse safely.
High-level design
Flow
Get selection: If selection text matches plot(…), proceed; else show a helpful error.
Parse: Use a small hand-rolled parser or a simple tokenizer to extract expr, xmin, xmax, options.
Normalize: Replace π with pi, ^ with ** (for Python), etc.
Backend dispatch:
If backend option set, honor it; otherwise use global default from Options.
Render:
Call external process (Python or Gnuplot) with a generated script and a temp output PNG path.
Insert:
Use Word COM: InlineShapes.AddPicture(tempPng, LinkToFile: false, SaveWithDocument: true).
Optionally set width/height in points to match pixels at 96 DPI, or scale proportionally.
Cleanup: Delete temp script (unless “Keep temp” is enabled).
Project structure (suggested)
Plotting/PlotCommandHandler.cs — entry point from Ribbon/shortcut
Plotting/Parser/PlotParser.cs — tokenizer + AST + options
Plotting/Backends/IPlotBackend.cs — interface
Plotting/Backends/MatplotlibBackend.cs
Plotting/Backends/GnuplotBackend.cs
Plotting/Word/WordInserter.cs — image insertion utilities
Options/PlotOptionsPage.cs — options UI
Word integration details (C# / VSTO)
// Pseudocode
void PlotSelection()
{
var sel = Globals.ThisAddIn.Application.Selection;
string text = sel?.Range?.Text?.Trim() ?? string.Empty;
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.