Home > other >  Wrap Latex Equation using custom tag PHP regex
Wrap Latex Equation using custom tag PHP regex

Time:10-04

I just want to wrap the Math latex equation using any html tag. The string be like

y = x2  or \(x = \sqrt{y}\) . For all \(y \geq 0\), x is defined. Thus, y is set of all non-negative and real number. Hence, range is \(y \geq 0\) or [0, ∞).

I want output like

y = x2  or <math>\(x = \sqrt{y}\)</math> . For all <math>\(y \geq 0\)</math>, x is defined. Thus, y is set of all non-negative and real number. Hence, range is <math>\(y \geq 0\)</math> or [0, ∞).

CodePudding user response:

The pattern for equation seems like this:

((\\\()((?!(\\\()|(\\\))).)*(\\\)))

The prefix (\\\() and suffix (\\\)) are simple check for start and end of the equation.

The middle part ((?!(\\\()|(\\\))).)* count any character (.) (except \n) unless by looking ahead "(?! ...)" the two character in front be one of \( or \).

The whole equation pattern are in (...), so it could be referred to as group1 or $1. So that should be substituted by:

<math>$1</math>

Link to test for PHP PCRE2

  • Related