# jgreitemann/latex-gnuplot architecture

> Architecture diagram of the GitHub repository jgreitemann/latex-gnuplot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jgreitemann/latex-gnuplot
- Repository: https://github.com/jgreitemann/latex-gnuplot

## Overview

##### Main Components
- Command-Line Interface (CLI) & Argument Parser: Represents the entry point that accepts options (like --pdf, --svg, --preamble, --inject, and --engine), input file names, and additional configuration.
- Temporary Directory Manager: A module responsible for copying the input gnuplot script and any data files into a temporary workspace so that intermediate files do not pollute the working directory.
- Gnuplot Execution Module: Handles the invocation of gnuplot on the copied script. It processes the gnuplot script and generates the intermediate .tex file containing the figure components (titles, axes, etc.).
- Template Processor: Reads the chosen LaTeX template file(s) — from directories like the local user directory or system paths as described in the README — and performs substitutions. Tokens like THEFILENAME, THEPREAMBLE, and THEINJECTION are replaced with the proper content (the gnuplot output, provided preamble, or injected LaTeX commands).
- LaTeX Engine Invoker: Executes the LaTeX typesetting process (using pdflatex, xelatex, etc.) to compile the full LaTeX file, generating the final output format(s) such as PDF, EPS, and/or SVG.
- Output Manager & Cleanup Module: Responsible for copying the final output back from the temporary directory to the working directory and then cleaning up temporary files unless the user opts to preserve them (e.g. with the --no-cleanup flag).

##### Relationships and Interactions
- The CLI receives user input and passes configuration along to the other modules.
- The Temporary Directory Manager takes the input files and provides an isolated workspace.
- The Gnuplot Execution Module runs first in the pipeline, outputting an intermediate .tex file.
- This output is then passed to the Template Processor, which embeds it into the appropriate LaTeX template (selected via a command-line flag or by default).
- The fully prepared LaTeX file is sent to the LaTeX Engine Invoker, which carries out the typesetting.
- Finally, the Output Manager ensures that the final figures are stored in the user’s working directory, and if no inspection of intermediate files is requested, the Temporary Directory Manager cleans up the workspace.

##### Architectural Patterns & Design Principles
- Pipeline Architecture: The overall design is a sequential “pipeline” where the output of one stage is the input to the next (from file copying to gnuplot execution, followed by template processing and LaTeX compilation).
- Separation of Concerns: Each module clearly addresses a specific aspect of processing (CLI, temporary file handling, gnuplot execution, templating, and LaTeX compilation), making debugging and future enhancements easier.
- Configurability & Extensibility: Through command-line flags, template substitution tokens, and the ability to inject custom preamble or commands, the design promotes flexibility for various output formats and custom use cases (journal submissions, web embedding, etc.).

##### Additional Details to Consider
- Reference the “templates” directory to show that template selection is hierarchical (from $HOME to /usr/share/latex-gnuplot/) and that templates undergo token substitution.
- External data flows such as user-provided gnuplot scripts and data files, and the final image outputs.
- Mention the optional branching (such as depending on provided flags: whether additional output formats like SVG are generated).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Processing Pipeline"
        A["User Input"]:::input
        B["CLI & Argument Parser"]:::processor
        C["Temporary Directory Manager"]:::support
        D["Gnuplot Runner"]:::processor
        E["Template Processor"]:::processor
        F["LaTeX Compiler"]:::processor
        G["Output Manager & Cleanup"]:::support
        H["Final Output"]:::output
    end

    A -->|"provides script & options"| B
    B -->|"copies files to temp directory"| C
    C -->|"sets workspace for execution"| D
    D -->|"generates intermediate .tex file"| E
    E -->|"injects gnuplot output into template"| F
    F -->|"compiles LaTeX file"| G
    G -->|"stores final output"| H

    %% External Dependencies
    subgraph "External Tools"
        I["gnuplot"]:::external
        J["LaTeX Engine"]:::external
    end

    D ---|"invokes"| I
    F ---|"runs"| J

    %% Click Events
    click B "https://github.com/jgreitemann/latex-gnuplot/blob/master/latex-gnuplot.sh"
    click D "https://github.com/jgreitemann/latex-gnuplot/blob/master/latex-gnuplot.sh"
    click F "https://github.com/jgreitemann/latex-gnuplot/blob/master/latex-gnuplot.sh"
    click G "https://github.com/jgreitemann/latex-gnuplot/blob/master/latex-gnuplot.sh"
    click E "https://github.com/jgreitemann/latex-gnuplot/tree/master/templates/"

    %% Styles
    classDef input fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef processor fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef support fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef output fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef external fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
```
