All Projects → stefano-meschiari → Latex2exp

stefano-meschiari / Latex2exp

Licence: other
Use LaTeX in R. More LaTeX, less plotmath!

Programming Languages

r
7636 projects

Labels

Projects that are alternatives of or similar to Latex2exp

Huxtable
An R package to create styled tables in multiple output formats, with a friendly, modern interface.
Stars: ✭ 277 (+87.16%)
Mutual labels:  latex, cran
Bookdownplus
The easiest way to use R package bookdown for writing varied types of books and documents
Stars: ✭ 198 (+33.78%)
Mutual labels:  latex, cran
Rblpapi
R package interfacing the Bloomberg API from https://www.bloomberglabs.com/api/
Stars: ✭ 133 (-10.14%)
Mutual labels:  cran
Nuaathesis
LaTeX document class for NUAA, supporting bachelor/master/PH.D thesis in Chinese/English/Japanese. 南航本科、硕士、博士学位论文 LaTeX 模板
Stars: ✭ 142 (-4.05%)
Mutual labels:  latex
Paper
Lightning Network Paper
Stars: ✭ 136 (-8.11%)
Mutual labels:  latex
D3r
d3.js helpers for R
Stars: ✭ 133 (-10.14%)
Mutual labels:  cran
Anytime
Anything to POSIXct or Date Converter
Stars: ✭ 137 (-7.43%)
Mutual labels:  cran
Hedgehog Lab
Run, compile and execute JavaScript for Scientific Computing and Data Visualization TOTALLY TOTALLY TOTALLY in your BROWSER! An open source scientific computing environment for JavaScript TOTALLY in your browser, matrix operations with GPU acceleration, TeX support, data visualization and symbolic computation.
Stars: ✭ 1,797 (+1114.19%)
Mutual labels:  latex
Sciblog
A blog made with django designed like a scientific paper written in Latex.
Stars: ✭ 145 (-2.03%)
Mutual labels:  latex
Magic Latex Buffer
Magical syntax highlighting for LaTeX-mode buffers
Stars: ✭ 135 (-8.78%)
Mutual labels:  latex
Docker Alpine Texlive Ja
Minimal TeX Live image for Japanese based on alpine
Stars: ✭ 140 (-5.41%)
Mutual labels:  latex
Dapper Invoice
A billable-time invoice featuring style over substance
Stars: ✭ 135 (-8.78%)
Mutual labels:  latex
Dvisvgm
A fast DVI, EPS, and PDF to SVG converter
Stars: ✭ 134 (-9.46%)
Mutual labels:  latex
Arules
Mining Association Rules and Frequent Itemsets with R
Stars: ✭ 139 (-6.08%)
Mutual labels:  cran
Pinp
Pinp Is Not PNAS -- Two-Column PDF Template
Stars: ✭ 134 (-9.46%)
Mutual labels:  cran
Ipynb Tex
Include Jupyter notebook cells in TeX documents
Stars: ✭ 143 (-3.38%)
Mutual labels:  latex
R Appveyor
Tools for using R with AppVeyor (https://appveyor.com)
Stars: ✭ 133 (-10.14%)
Mutual labels:  cran
Scatterd3
R scatter plot htmlwidget based on D3.js
Stars: ✭ 135 (-8.78%)
Mutual labels:  cran
Latex Cheatsheet
Template for a compact LaTeX Cheatsheet I made some years ago.
Stars: ✭ 136 (-8.11%)
Mutual labels:  latex
Whatsbook
Create books from WhatsApp group chats with Python and LaTeX
Stars: ✭ 147 (-0.68%)
Mutual labels:  latex

latex2exp

latex2exp is an R package that parses and converts LaTeX math formulas to R’s plotmath expressions. Plotmath expressions are used to enter mathematical formulas and symbols to be rendered as text, axis labels, etc. throughout R’s plotting system. I find plotmath expressions to be quite opaque, while LaTeX is a de-facto standard for mathematical expressions, so this package might be useful to others as well.

Installation

Install this package from CRAN:

install.packages('latex2exp')

You can also install from GitHub using devtools:

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

Usage

library(latex2exp)

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

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

Before R 4.0, it is necessary to escape the backslash within the string literal: 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 ... can contain any character sequence, including \:

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

The return value of TeX() can be used anywhere a plotmath expression is 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 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)

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 parser cannot build a correct plotmath expression, it will stop() and show the invalid plotmath expression built.

Add parameters bold=TRUE or italic=TRUE to make the entire label bold 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 the string representation of the expression (which could be converted into an expression using parse(text=)).

If the output option is equal to ast, it will return the tree built by 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 symbols will 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. Their rendering 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 to make 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 square brackets.

0.3.3 [08/11/2015]

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

0.3.2 [07/28/2015]

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

0.3.1 [07/02/2015]

Fixes bug #2 (white space causes unexpected behaviour). latex2exp should 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 LaTeX proper. Text does not need to be enclosed in \text tags anymore.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].