latex2exp R Package

repository·main·Indexed 41 days ago

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

An R package that converts LaTeX strings into R plotmath expressions for rendering mathematical formulas and formatted text in base graphics and ggplot2. Features the TeX() function for parsing LaTeX and latex2exp_examples() for viewing supported commands.

Tokens
1.2K
Snippets
6
Records
8
Agent score
2%

What's inside latex2exp

  1. Install latex2exp

    main

    You can install latex2exp from CRAN or the development version from GitHub.

    To install the stable version from CRAN:

    install.packages('latex2exp')

    To install the development version from GitHub using devtools:

    devtools::install_github('stefano-meschiari/latex2exp')
    install.packages('latex2exp')
  2. Use latex2exp in base R graphics

    main

    You can use TeX() for xlab, ylab, main, and legend arguments in base R plotting functions.

    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)
  3. Use latex2exp in ggplot2

    main

    In ggplot2, TeX() can be used within ylab(), ggtitle(), and scale_*_discrete(labels=...).

    Important Note: When using scale_*_discrete to provide labels, ggplot2 requires the labels to be a list of expressions, not a vector of expressions. Use lapply() to wrap the TeX() calls.

    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)) 
    
    print(p)
  4. Preview LaTeX translations using plot()

    main

    To quickly see how a LaTeX string will be rendered as a plotmath expression, pass the TeX() output to the plot() function.

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

    main

    The TeX() function is the core API of the package. It takes a LaTeX string, parses it, and returns the closest R plotmath expression. The resulting expression can be used in axis labels, legends, titles, and annotations for both base R graphics and ggplot2.

    # Using R 4.0+ raw string syntax (recommended)
    TeX(r"(\textbf{Euler's identity} is $e^{i\pi} + 1 = 0$.")
    
    # Using regular strings (requires escaping backslashes)
    TeX("\\textbf{Euler's identity} is $e^{i\\pi} + 1 = 0$.")