latex2exp

Use LaTeX in R. More LaTeX, less plotmath!
授权协议 View license
开发语言
所属分类 企业应用、 LaTeX排版系统
软件类型 开源软件
地区 不详
投 递 者 颜经艺
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

latex2exp

latex2exp is an R package that parses and converts LaTeX mathformulas to R’s plotmathexpressions.Plotmath expressions are used to enter mathematical formulas and symbolsto be rendered as text, axis labels, etc. throughout R’s plottingsystem. I find plotmath expressions to be quite opaque, while LaTeX is ade-facto standard for mathematical expressions, so this package might beuseful to others as well.

Installation

Install this package from CRAN:

install.packages('latex2exp')

You can also install from GitHub usingdevtools:

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

Usage

library(latex2exp)

The TeX function takes a LaTeX string and returns a plotmathexpression suitable for use in plotting, e.g.,

TeX('$\\gamma^\\alpha$')

Before R 4.0, it is necessary to escape the backslash within the stringliteral: therefore, one writes '$\\gamma$' rather than '$\gamma$'(the latter will cause an error).

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

TeX(r'($\gamma^\alpha$)')

The return value of TeX() can be used anywhere a plotmath expressionis accepted, including plot labels, legends, and text.

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, italic=TRUE))

invisible(sapply(alpha, function(a) 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:

library(purrr)
library(ggplot2)
library(tibble)

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)

You can quickly test out what a translated LaTeX string would look likeby 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)

Syntax

Use

TeX(r'(My string containing math $\sin(2\pi\lambda x)$)')

to build a plotmath expression, ready for use in plots. If the parsercannot build a correct plotmath expression, it will stop() and showthe invalid plotmath expression built.

Add parameters bold=TRUE or italic=TRUE to make the entire labelbold or italic:

TeX(r'(My string containing math $\sin(2\pi\lambda x)$)', bold=TRUE, italic=TRUE)
TeX('latexString', output=c('expression', 'character', 'ast'))

If the output option is equal to character, it will return thestring representation of the expression (which could be converted intoan expression using parse(text=)).

If the output option is equal to ast, it will return the tree builtby the parser (this is only useful for debugging).


latex2exp_examples()

will show a demo of the supported LaTeX syntax.


latex2exp_supported(plot=FALSE)

returns a list of supported LaTeX. If plot=TRUE, a table of symbolswill be plotted.

Supported LaTeX

Formulas should go between dollar characters ($).

Only a subset of LaTeX is supported. Greek symbols (\alpha, \beta,etc.) and the usual operators (+, -, etc.) are supported. Theirrendering depends on R’s interpretation of the plotmath expression.

In addition, the following should be supported:

latex2exp_supported(plot=TRUE)

A few examples:

latex2exp_examples()

## [1] TRUE

Changes

0.5.0 [03/14/2020]

  • Adds parameters bold and italic to TeX(). These can be used tomake the entire expression bold or italic.
  • Adds \phantom{}(PR)

0.4.0 [08/29/2015]

  • Deprecated the latex2exp() function; use TeX() instead.
  • Added \lbrack and \rbrack to type left and right squarebrackets.

0.3.3 [08/11/2015]

Fixes bug #4 (“fix parsing of numbers”), where certain numbers insideformulas where not parsed correctly.

0.3.2 [07/28/2015]

Fixes bug #3 (“subscript and superscript style”). latex2exp nowrenders combined subscripts and superscripts correctly.

0.3.1 [07/02/2015]

Fixes bug #2 (white space causes unexpected behaviour). latex2expshould now be a bit more compliant with how LaTeX handles whitespace.

0.3.0 [06/30/2015]

latex2exp is now a proper package.

0.2.0 [06/29/2015]

