File: protect_math.Rd

package info (click to toggle)
r-cran-xfun 0.37%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 792 kB
  • sloc: ansic: 242; sh: 22; makefile: 2
file content (48 lines) | stat: -rw-r--r-- 1,943 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/markdown.R
\name{protect_math}
\alias{protect_math}
\title{Protect math expressions in pairs of backticks in Markdown}
\usage{
protect_math(x, token = "")
}
\arguments{
\item{x}{A character vector of text in Markdown.}

\item{token}{A character string to wrap math expressions at both ends. This
can be a unique token so that math expressions can be reliably identified
and restored after the Markdown text is converted.}
}
\value{
A character vector with math expressions in backticks.
}
\description{
For Markdown renderers that do not support LaTeX math, we need to protect
math expressions as verbatim code (in a pair of backticks), because some
characters in the math expressions may be interpreted as Markdown syntax
(e.g., a pair of underscores may make text italic). This function detects
math expressions in Markdown (by heuristics), and wrap them in backticks.
}
\details{
Expressions in pairs of dollar signs or double dollar signs are treated as
math, if there are no spaces after the starting dollar sign, or before the
ending dollar sign. There should be spaces before the starting dollar sign,
unless the math expression starts from the very beginning of a line. For a
pair of single dollar signs, the ending dollar sign should not be followed by
a number. With these assumptions, there should not be too many false
positives when detecing math expressions.

Besides, LaTeX environments (\verb{\begin{*}} and \verb{\end{*}}) are also
protected in backticks.
}
\note{
If you are using Pandoc or the \pkg{rmarkdown} package, there is no
  need to use this function, because Pandoc's Markdown can recognize math
  expressions.
}
\examples{
library(xfun)
protect_math(c("hi $a+b$", "hello $$\\\\alpha$$", "no math here: $x is $10 dollars"))
protect_math(c("hi $$", "\\\\begin{equation}", "x + y = z", "\\\\end{equation}"))
protect_math("$a+b$", "===")
}