From a4dd449c68b7124cb653d685c841b6cfdceaf4c9 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Thu, 10 Oct 2024 08:39:02 -0500 Subject: [PATCH] fix yihui/litedown#34: allow () around math expressions (outside dollar signs) --- DESCRIPTION | 2 +- NEWS.md | 2 ++ R/markdown.R | 13 +++++++------ man/protect_math.Rd | 9 +++++---- tests/test-cran/test-markdown.R | 7 +++++++ 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3c5f223..938e6eb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: xfun Type: Package Title: Supporting Functions for Packages Maintained by 'Yihui Xie' -Version: 0.48.4 +Version: 0.48.5 Authors@R: c( person("Yihui", "Xie", role = c("aut", "cre", "cph"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")), person("Wush", "Wu", role = "ctb"), diff --git a/NEWS.md b/NEWS.md index 4cf5381..6f93206 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ - `protect_math()` will ignore `$ $` if there are backticks after the opening `$` or before the closing `$`, e.g., ``$`this is not math`$``. +- `protect_math()` allows for parentheses `()` around math expressions now, e.g., `($x$)` was previously not recognized but is recognized now (thanks, @AlbertLei, yihui/litedown#34). + - `html_escape()` will not escape double quotes (i.e., convert `"`" to `"`) by default, and the conversion will be done only for `html_escape(attr = TRUE)`. - Added an argument `start` to `make_fence()`. diff --git a/R/markdown.R b/R/markdown.R index c7c8ac9..b79c305 100644 --- a/R/markdown.R +++ b/R/markdown.R @@ -56,10 +56,11 @@ prose_index = function(x, warn = TRUE) { #' #' 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 +#' ending dollar sign. There should be a space or `(` 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, and the inner math expression should not be wrapped in +#' backticks. 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 @@ -90,7 +91,7 @@ protect_math = function(x, token = '', use_block = FALSE) { escape_math = function(x, token = '', use_block = FALSE) { # replace $x$ with `\(x\)` (protect inline math in ) - m = gregexpr('(?<=^|[\\s])[$](?![ `])[^$]+?(?