Formulas must now be enclosed between dollar characters ($), as in LaTeXproper. Text does not need to be enclosed in \text tags anymore.

  • R语言使用plot函数可视化数据散点图,使用latex2exp包的Tex函数在标题中添加数学符号(希腊字母、类似于latex) 目录

  • 我想使用或 的组合LaTeX为绘图元素添加排版R(例如:标题、轴标签、注释等)。base/latticeggplot2 CRAN 包 latex2exp包含一个TeX将 LaTeX 公式转换为 R 的 plotmath 表达式的函数。您可以在任何可以输入数学注释的地方使用它,例如轴标签、图例标签和一般文本。 library(latex2exp) x <- seq(0, 4, length.out=

  • 如何插入公式 LATEX的数学公式有两种:行中公式和独立公式(行间公式)。行中公式放在文中与其它文字混编,独立公式单独成行。 行中公式可以用如下方法表示: $ 数学公式 $ 独立公式可以用如下方法表示: $$ 数学公式 $$ 函数 指数 \exp_a b = a^b, \exp b = e^b, 10^m expab=ab,expb=eb,10mexpa⁡b=ab,exp⁡b=eb,10m 对数

  • Markdown 和 LaTeX 写作规范 事实上,在我写下这个标题的时候,就已经不规范了,因为 LaTeX 标准的写法是 LaTeX \LaTeX LATE​X,但我依然还是写成 LaTeX,是因为方便 SEO 搜索优化,以及读者错误的关键词搜索,也能找到这篇博客。 下面介绍一些我所总结的写作当中应该注意写作规范以及一些技巧,建议收藏,时时参照,规范写作,养成习惯。以下规范仅代表个人看法,如有意

  • latex 写在最前面: latex查询文档:https://latexref.xyz/ latex源文件的基本结构  % 导言区  \documentclass{article}%book,report,letter  \usepackage[UTF8]{ctex}%支持书写中文内容  \title  %正文区(文稿区)  \begin{document}   内容  \end{docum

  • LaTex Math的语法 每次想把公式打出来都不知道怎么写LaTex 公式,要将数学公式展示出来,使用Markdown以及LaTex Math的结合是最佳的方法。 LaTex Math的语法多且杂,我们是没法完全记住这些语法的,能记住也不提倡吧,我连Markdown语法都没能全记住。查询手册在手,天下我有,这里比较推荐名校莱斯Rice大学的一个语法手册,莱斯大学LaTex Math在线PDF手册

  • LaTeX 送柴侍御(唐 - 王昌龄) 沅水通波接武冈,送君不觉有离伤。 青山一道同云雨,明月何曾是两乡。 Latex中的命令格式: \CommandName[Optional Argument]{Compulsory Argument}或 \CommandName{Compulsory Argument}[Optional Argument]. 环境格式: \begin{environment}

  • LATEX笔记 希腊字母 δ , α , β Δ \delta,\alpha,\beta \\ \Delta δ,α,βΔ 上下标 x 2 x y + 1 y 1 , x i j x^2\\ x^{y+1}\\ y_1,x_{ij} x2xy+1y1​,xij​ 分式与根式 1 2 , 3 4 1 x + 1 y + 1 \frac 1 2,\frac {3} {4}\\ \frac {\dfra

  • Matlab希腊字母Latex输入_数学_自然科学_专业资料。1 I. TEX ... 数据图 gnuplot: matlab: origin: 关于 TeX 和 LaTeX 的插图方面可以关参考...0.000000 font "" set xlabel "X axis" -5.000000,-2.000000 font "...... \\Large f(x)=\\int_{-\\infty}

  • 数学公式的表达 latex2exp latex2exp包实际上是一种将latex语法转变为expression语句的辅助包,当然也可以直接用expression()这个基础函数,只不过直接用的语法不是tex语法,这就很烦。 install.packages('latex2exp') library(latex2exp) 具体使用,例如: plot(1,1,main= TeX('$\\alpha^

  • 如何在latex中使用python及pythontex功能简介 这是我在latexstudio写的文章。 请直接看: 【如何在latex中使用python及pythontex功能简介】