/** * Creates a comprehensive, reusable prompt section for ensuring strict MathML compliance. * This module serves as the single source of truth for all MathML-related rules and examples. */ export function createMathmlComplianceSection(): string { return ` ## 📐 CRITICAL MathML COMPLIANCE RULES (ZERO TOLERANCE) You MUST adhere to the following rules for all mathematical and chemical notation. Any deviation will result in rejection. ### Banned Patterns (DO NOT OUTPUT) - **NO LaTeX:** Absolutely no LaTeX commands (e.g., \`\\frac\`, \`\\sqrt\`) or dollar-sign delimiters (\`$...$\`). - **NO Raw Currency/Percent in Text:** The dollar sign \`$\` and percent sign \`%\` are banned from all plain text. They MUST be rendered using MathML. - **NO \`\` Element:** The \`\` MathML element is deprecated and BANNED. Use \`\` with explicit parenthesis characters like \`(\` and \`)\` instead. - **NO Outer \`\` Wrapper:** All MathML must be **inner MathML** suitable for embedding inside a parent \`\` tag. Do not include your own \`\` or \`\` wrapper in the \`mathml\` string. ### Correct Usage - All mathematical notation MUST be placed inside a \`{ "type": "math", "mathml": "..." }\` inline item. - Use the examples below as a definitive guide for correct formatting. --- ### ✅ GOOD vs. ❌ BAD Examples #### 1. Fractions and Ratios // BAD (text): "4/9", "a/b", "a:b" // GOOD (math): { "type": "math", "mathml": "49" } { "type": "math", "mathml": "ab" } { "type": "math", "mathml": "a:b" } #### 2. Cross Multiplication // BAD (text): "9 × a = 4 × b" // GOOD (math): { "type": "math", "mathml": "9×a=4×b" } #### 3. Percent and Currency // BAD (text): "75%", "$5", "$1,234.56", "-$5", "5%–10%" // GOOD (math): { "type": "math", "mathml": "75%" } { "type": "math", "mathml": "$5" } { "type": "math", "mathml": "$1,234.56" } { "type": "math", "mathml": "−$5" } // Currency examples (comprehensive): // ❌ WRONG: { "type": "text", "content": "The cost is $5.50." } // ✅ CORRECT: [ { "type": "text", "content": "The cost is " }, { "type": "math", "mathml": "$5.50" }, { "type": "text", "content": "." } ] // ❌ WRONG: { "type": "text", "content": "The discount is 25%!" } // ✅ CORRECT: [ { "type": "text", "content": "The discount is " }, { "type": "math", "mathml": "25%" }, { "type": "text", "content": "!" } ] // ❌ WRONG: "You paid $1,234.56 for it" // ✅ CORRECT: [ { "type": "text", "content": "You paid " }, { "type": "math", "mathml": "$1,234.56" }, { "type": "text", "content": " for it" } ] // ❌ WRONG: "Your balance is -$5" // ✅ CORRECT: [ { "type": "text", "content": "Your balance is " }, { "type": "math", "mathml": "−$5" } ] // ❌ WRONG: "A discount of 5%–10%" // ✅ CORRECT: [ { "type": "text", "content": "A discount of " }, { "type": "math", "mathml": "5%" }, { "type": "text", "content": "–" }, { "type": "math", "mathml": "10%" } ] **CRITICAL**: Before finalizing, SCAN ALL text nodes for "$" or "%" and rewrite by splitting the text. #### 4. Expressions and Functions // BAD (text): "Area = Length × Width", "f(x)" // GOOD (math): { "type": "math", "mathml": "Area=Length×Width" } { "type": "math", "mathml": "f(x)" } #### 5. Chemistry (Subscripts, Charges, Reactions, Phase Labels) // BAD (text): "NH4Cl", "Ca2+", "2H2 + O2 -> 2H2O", "NaCl(aq)" // GOOD (math): { "type": "math", "mathml": "NH4Cl" } { "type": "math", "mathml": "Ca2+" } { "type": "math", "mathml": "2H2+O2→2H2O" } // Phase labels (aqueous, solid, liquid, gas): { "type": "math", "mathml": "NaCl(aq)" } { "type": "math", "mathml": "H2O(l)" } // Equilibrium reactions: { "type": "math", "mathml": "N2+3H2⇌2NH3" } Chemistry notation details: - Subscripts: Use for element counts (H₂ → H2) - Superscripts: Use for ion charges (Ca²⁺ → Ca2+) - Coefficients: Use for reaction coefficients (2H₂ → 2H2) - Arrows: → for irreversible, ⇌ for equilibrium - Phase labels: Wrap in with parenthesis tokens: (aq) - BANNED: Plain text formulas, HTML /, ASCII arrows like "->" #### 6. Exponents and Roots // BAD (text): "x^2", "sqrt(x)", "cube root of 8", "nth root" // GOOD (math): { "type": "math", "mathml": "x2" } { "type": "math", "mathml": "x" } { "type": "math", "mathml": "83" } { "type": "math", "mathml": "xn" } #### 7. Linear Equations and Inequalities // BAD (text): "2x + 3 = 7", "x <= 5", "x < 10", "y >= 2", "y != 2" // GOOD (math): { "type": "math", "mathml": "2x+3=7" } { "type": "math", "mathml": "x≤5" } { "type": "math", "mathml": "x<10" } { "type": "math", "mathml": "y≥2" } { "type": "math", "mathml": "y≠2" } Note: Use HTML entities for < and > inside MathML: < and > #### 8. Absolute Value & Parentheses (no ) // BAD (text): "|x|", "(a+b)" // GOOD (math): { "type": "math", "mathml": "|x|" } { "type": "math", "mathml": "(a+b)" } #### 9. Mixed Numbers and Ratios // BAD (text): "3 1/2", "a:b = c:d" // GOOD (math): { "type": "math", "mathml": "312" } { "type": "math", "mathml": "a:b=c:d" } #### 10. Scientific Notation // BAD (text): "3.2 x 10^5" // GOOD (math): { "type": "math", "mathml": "3.2×105" } #### 11. Trigonometry, Degrees, and Angles // BAD (text): "sin(pi/2) = 1", "90°", "∠ABC" // GOOD (math): { "type": "math", "mathml": "sin(π2)=1" } { "type": "math", "mathml": "90°" } { "type": "math", "mathml": "∠ABC" } #### 12. Logs, Summations, and Matrices // BAD (text): "log_2 8", "sum_{i=1 to n} i", "[[1,2],[3,4]]" // GOOD (math): { "type": "math", "mathml": "log28" } { "type": "math", "mathml": "∑i=1ni" } { "type": "math", "mathml": "[1234]" } #### 13. Units and Probability // BAD (text): "cm^2", "P(A|B)" // GOOD (math): { "type": "math", "mathml": "cm2" } { "type": "math", "mathml": "P(A|B)" } #### 14. Polynomial Factors // BAD (text): "(x+3)(x-2)" // GOOD (math): { "type": "math", "mathml": "(x+3)(x−2)" } #### 15. Mixed Text and Math // BAD (text only): "Compute 3/4 of 20." // GOOD (mixed content array): [ { "type": "text", "content": "Compute " }, { "type": "math", "mathml": "34" }, { "type": "text", "content": " of " }, { "type": "math", "mathml": "20" }, { "type": "text", "content": "." } ] #### 16. Spacing Between Text and Math (Units and Adjacent Math) // ❌ BAD: Missing spaces around units yields hard-to-read output like "4km20m" [ { "type": "text", "content": "Keira walks " }, { "type": "math", "mathml": "4" }, { "type": "text", "content": "km" }, { "type": "math", "mathml": "20" }, { "type": "text", "content": "m during those three days." } ] // ✅ GOOD: Insert spaces in surrounding text (preferred) [ { "type": "text", "content": "Keira walks " }, { "type": "math", "mathml": "4" }, { "type": "text", "content": " km " }, { "type": "math", "mathml": "20" }, { "type": "text", "content": " m during those three days." } ] // ❌ BAD: Number glued to unit, e.g., "12miles" [ { "type": "text", "content": "The total distance is " }, { "type": "math", "mathml": "12" }, { "type": "text", "content": "miles." } ] // ✅ GOOD: Ensure a space before the unit text [ { "type": "text", "content": "The total distance is " }, { "type": "math", "mathml": "12" }, { "type": "text", "content": " miles." } ] // ❌ BAD: Missing space after colon before math ("Use the area formula:") [ { "type": "text", "content": "Use the area formula:" }, { "type": "math", "mathml": "A=l×w" } ] // ✅ GOOD: Add a space after the colon before math [ { "type": "text", "content": "Use the area formula: " }, { "type": "math", "mathml": "A=l×w" } ] // ❌ BAD: Missing space after colon before math ("Substitute the length and your width from Step 1:") [ { "type": "text", "content": "Substitute the length and your width from Step 1:" }, { "type": "math", "mathml": "A=23×w" } ] // ✅ GOOD: Add a space after the colon before math [ { "type": "text", "content": "Substitute the length and your width from Step 1: " }, { "type": "math", "mathml": "A=23×w" } ] // ❌ BAD: Missing trailing space before math at phrase boundary ("by checking") [ { "type": "text", "content": "Verify that the dimensions satisfy the given perimeter by checking" }, { "type": "math", "mathml": "2(l+w)=80" }, { "type": "text", "content": "." } ] // ✅ GOOD: Include a trailing space before math [ { "type": "text", "content": "Verify that the dimensions satisfy the given perimeter by checking " }, { "type": "math", "mathml": "2(l+w)=80" }, { "type": "text", "content": "." } ] // ❌ BAD: Missing leading space in text after math [ { "type": "math", "mathml": "A=23×w" }, { "type": "text", "content": "Evaluate this expression to confirm it matches the value you selected, in square feet." } ] // ✅ GOOD: Start the following text with a leading space [ { "type": "math", "mathml": "A=23×w" }, { "type": "text", "content": " Evaluate this expression to confirm it matches the value you selected, in square feet." } ] // ❌ BAD: Missing space before math after "to write" [ { "type": "text", "content": "to write" }, { "type": "math", "mathml": "w=802−23" } ] // ✅ GOOD: Include a trailing space before math after "to write" [ { "type": "text", "content": "to write " }, { "type": "math", "mathml": "w=802−23" } ] **MANDATORY RULE:** Whenever a text item is adjacent to a math item, include necessary leading/trailing spaces in the text items so the rendered sentence has natural spacing. Do NOT rely on the renderer to insert spaces. --- ### Pattern Recognition Checklist Before finalizing your output, SCAN ALL text nodes for these patterns and convert them to MathML: - **Dollar before number**: \`/$(?=\\s*\\d)/\` → Split text and insert \`$amount\` - **Number before percent**: \`/\\d\\s*%/\` → Split text and insert \`number%\` - **Fraction slash**: \`/\\d+\\/\\d+/\` or \`/[a-z]\\/[a-z]/i\` → Use \`\` - **Exponent caret**: \`/\\^/\` → Use \`\` - **Scientific notation**: \`/x\\s*10\\^/\` → Use \`×10exp\` - **Chemistry subscripts**: \`/H2|O2|CO2|NH4/\` etc. → Use \`\` - **Chemistry superscripts**: \`/Ca2\\+|Fe3\\+/\` etc. → Use \`\` - **Arrows**: \`/->/\` or \`/→/\` in reactions → Use \`→\` inside math inline item - **Angle symbols**: \`/∠/\` → Use \`∠\` inside math inline item - **Degree symbols**: \`/°/\` → Use \`°\` inside math inline item - **Absolute value bars**: \`/\\|[^|]+\\|/\` → Use \`|...|\` inside math inline item **ZERO TOLERANCE**: If ANY of these patterns appear in plain text nodes, your output will be REJECTED. ` }