polarwinkel / mdtex2html

python3-library to convert Markdown with included LaTeX-Formulas to HTML with MathML
GNU Lesser General Public License v2.1
41 stars 5 forks source link

Higher order derivatives cause `LaTeX-Convert-Error` #2

Open nxfi777 opened 6 months ago

nxfi777 commented 6 months ago

Lagrange Derivative Notation Conversion Error

Example Code

import mdtex2html

responseHTML=r"""The Taylor series of a function \(f(x)\) at a point \(a\) is given by the formula:

\[ f(x) = f(a) + \frac{f'(a)}{1!}(x - a) + \frac{f''(a)}{2!}(x - a)^2 + \frac{f'''(a)}{3!}(x - a)^3 + ... \]

where \(f'(a)\), \(f''(a)\), \(f'''(a)\), etc., represent the first, second, third, etc., derivatives of \(f(x)\) evaluated at \(x = a\), and \(n!\) denotes the factorial of \(n\). This series continues infinitely, but in practice, it is often truncated to a finite number of terms to approximate \(f(x)\) near the point \(a\).
"""

print(mdtex2html.convert(responseHTML))

Here, the {f'''(a)}, and \(f'''(a)\) terms would cause the error. This applies to all cases, not just functions denoted by f

polarwinkel commented 6 months ago

Hi nxfi777, thanks for the bug-report, I can confirm that three tick marks ''' cause the error, I'll have a deeper look into this in the next days. But there is an (ugly) workaround for now: You can prepend a non-breaking space ~ to the tick marks, so \[f(x)~'~'~'=...\] will render your formula (putting ugly spaces between the tick marks).

I'll come back to this soon when I have a little more time for debugging!

nxfi777 commented 6 months ago

Hi nxfi777, thanks for the bug-report, I can confirm that three tick marks ''' cause the error, I'll have a deeper look into this in the next days. But there is an (ugly) workaround for now: You can prepend a non-breaking space ~ to the tick marks, so \[f(x)~'~'~'=...\] will render your formula (putting ugly spaces between the tick marks).

I'll come back to this soon when I have a little more time for debugging!

Current solution:

def replace_prime_notation(md_input):
                    def replace_with_power(match):
                        character = match.group(1)
                        primes = match.group(2)
                        power = len(primes)  # Count of prime symbols
                        # Only replace if there are 3 or more prime symbols
                        if power >= 3:
                            return f"{character}^{{({power})}}"
                        else:
                            # Return the original match if the condition is not met
                            return match.group(0)

                    # Corrected regular expression to match a character followed by three or more prime symbols (')
                    pattern = r"(\w)('{3,})"

                    md_input= re.sub(pattern, replace_with_power, md_input)

                    return md_input

Thanks for the update. I'm looking forward to the fix.

polarwinkel commented 6 months ago

It looks like it is a bug in the latex2mathml-library which I reported now, but I'll leave this issue open until it is confirmed there.