expr2latex()
Summary: The implementation of expr2latex()
seems pretty incomplete (see its conversion of ?plotmath
expressions below). BTW, the source of these two files will be invaluable if anyone decides to implement something more complete:
* https://github.com/cran/simsalapar/blob/master/R/expr2latex.R
* https://github.com/wch/r-source/blob/trunk/src/main/plotmath.c
expr2tex <- function(x, inline = FALSE) {
tag <- if (inline) "$" else "$$"
tex <- paste0(tag, simsalapar::expr2latex(x), tag)
knitr::asis_output(tex)
}
expr2tex( quote( N[sim] ) )
\[N_{sim}\]
An inline equation: \(N_{sim} \ O(n)\)
expr2tex( quote(x %notin% N) )
\[x \not\in N\]
expr2tex( quote(x %+-% epsilon) )
\[x \pm \epsilon\]
expr2tex( quote(N[s*m^2]) )
\[N_{s m ^ 2}\]
expr2tex( quote( 2^{N[sim] - 3} ~~~ O(n^{n^2}) ) )
\[2 ^ {N_{sim} - 3} \ \ \ O(n ^ {n ^ 2})\]
Hmm, what happened to the ~
?
expr2tex(quote(X[i] ~ N(mu, sigma^2)))
\[X_{i} \ N(\mu,\sigma ^ 2)\]
From ?plotmath
(via datapasta):
d <- tibble::tribble(
~Syntax, ~Meaning,
quote(x + y), "x plus y",
quote(x - y), "x minus y",
quote(x*y), "juxtapose x and y",
quote(x/y), "x forwardslash y",
quote(x %+-% y), "x plus or minus y",
quote(x %/% y), "x divided by y",
quote(x %*% y), "x times y",
quote(x %.% y), "x cdot y",
quote(x[i]), "x subscript i",
quote(x^2), "x superscript 2",
quote(paste(x, y, z)), "juxtapose x, y, and z",
quote(sqrt(x)), "square root of x",
quote(sqrt(x, y)), "yth root of x",
quote(x == y), "x equals y",
quote(x != y), "x is not equal to y",
quote(x < y), "x is less than y",
quote(x <= y), "x is less than or equal to y",
quote(x > y), "x is greater than y",
quote(x >= y), "x is greater than or equal to y",
quote(x %~~% y), "x is approximately equal to y",
quote(x %=~% y), "x and y are congruent",
quote(x %==% y), "x is defined as y",
quote(x %prop% y), "x is proportional to y",
quote(x %~% y), "x is distributed as y",
quote(plain(x)), "draw x in normal font",
quote(bold(x)), "draw x in bold font",
quote(italic(x)), "draw x in italic font",
quote(bolditalic(x)), "draw x in bolditalic font",
quote(symbol(x)), "draw x in symbol font",
#quote(list(x, y, z)), "comma-separated list",
quote(...), "ellipsis (height varies)",
quote(cdots), "ellipsis (vertically centred)",
quote(ldots), "ellipsis (at baseline)",
quote(x %subset% y), "x is a proper subset of y",
quote(x %subseteq% y), "x is a subset of y",
quote(x %notsubset% y), "x is not a subset of y",
quote(x %supset% y), "x is a proper superset of y",
quote(x %supseteq% y), "x is a superset of y",
quote(x %in% y), "x is an element of y",
quote(x %notin% y), "x is not an element of y",
quote(hat(x)), "x with a circumflex",
quote(tilde(x)), "x with a tilde",
quote(dot(x)), "x with a dot",
quote(ring(x)), "x with a ring",
quote(bar(xy)), "xy with bar",
quote(widehat(xy)), "xy with a wide circumflex",
quote(widetilde(xy)), "xy with a wide tilde",
quote(x %<->% y), "x double-arrow y",
quote(x %->% y), "x right-arrow y",
quote(x %<-% y), "x left-arrow y",
quote(x %up% y), "x up-arrow y",
quote(x %down% y), "x down-arrow y",
quote(x %<=>% y), "x is equivalent to y",
quote(x %=>% y), "x implies y",
quote(x %<=% y), "y implies x",
quote(x %dblup% y), "x double-up-arrow y",
quote(x %dbldown% y), "x double-down-arrow y",
quote(alpha -- omega), "Greek symbols",
quote(Alpha -- Omega), "uppercase Greek symbols",
#quote(theta1, phi1, sigma1, omega1), "cursive Greek symbols",
quote(theta1), "cursive Greek symbols",
quote(Upsilon1), "capital upsilon with hook",
quote(aleph), "first letter of Hebrew alphabet",
quote(infinity), "infinity symbol",
quote(partialdiff), "partial differential symbol",
quote(nabla), "nabla, gradient symbol",
quote(32*degree), "32 degrees",
quote(60*minute), "60 minutes of angle",
quote(30*second), "30 seconds of angle",
quote(displaystyle(x)), "draw x in normal size (extra spacing)",
quote(textstyle(x)), "draw x in normal size",
quote(scriptstyle(x)), "draw x in small size",
quote(scriptscriptstyle(x)), "draw x in very small size",
quote(underline(x)), "draw x underlined",
quote(x ~~ y), "put extra space between x and y",
quote(x + phantom(0) + y), "leave gap for \"0\", but don\'t draw it",
quote(x + over(1, phantom(0))), "leave vertical gap for \"0\" (don\'t draw)",
quote(frac(x, y)), "x over y",
quote(over(x, y)), "x over y",
quote(atop(x, y)), "x over y (no horizontal bar)",
quote(sum(x[i], i==1, n)), "sum x[i] for i equals 1 to n",
quote(prod(plain(P)(X==x), x)), "product of P(X=x) for all values of x",
quote(integral(f(x)*dx, a, b)), "definite integral of f(x) wrt x",
quote(union(A[i], i==1, n)), "union of A[i] for i equals 1 to n",
quote(intersect(A[i], i==1, n)), "intersection of A[i]",
quote(lim(f(x), x %->% 0)), "limit of f(x) as x tends to 0",
quote(min(g(x), x > 0)), "minimum of g(x) for x greater than 0",
quote(inf(S)), "infimum of S",
quote(sup(S)), "supremum of S",
quote(x^y + z), "normal operator precedence",
quote(x^(y + z)), "visible grouping of operands",
quote(x^{y + z}), "invisible grouping of operands",
quote(group("(",list(a, b),"]")), "specify left and right delimiters",
quote(bgroup("(",atop(x,y),")")), "use scalable delimiters",
quote(group(lceil, x, rceil)), "special delimiters",
quote(group(lfloor, x, rfloor)), "special delimiters"
)
result <- lapply(d$Syntax, function(expr) {
tex <- tryCatch(expr2tex(expr), error = function(e) e$message)
cat(tex)
tex
})
\[x + y\]\[x - y\]\[x y\]\[x / y\]\[x \pm y\]\[x \div y\]\[x \times y\]\[x \cdot y\]\[x_{i}\]\[x ^ 2\]length(expr) = 4 (>= 4); not yet implemented\[sqrt(x)\]\[sqrt(x,y)\]\[x = y\]\[x \ne y\]\[x < y\]\[x \le y\]\[x > y\]\[x \ge y\]\[x \approx y\]\[x \cong y\]\[x \equiv y\]\[x \propto y\]\[x \sim y\]\[plain(x)\]\[\mathbf{x}\]\[\mathit{x}\]\[bolditalic(x)\]\[symbol(x)\]\[...\]\[cdots\]\[ldots\]\[x \subset y\]\[x \subseteq y\]\[x \notsubset y\]\[x \supset y\]\[x \supseteq y\]\[x \in y\]\[x \not\in y\]\[hat(x)\]\[tilde(x)\]\[dot(x)\]\[ring(x)\]\[bar(xy)\]\[widehat(xy)\]\[widetilde(xy)\]\[x \leftrightarrow y\]\[x \rightarrow y\]\[x \leftarrow y\]\[x \uparrow y\]\[x \downarrow y\]\[x \Leftrightarrow y\]\[x \Rightarrow y\]\[x \Leftarrow y\]\[x \Uparrow y\]\[x \Downarrow y\]\[\alpha - - \omega\]\[\Alpha - - \Omega\]\[\theta1\]\[Upsilon1\]\[aleph\]\[infinity\]\[partialdiff\]\[nabla\]\[32 degree\]\[60 minute\]\[30 second\]\[displaystyle(x)\]\[textstyle(x)\]\[scriptstyle(x)\]\[scriptscriptstyle(x)\]\[underline(x)\]\[x \ \ y\]\[x + phantom(0) + y\]\[x + over(1,phantom(0))\]\[frac(x,y)\]\[over(x,y)\]\[atop(x,y)\]length(expr) = 4 (>= 4); not yet implemented\[prod(plain(P)(X = x),x)\]length(expr) = 4 (>= 4); not yet implementedlength(expr) = 4 (>= 4); not yet implementedlength(expr) = 4 (>= 4); not yet implemented\[lim(f(x),x \rightarrow 0)\]\[min(g(x),x > 0)\]\[inf(S)\]\[sup(S)\]\[x ^ y + z\]\[x ^ (y + z)\]\[x ^ {y + z}\]length(expr) = 4 (>= 4); not yet implementedlength(expr) = 4 (>= 4); not yet implementedlength(expr) = 4 (>= 4); not yet implementedlength(expr) = 4 (>= 4); not yet implemented