pic() wraps pic source code in an rpic_source object: the source
string together with the options it needs, circuits and texlabels.
Every rpic_*() function takes an rpic_source (or a plain string) as
its first argument, so a drawing reads as a pipe:
pic(src) |> rpic_png("a.png"), pic(src) |> rpic_sf(),
pic(src) |> rpic_objects(). When an rpic_*() function is given an
rpic_source, its circuits and texlabels are the defaults for those
arguments (an explicit argument still wins); a plain string defaults to
FALSE for both, as before.
Usage
pic(src, circuits = NULL, texlabels = NULL)
# S3 method for class 'rpic_source'
format(x, n = 6, ...)
# S3 method for class 'rpic_source'
print(x, n = 6, view = interactive(), ...)
# S3 method for class 'rpic_source'
knit_print(x, options = list(), ...)Arguments
- src
pic source code: a string, a character vector (its elements are the lines, joined with newlines — e.g. from
readLines()) or anrpic_source(whose options are then updated).- circuits
load the native circuit-element library:
TRUE,FALSEorNULLto detect it (see Detection).- texlabels
typeset fully
$...$-delimited labels as TeX math:TRUE,FALSEorNULLto detect it (see Detection).- x
an
rpic_source.- n
- ...
unused.
- view
render and open the drawing (see Printing).
- options
knitr chunk options (supplied by knitr).
Value
pic(): an object of class rpic_source — the source as a
character scalar with the attributes circuits and texlabels
(logicals). format(): a character vector. print(): x, invisibly.
knit_print(): knitr output.
Detection
With circuits = NULL or texlabels = NULL (the default), the option is
detected from the source, the way the rpic Studio does — outside string
literals and # comments:
circuitsisTRUEwhen the source uses a macro of the native circuit-element library (one of the public macros of the engine'scircuits.pic) — called (resistor(A,B),and_gate (P)) or bare (ground; the engine expands a macro name without arguments too) — or loads it itself withcopy "circuits"(loading it twice is harmless: the library only defines macros). A bare name the source assigns or labels anywhere (wire = 3,ground: ...) is the user's own variable or place and does not count, nor does a field such as.ground. A macro of your own with a library name counts.texlabelsisTRUEwhen some string literal (outside comments) is entirely one$...$(after trimming spaces):"$x^2$"is math,"costs $10"is not.
A # starts a comment outside strings only ("a # b" is a string), and
a " inside a comment opens no string.
Pass TRUE or FALSE to override either guess.
Printing
print() shows a summary (lines, characters, options) and the first
lines of the source; in an interactive session it also renders the
drawing to a temporary SVG file and opens it in the viewer
(getOption("viewer"), as in RStudio and Positron) or else the browser
(utils::browseURL()); a compile error is then reported as a message,
not raised. In a knitr document (R Markdown, Quarto) an rpic_source
left visible at the end of a chunk is drawn as the chunk's figure (a PNG,
like the rpic engine's; chunk option scale, default 2). format()
returns the summary line followed by the source lines.
Examples
p <- pic('box "in"; arrow; box "out"')
p
#> <pic source: 1 line, 26 characters>
#> box "in"; arrow; box "out"
rpic_svg(p) |> substr(1, 40)
#> [1] "<svg xmlns=\"http://www.w3.org/2000/svg\" "
# detected options
attr(pic("A:(0,0); B:(2,0)\nresistor(A,B)"), "circuits") # TRUE
#> [1] TRUE
attr(pic('box "$\\frac{T}{2}$" fit'), "texlabels") # TRUE
#> [1] TRUE
attr(pic('box "costs $10"'), "texlabels") # FALSE
#> [1] FALSE
# pipe-first
pic('A: box "A"; arrow; B: circle "B"') |> rpic_objects()
#> id kind line col end_col file bbox_x0 bbox_y0 bbox_x1 bbox_y1 link
#> 1 s0 box 1 4 7 <NA> 1.0667 0.5333 73.0667 48.5333 <NA>
#> 2 s1 path 1 13 18 <NA> 73.0667 22.1333 121.0667 26.9333 <NA>
#> 3 s2 circle 1 23 29 <NA> 121.0667 0.5333 169.0667 48.5333 <NA>