The 'pic' Graphics Language Rendered to SVG, PNG and PDF

Renders diagrams written in the 'pic' picture-drawing language (Kernighan, 1984) to SVG, PNG and PDF, using 'rpic' < https://github.com/milkway/rpic-lang>, a reimplementation of 'pic' in 'Rust' with no system dependencies. Includes a native circuit-element library in the spirit of 'circuit_macros', TeX math labels typeset natively, structured compile diagnostics, and a 'knitr' language engine for inline diagrams in 'R Markdown' and 'Quarto' documents.


rpic rpic website

R-CMD-check pkgdown CRAN status GitHub release License: BSD-2

R bindings for rpic — a Rust reimplementation of Brian Kernighan's pic picture-drawing language. You describe a diagram by walking around a plane dropping primitives; rpic renders it to SVG / PNG / PDF, pure Rust, no system dependencies (no troff, no LaTeX, no ImageMagick):

  • 79 native circuit elements — a from-scratch re-imagining of circuit_macros (circuits = TRUE or copy "circuits" in the source);
  • TeX math labels typeset natively (texlabels = TRUE, KaTeX-grade via a pure-Rust engine);
  • structured diagnostics — compile errors are classed conditions with exact positions and did-you-mean hints;
  • a knitr engine for inline diagrams in R Markdown / Quarto.

The language, its extensions and a live playground are documented at rpic.dev; this package wraps the same engine via extendr.

Install

# install.packages("remotes")
remotes::install_github("milkway/rpic-r")

Building from source requires a Rust toolchain (cargo/rustc). Release tarballs with vendored Rust dependencies (no network needed at build time) are attached to each GitHub release.

Usage

library(rpic)

rpic_svg('box "input"; arrow; box "process" fill 0.9; arrow; ellipse "output"')

# circuit library — two-terminal elements take two named points:
rpic_png('A:(0,0); B:(2,0)
resistor(A,B)', "circuit.png", scale = 2, circuits = TRUE)

# TeX math labels, typeset natively:
rpic_svg('box "$-\\frac{T}{2}$" fit', texlabels = TRUE)

# the full bundle: svg + animation manifest + diagnostics + warnings
jsonlite::fromJSON(rpic_manifest('box; animate last box with "pop"'))

Errors you can point at

Compile failures raise a classed rpic_error condition carrying the structured diagnostic — position (always relative to your source, even with circuits = TRUE), kind, and a did-you-mean hint:

tryCatch(
  rpic_svg("bxo", circuits = TRUE),
  rpic_error = function(e) list(line = e$info$line, hint = e$info$hint)
)
#> $line
#> [1] 1
#> $hint
#> [1] "did you mean `box`?"

e$info$file names a copy include when the problem is inside one (NA means your own input).

knitr / Quarto engine

rpic::rpic_register_knitr()

then write pic code directly in a chunk:

```{rpic, circuits=TRUE, scale=2}
A:(0,0); B:(2,0)
resistor(A,B)
```

Chunk options: circuits, texlabels, scale.

Develop

devtools::load_all(".")      # compiles the Rust and loads the package

For an installable/CRAN-style tarball, the Rust dependencies are vendored:

rextendr::vendor_crates(".")   # bundles crate sources into src/rust/vendor.tar.xz
R CMD build .

Acknowledgments

pic was created by Brian W. Kernighan; dpic and circuit_macros are J. D. Aplevich's; pikchr is D. Richard Hipp's. See ACKNOWLEDGMENTS in the engine repository.

License

BSD-2-Clause. Compiled Rust dependencies are acknowledged in inst/AUTHORS.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("rpic")

0.11.3 by André Leite, 8 days ago


https://milkway.github.io/rpic-r/, https://github.com/milkway/rpic-r, https://github.com/milkway/rpic-lang


Report a bug at https://github.com/milkway/rpic-r/issues


Browse source code at https://github.com/cran/rpic


Authors: André Leite [aut, cre, cph] (ORCID: , The authors of the dependency Rust crates [cph] (see inst/AUTHORS for details)


Documentation:   PDF Manual  


BSD_2_clause + file LICENSE license


Suggests knitr, rmarkdown

System requirements: Cargo (Rust's package manager), rustc (>= 1.88.0)


See at CRAN