• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

stefano-meschiari/latex2exp: Use LaTeX in R graphics.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

stefano-meschiari/latex2exp

开源软件地址(OpenSource Url):

https://github.com/stefano-meschiari/latex2exp

开源编程语言(OpenSource Language):

R 100.0%

开源软件介绍(OpenSource Introduction):

R-CMD-check

latex2exp

latex2exp is an R package that lets you use LaTeX in plots. It parses and converts LaTeX to R’s custom plotmath expressions. You can read the full documentation on the package’s website.

Expressions returned by latex2exp can be used to create formatted text and mathematical formulas and symbols to be rendered as axis labels, annotations, legends, titles, etc. throughout R’s plotting system.

Installation

Install this package from CRAN:

install.packages('latex2exp')

You can also install the development version from GitHub using devtools:

devtools::install_github('stefano-meschiari/latex2exp')

Usage

The TeX function takes a LaTeX string, parses it, and returns the closest plotmath expression suitable for use in graphics. The return value of TeX() can be used anywhere a plotmath expression is accepted, including plot labels, legends, and text for both base graphics and ggplot2.

Here’s a simple example:

# Use raw strings, no need to escape backslashes.
TeX(r"(\textbf{Euler's identity} is $e^{i\pi} + 1 = 0$.)")

In this example, \textbf{} is used to mark a fragment of text as bold, $ introduces inline math mode, ^{} typesets its contents as superscript, and \pi typesets the letter π.

Starting with R 4.0, it is recommended to use the new raw string literal syntax (see ?Quotes). The syntax looks like r"(...)", where ... can contain any character sequence, including \.

Another option is to escape the backslash character (\) for LaTeX commands, such that the command will be written as \\command rather than \command. This will also work on versions of R older than 4.0:

# Equivalent to the previous code fragment.
# Use regular strings, but escape the backslashes.
TeX("\\textbf{Euler's identity} is $e^{i\\pi} + 1 = 0$.")

You can quickly preview what a translated LaTeX string would look like by using plot:

plot(TeX(r'(A $\LaTeX$ formula: $\frac{2hc^2}{\lambda^5}\frac{1}{e^{\frac{hc}{\lambda k_B T}} - 1}$)'), cex=2, main="")

The following example shows plotting in base graphics:

x <- seq(0, 4, length.out=100)
alpha <- 1:5

plot(x, xlim=c(0, 4), ylim=c(0, 10), 
     xlab='x', ylab=TeX(r'($\alpha  x^\alpha$, where $\alpha \in \{1 \ldots 5\}$)'), 
     type='n', main=TeX(r'(Using $\LaTeX$ for plotting in base graphics!)', bold=TRUE))

for (a in alpha) {
  lines(x, a*x^a, col=a)
}

legend('topleft', 
       legend=TeX(sprintf(r'($\alpha = %d$)', alpha)), 
       lwd=1, 
       col=alpha)

This example shows plotting in ggplot2:

x <- seq(0, 4, length.out=100)
alpha <- 1:5
data <- map_df(alpha, ~ tibble(v=.*x^., x=x, alpha=.))

p <- ggplot(data, aes(x=x, y=v, color=as.factor(alpha))) +
    geom_line() + 
    ylab(TeX(r'($\alpha  x^\alpha$, where $\alpha \in 1\ldots 5$)')) +
    ggtitle(TeX(r'(Using $\LaTeX$ for plotting in ggplot2. I $\heartsuit$ ggplot!)')) +
    coord_cartesian(ylim=c(-1, 10)) +
    guides(color=guide_legend(title=NULL)) +
    scale_color_discrete(labels=lapply(sprintf(r'($\alpha = %d$)', alpha), TeX)) 
    # Note that ggplot2 legend labels must be lists of expressions, not vectors of expressions

print(p)

Here are a few examples of what you can do with latex2exp:

latex2exp_examples(cex=0.9)

Supported LaTeX commands

Table of supported LaTeX




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap