Sharing Math Formulas Across Platforms: An Engineering Pipeline from Source Code to Standalone Images
Math formulas are usually stored as typesetting source code that only renders correctly in environments with a matching engine. This article explains why formulas are hard to share across chat apps, social networks, and web platforms, compares common workarounds, and walks through a local rendering pipeline with four stages—extraction, rendering, post-processing, and output—covering template design, trimming, DPI settings, naming conventions, and cache strategies for consistent, reproducible formula images.
Why Formulas Are Hard to Share Across Platforms
In academic writing, formulas are typically saved as typesetting source code inside documents. The source describes structure—the numerator and denominator of a fraction, the placement of subscripts and superscripts, the choice of Greek letters—rather than the final pixels. Only an environment with the corresponding typesetting engine can render that source back into a formula.
Most chat platforms, social networks, and messaging apps support only plain text or limited rich text. Pasting source code directly gives readers a jumble of backslashes, curly braces, and subscript symbols that is hard to read, and the display result varies across clients.
There are three common workarounds:
- Screenshotting a full PDF page: cropping is rough, font sizes and margins are inconsistent, and the result clashes visually when placed into slides or blog posts.
- Using an online conversion service: easy to operate, but it requires handing unpublished research content to a third party, speed depends on the network, and service availability is outside your control.
- Dropping formulas in favor of textual descriptions: significant information loss, and readers can hardly verify the derivation.
None of these options simultaneously delivers clarity, consistency, batch processing, and control. Rendering each formula into a standalone image locally is the more reliable choice.
Comparing the Options
| Dimension | Manual screenshots | Online conversion | Local rendering pipeline |
|---|---|---|---|
| Visual consistency | Low | Medium | High |
| Data privacy | High | Low | High |
| Batch processing | Poor | Medium | Good |
| Initial investment | None | None | Environment setup required |
| Reproducibility | Low | Depends on external services | High |
The main advantages of a local pipeline are reproducibility and control. The initial investment goes into environment setup and template design. Once that is done, the marginal cost of adding a formula is close to zero, and since all formulas share the same style, the visual appearance of the whole document stays naturally consistent.
The Four Stages of the Pipeline
A complete pipeline consists of four stages: extraction, rendering, post-processing, and output. The input and output of each stage should be clearly defined so that any failure can be precisely located.
3.1 Formula Extraction
Formulas in a document take several forms: inline formulas, display formulas on their own lines, and numbered multi-line environments. The extractor needs to recognize these delimiters, skip escaped dollar signs and comments, and handle nested curly braces correctly.
Below is language-agnostic pseudocode for the extraction logic:
function extract_formulas(source):results = []cursor = 0while true:open = find_next_delimiter(source, cursor)if open is none:breakclose = find_matching_close(source, open)if close is none:raise ParseError("Unclosed formula at position: " + open.position)results.append(source[open.end : close.start])cursor = close.endreturn results
Three things commonly go wrong in implementation. First, escaped delimiters must not be treated as formula boundaries. Second, comment content should be skipped entirely, otherwise spurious formulas appear. Third, curly braces must be paired using a counter, or nested fractions and superscripts will cause boundary detection errors. When the extractor encounters an unmatched delimiter, it should fail immediately with the position in the source file rather than silently dropping it.
3.2 Unified Rendering
The core of the rendering stage is applying the same page template to every formula. The template fixes the font, font size, page dimensions, and required symbol packages, with the formula source as the only variable part.
This brings two benefits. First, baselines, font sizes, and spacing remain consistent across all formulas, so they never appear mismatched in size when placed side by side. Second, the template itself can be versioned; when the template changes, all formulas can be regenerated with the new version.
Rendering is best done in two steps: first produce a vector result, then rasterize it to a bitmap at the target resolution. The vector result preserves infinite scalability for future print or high-resolution use. Each formula should be compiled independently so that one formula’s syntax error does not block the entire batch. Failed formulas should be logged with error messages and source positions, allowing you to fix and rerun them individually.
3.3 Post-Processing
Rendered output usually carries large page margins. In post-processing, compute the bounding box of non-background pixels, crop away the excess whitespace, and then add a fixed-width inner padding. Fixed padding ensures consistent visual spacing between different formulas in the document.
Background handling depends on the use case. A transparent background works well when overlaying on colored surfaces, but on dark-mode pages, dark formulas may become hard to read. In that scenario, you can output an additional white-background version.
Resolution should be set according to the use case. For screen display, 150–200 dpi is usually crisp enough; for print, at least 300 dpi is recommended. Excessively high resolution inflates file size and slows page loading, so uniformly using the maximum value is not advisable.
3.4 Output and Caching
Output filenames should be stable. A combination of section number plus formula index is preferable to timestamps, so replacing images after document edits does not leave orphaned old files. Also maintain a manifest file recording each formula ID, a source code digest, and the output path for verification and traceability.
Caching significantly reduces repeated work. Use a hash of the template version concatenated with the formula source as the cache key, and only re-render when the formula or the template changes:
for formula in formulas:key = hash(template_version + formula.source)if cache contains key:reuse cache[key]else:image = render(formula, template)cache[key] = image
The cache key must include the template version. After a font file or template upgrade, if the key remains unchanged, stale images will be incorrectly reused.
