From 2d4a3ecf2b322840e7fbce01786a79abb2d00453 Mon Sep 17 00:00:00 2001 From: Kucharssim Date: Tue, 23 May 2023 11:57:33 +0200 Subject: [PATCH] add mathExpression --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/mathExpression.R | 28 ++++++++++++++++++++++++++++ man/mathExpression.Rd | 26 ++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 R/mathExpression.R create mode 100644 man/mathExpression.Rd diff --git a/DESCRIPTION b/DESCRIPTION index bb24361b..f19b843f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -34,7 +34,7 @@ Imports: svglite, systemfonts, withr -RoxygenNote: 7.2.2 +RoxygenNote: 7.2.3 Roxygen: list(markdown = TRUE) Encoding: UTF-8 LinkingTo: Rcpp diff --git a/NAMESPACE b/NAMESPACE index 9670fc8d..fbf1c8de 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -103,6 +103,7 @@ export(jaspResultsCalledFromJasp) export(logNormDist) export(logit) export(makeJaspFormula) +export(mathExpression) export(negBinomDist) export(normalDist) export(poisDist) diff --git a/R/mathExpression.R b/R/mathExpression.R new file mode 100644 index 00000000..a28a1666 --- /dev/null +++ b/R/mathExpression.R @@ -0,0 +1,28 @@ +#' LaTeX Expressions in JASP +#' +#' @description Construct LaTeX expressions for JASP output +#' +#' @param x A string that contains the LaTeX code for the math expression +#' @param inline Should the expression be inline or in display mode? +#' +#' @examples +#' # Escape the backslash character for LaTeX code +#' jaspBase::mathExpression("\\alpha + \\beta") +#' # Alternatively use the raw character constant +#' jaspBase::mathExpression(r"{\alpha + \beta}") +#' +#' # By default expressions are inline, to disable use +#' jaspBase::mathExpression(r"{ \bar{x} = \frac{\sum_i^n x_i}{n} }", inline = FALSE) +#' +#' @export +mathExpression <- function(x, inline = TRUE) { + stopifnot(is.character(x)) + + if (!inline) { + output <- sprintf("\\[%s\\]", x) + } else { + output <- sprintf("\\(%s\\)", x) + } + + return(output) +} diff --git a/man/mathExpression.Rd b/man/mathExpression.Rd new file mode 100644 index 00000000..6dea7055 --- /dev/null +++ b/man/mathExpression.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/mathExpression.R +\name{mathExpression} +\alias{mathExpression} +\title{LaTeX Expressions in JASP} +\usage{ +mathExpression(x, inline = TRUE) +} +\arguments{ +\item{x}{A string that contains the LaTeX code for the math expression} + +\item{inline}{Should the expression be inline or in display mode?} +} +\description{ +Construct LaTeX expressions for JASP output +} +\examples{ +# Escape the backslash character for LaTeX code +jaspBase::mathExpression("\\\\alpha + \\\\beta") +# Alternatively use the raw character constant +jaspBase::mathExpression(r"{\alpha + \beta}") + +# By default expressions are inline, to disable use +jaspBase::mathExpression(r"{ \bar{x} = \frac{\sum_i^n x_i}{n} }", inline = FALSE) + +}