Debian Codegraph @

man/protect_math.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/markdown.R
3 \name{protect_math}
4 \alias{protect_math}
5 \title{Protect math expressions in pairs of backticks in Markdown}
6 \usage{
7 protect_math(x, token = "", use_block = FALSE)
8 }
9 \arguments{
10 \item{x}{A character vector of text in Markdown.}
11
12 \item{token}{A character string to wrap math expressions at both ends. This
13 can be a unique token so that math expressions can be reliably identified
14 and restored after the Markdown text is converted.}
15
16 \item{use_block}{Whether to use code blocks (\verb{```md-math}) to protect
17 \verb{$$ $$} expressions that span across multiple lines. This is necessary when
18 a certain line in the math expression starts with a special character that
19 can accidentally start a new element (e.g., a leading \code{+} may start a
20 bullet list). Only code blocks can prevent this case.}
21 }
22 \value{
23 A character vector with math expressions in backticks.
24 }
25 \description{
26 For Markdown renderers that do not support LaTeX math, we need to protect
27 math expressions as verbatim code (in a pair of backticks), because some
28 characters in the math expressions may be interpreted as Markdown syntax
29 (e.g., a pair of underscores may make text italic). This function detects
30 math expressions in Markdown (by heuristics), and wrap them in backticks.
31 }
32 \details{
33 Expressions in pairs of dollar signs or double dollar signs are treated as
34 math, if there are no spaces after the starting dollar sign, or before the
35 ending dollar sign. There should be a space or \code{(} before the starting dollar
36 sign, unless the math expression starts from the very beginning of a line.
37 For a pair of single dollar signs, the ending dollar sign should not be
38 followed by a number, and the inner math expression should not be wrapped in
39 backticks. With these assumptions, there should not be too many false
40 positives when detecing math expressions.
41
42 Besides, LaTeX environments (\verb{\begin{*}} and \verb{\end{*}}) are also
43 protected in backticks.
44 }
45 \note{
46 If you are using Pandoc or the \pkg{rmarkdown} package, there is no
47 need to use this function, because Pandoc's Markdown can recognize math
48 expressions.
49 }
50 \examples{
51 library(xfun)
52 protect_math(c("hi $a+b$", "hello $$\\\\alpha$$", "no math here: $x is $10 dollars"))
53 protect_math(c("hi $$", "\\\\begin{equation}", "x + y = z", "\\\\end{equation}"))
54 protect_math("$a+b$", "===")
55 }