GithubHelp home page GithubHelp logo

roniemartinez / latex2mathml Goto Github PK

View Code? Open in Web Editor NEW
173.0 7.0 23.0 1.54 MB

Pure Python library for LaTeX to MathML conversion

License: MIT License

Python 99.77% Makefile 0.23%
mathml latex python3 python

latex2mathml's Introduction

I am a software developer from the Philippines.

Philippines πŸ‡΅πŸ‡­ ➑️ Germany πŸ‡©πŸ‡ͺ ➑️ United Kingdom πŸ‡¬πŸ‡§

I am now in the UK. πŸ‡¬πŸ‡§

So what do I do in the world of software engineering?

Sponsors keep me motivated in writing, maintaining projects, help open source and building new things.

Buy Me A Coffee

Use my DigitalOcean referral below.

DigitalOcean Referral Badge

latex2mathml's People

Contributors

06180339 avatar allcontributors[bot] avatar askender avatar dependabot[bot] avatar felixonmars avatar mcsinyx avatar miakramer avatar rob-blackbourn avatar roniemartinez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

latex2mathml's Issues

Convert from example result in IndexError: list index out of range

Hello there, I have a file (math.txt) with the following content (from examples).

 \begin{bmatrix}
  a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\
  a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\
  \vdots  & \vdots  & \ddots & \vdots  \\
  a_{m,1} & a_{m,2} & \cdots & a_{m,n} 
 \end{bmatrix}

The code below is trying to parse and convert the above but fail with IndexError: list index out ouf range

#! /usr/bin/python3

import latex2mathml.converter

latex_input = open("math.txt","r").read().strip()
mathml_output = latex2mathml.converter.convert(latex_input)
print(mathml_output)

Is there something I'm doing the wrong way? I'm using version 2.5.2

Crash when converting systems of equations

Hi, I've stumbled upon a crash when trying to convert systems of equations. For example, when I try to convert this formula:
\left\{ \begin{array} { l } { 3x - 5y + 4z = 0} \\ { x - y + 8z = 0} \\ { 2x - 6y + z = 0} \end{array} \right.

I got the following error in my log:

File "/usr/local/lib/python2.7/dist-packages/latex2mathml/converter.py", line 20, in convert
_classify_subgroup(aggregate(latex), row)
File "/usr/local/lib/python2.7/dist-packages/latex2mathml/converter.py", line 95, in _classify_subgroup
_classify_subgroup(element, _row)
File "/usr/local/lib/python2.7/dist-packages/latex2mathml/converter.py", line 97, in _classify_subgroup
_convert_command(element, elements, i, iterable, row)
File "/usr/local/lib/python2.7/dist-packages/latex2mathml/converter.py", line 118, in _convert_command
_convert_array_content(param, new_parent, alignment)
File "/usr/local/lib/python2.7/dist-packages/latex2mathml/converter.py", line 70, in _convert_array_content
__alignment = _alignment[index]
IndexError: list index out of range

Please see if you can fix this. Thanks for the library!
Ps: I'm using latest version 2.0.2.

'{' can't convert

r"\left{x y\right}" ->

<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mrow><mo fence="true" form="prefix" stretchy="true">\{x</mo><mrow><mi>y</mi></mrow><mo fence="true" form="postfix" stretchy="true">&#x0007D;</mo></mrow></mrow></math>

You can try this : r"\left{ x y\right}", but they are equivalent.

Problem with max operator

I think that conversion of \max operator is little buggy. For example, if I type

latex2mathml.converter.convert("\\max f")

I get

<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline">
  <mrow><mo>max</mo></mrow>
</math>

which is obviously wrong. Another example:

latex2mathml.converter.convert("\\max \\{a, b, c\\}")
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline">
  <mrow>
    <munder><mo>max</mo><mi>,</mi></munder>
    <mi>b</mi><mi>,</mi><mi>c</mi><mi>&#x0007D;</mi>
  </mrow>
</math>

Operators \min, \sup and \inf have same problem.

Trigonometric functions ?

I always get a backward slash when converting trigonometric function, for example "\cos" converts to "\cos".

Is this the expected behavior ? Are trigonometric functions not implemented ?

If not I suppose I can write a routine to clean this up

Converting Formula returns weird values

Dear @Code-ReaQtor,

When I try to parse a relatively straightforward formula:

$F_{x} = m \cdot a$

from latex2mathml.converter import convert
convert("$F_{x} = m \cdot a$")

the following is returned:

b'<math>
    <mrow>
        <mi>$</mi>
        <msub>
            <mi>F</mi>
            <mrow>
                <mi>x</mi>
            </mrow>
        </msub>
        <mo>&amp;#x0003D;</mo>
        <mi>m</mi>
        <mo>&amp;#x022C5;</mo>
        <mi>a</mi>
        <mi>$</mi>
    </mrow>
</math>'

Why are \cdot and the '=' converted to '&#x0003D;' and '&#x022C5;' ??

I'm using python 3.6.4 and running the code in jupyter notebook.

thanks in advance

MathML output contains backslashes

When using latex2mathml

$ latex2mathml --version
latex2mathml 2.13.0

I'm finding that with inputs like this:

$ latex2mathml -t '\frac{\tan^{2}x}{1+\sec x}'

latex2mathml is producing outputs like this:

<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline">
	<mrow>
		<mfrac>
			<mrow>
				<msup>
					<mi>\tan</mi>
					<mrow>
						<mn>2</mn>
					</mrow>
				</msup>
				<mi>x</mi>
			</mrow>
			<mrow>
				<mn>1</mn>
				<mo>&#x0002B;</mo>
				<mi>\sec</mi>
				<mi>x</mi>
			</mrow>
		</mfrac>
	</mrow>
</math>

Now, as you can see, the expressions with trigonometric functions contain backslashes in them. \tan should just be tan. and \sec should just be sec. This happens for every trigonometric operator I've tried, but I can't speak to other operators.

To mitigate this issue, I wrote a script that just pipes latex2mathml's output through sed like this:

#!/usr/bin/env sh
latex2mathml -t "$1" | sed 's|\\||g'

But that's not really the ideal solution.

Documentation

Hey, thanks for this great project. I'm adding it as a dependency to https://github.com/StevenClontz/checkit-platform to convert authors' LaTeX mathematics into MathML, which seems to be required to export question banks compatible with the Brightspace LMS.

Your conversion works pretty well, but isn't compatible with a few non-semantic commands I've been using for presentation purposes (e.g. an \hspace{1em} to pad out an ODE from its initial values, \big modifiers on parentheses because I thought it looked better in an exercise).

image

I need to document my platform for a few authors who will adopt the platform this summer, which should include a style guide for mathematical notation. I think the right thing to do is to tell authors to keep their LaTeX relatively simple to make sure the MathML conversion will handle things elegantly. Does this library have any documentation on what subset of LaTeX it aims to support?

\frac not working

\frac is not being recognized,
The version of latex2mathml is 2.13.0
The output

> Python 3.7.5 (default, Nov  7 2019, 10:50:52) 
> [GCC 8.3.0] on linux
>  l = "\frac{3}{2}"
>  o = latex2mathml.converter.convert(l)
>  o
> `<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>r</mi><mi>a</mi><mi>c</mi><mrow><mn>3</mn></mrow><mrow><mn>2</mn></mrow></mrow></math>
> `

'\substack' Can't Convert

When convert r"\frac{d}{d x} g{\left(x \right)} \left. \frac{\partial}{\partial \xi_{2}} g{\left(x,\xi_{2} \right)} \right|{\substack{ \xi{2}=g{\left(x \right)} }} + \left. \frac{\partial}{\partial \xi_{1}} g{\left(\xi_{1},g{\left(x \right)} \right)} \right|{\substack{ \xi{1}=x }}", r"\substack" was not correct.

"""<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mfrac><mrow><mi>d</mi></mrow><mrow><mi>d</mi><mi>x</mi></mrow></mfrac><mi>g</mi><mrow><mrow><mo fence="true" form="prefix" stretchy="true">&#x00028;</mo><mrow><mi>x</mi></mrow><mo fence="true" form="postfix" stretchy="true">&#x00029;</mo></mrow></mrow><mrow><mo fence="true" form="prefix" stretchy="true" /><mrow><mfrac><mrow><mo>&#x02202;</mo></mrow><mrow><mo>&#x02202;</mo><msub><mi>&#x003BE;</mi><mrow><mn>2</mn></mrow></msub></mrow></mfrac><mi>g</mi><mrow><mrow><mo fence="true" form="prefix" stretchy="true">&#x00028;</mo><mrow><mi>x</mi><mi>,</mi><msub><mi>&#x003BE;</mi><mrow><mn>2</mn></mrow></msub></mrow><mo fence="true" form="postfix" stretchy="true">&#x00029;</mo></mrow></mrow></mrow><mo fence="true" form="postfix" stretchy="true">&#x0007C;</mo></mrow><mrow><mi>substack</mi><mrow><msub><mi>&#x003BE;</mi><mrow><mn>2</mn></mrow></msub><mo>&#x0003D;</mo><mi>g</mi><mrow><mrow><mo fence="true" form="prefix" stretchy="true">&#x00028;</mo><mrow><mi>x</mi></mrow><mo fence="true" form="postfix" stretchy="true">&#x00029;</mo></mrow></mrow></mrow></mrow><mo>&#x0002B;</mo><mrow><mo fence="true" form="prefix" stretchy="true" /><mrow><mfrac><mrow><mo>&#x02202;</mo></mrow><mrow><mo>&#x02202;</mo><msub><mi>&#x003BE;</mi><mrow><mn>1</mn></mrow></msub></mrow></mfrac><mi>g</mi><mrow><mrow><mo fence="true" form="prefix" stretchy="true">&#x00028;</mo><mrow><msub><mi>&#x003BE;</mi><mrow><mn>1</mn></mrow></msub><mi>,</mi><mi>g</mi><mrow><mrow><mo fence="true" form="prefix" stretchy="true">&#x00028;</mo><mrow><mi>x</mi></mrow><mo fence="true" form="postfix" stretchy="true">&#x00029;</mo></mrow></mrow></mrow><mo fence="true" form="postfix" stretchy="true">&#x00029;</mo></mrow></mrow></mrow><mo fence="true" form="postfix" stretchy="true">&#x0007C;</mo></mrow><mrow><mi>substack</mi><mrow><msub><mi>&#x003BE;</mi><mrow><mn>1</mn></mrow></msub><mo>&#x0003D;</mo><mi>x</mi></mrow></mrow></mrow></math>"""

'(' missing

image
After convert:
image
And the '()' is not what I want.

$x^{x^{x^{x}}} \left(x^{x^{x}} \left(x^{x} \left(\log{\left(x \right)} + 1\right) \log{\left(x \right)} + \frac{x^{x}}{x}\right) \log{\left(x \right)} + \frac{x^{x^{x}}}{x}\right)$

IndexError on r'\sqrt { ( - 25 ) ^ { 2 } } = \pm 25'

Received bug report thru email:

Hi, Ronie Martinez

Thanks for your project latextomathml. It's a great project, easy to use and powerful. But I find some bugs, here I input the latex:
latex = r'\sqrt { ( - 25 ) ^ { 2 } } = \pm 25'

Here is the output ~

Hope it help ~
Best wishes,
Zhilun

On another email:

I found if I use latex = r'\sqrt { - 25 ^ { 2 } } = \pm 25', without this (), it works.
But, maybe the latex is \sqrt { (x - 25) ^ { 2 } } = \pm 25. It crashed ~

\overrightarrow {a} conversion failed

\overrightarrow {a}=(\sqrt{3},-1)

to

 <math display="inline" xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mi>\overrightarrow</mi><mrow><mi>a</mi></mrow><mo>&#x0003D;</mo><mo stretchy="false">&#x00028;</mo><msqrt><mrow><mn>3</mn></mrow></msqrt><mi>,</mi><mo>&#x02212;</mo><mn>1</mn><mo stretchy="false">&#x00029;</mo></mrow></math>
\overrightarrow can not convert 

\vec is not translated correctly

The vector-arrow above a letter created with \vec in a formula is shown in front of the sign, not above.

When converting for example
/vec{E}
I get
<mrow><mi>&#x020D1;</mi><mi>E</mi>
as output, the expected behaviour is to get
<mover><mi>E</mi><mo stretchy="true">&#x020D1;</mo></mover>

\dot{A} renders the dot before, not above the A

\dot{A} is often used for a time derivation, putting a dot above the symbol.

Expected behaviour: \dot{A} and \dot A to <mrow><mover><mi>A</mi><mi>Μ‡</mi></mover></mrow>

Actual behaviour: Both \dot{A} and \dot A is translated to <mrow><mi>Μ‡</mi><mi>A</mi></mrow>, the dot is placed before the symbol, not above.

This might be related or similar to the solved #103.

'[]' not correct

r"\left[\begin{matrix}1 & 0 & 0 & 0\0 & 1 & 0 & 0\0 & 0 & 1 & 0\0 & 0 & 0 & 1\end{matrix}\right]"
It looks like this: image, not this:
image

PS: Edit: '$' is not included.

list index out of range

Input:

(\frac{1+x^{2}}{1+y^{2}})^{t}\leq2^{|t|}(1+(x+y)^{2})^{|t|}
(\sin(x)^{2}+3\cos(x)^{2})^{4}
(\frac{1+x^{2}}{1+y^{2}})\leq2^{|t|}(1+(x-y)^{2})^{|t|}

Error:

Traceback (most recent call last):
  File "/Users/<path>/latex2mathml/example.py", line 23, in <module>
    print(convert_to_mathml(string))
  File "/Users/<path>/latex2mathml/example.py", line 13, in convert_to_mathml
    mathml_output = convert(latex_input)
  File "/Users/<path>/latex2mathml/latex2mathml/converter.py", line 26, in convert
    _classify_subgroup(aggregate(latex), row)
  File "/Users/<path>/latex2mathml/latex2mathml/converter.py", line 137, in _classify_subgroup
    _convert_command(element, elements, i, iterable, row)
  File "/Users/<path>/latex2mathml/latex2mathml/converter.py", line 166, in _convert_command
    param = elements[index]
IndexError: list index out of range

Something wrong about the '\mathrm{}'

Why must there be a space between the '\mathrm' and '{}'.
I test it that '\mathrm {}' can be converted correctly but not the '\mathrm{}'.

image

l2m version: 2.14.0
Python version: 3.7.6
Operating System: Windows 10

gaint error

latex_input = '\frac{1}{2}'
mathml_output = latex2mathml.converter.convert(latex_input)
mathml_output
'rac12'

IndexError: list index out of range

When convert r"\frac{x + 4}{x + \frac{123 \left(\sqrt{x} + 5\right)}{x + 4} - 8}" an error was thrown:

Traceback (most recent call last):
File "", line 1, in
File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\latex2mathml\converter.py", line 20, in convert
_classify_subgroup(aggregate(latex), row)
File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\latex2mathml\converter.py", line 108, in _classify_subgroup
_convert_command(element, elements, i, iterable, row)
File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\latex2mathml\converter.py", line 138, in _convert_command
_classify_subgroup(param, _parent)
File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\latex2mathml\converter.py", line 108, in _classify_subgroup
_convert_command(element, elements, i, iterable, row)
File "C:\Users\PC\AppData\Local\Programs\Python\Python37-32\lib\site-packages\latex2mathml\converter.py", line 124, in _convert_command
param = elements[index]
IndexError: list index out of range

Why does it can't convert?

Support for `\newcommand` substitution

Kudos to you for this project, thanks!

I played around with it to get a basic example working, but when I want to use it with existing LaTeX then I often have the case that the LaTeX contains commands that have been defined in the preamble or a .sty file with \newcommand{\alias}{\somemath}. This could be solved in the calling code such that latex2mathml only sees the substituted strings, but the substitution then has to happen manually, e.g. via a Python dict that represents a math_commands.sty with the command mappings and ad-hoc string (regexp?) substitution code.

Any idea how this might be supported within latex2mathml? Or do you consider this out-of-scope?

err in trans ^

\frac{3}{\frac{1}{2}{x}^{2}-\frac{3\sqrt[]{3}}{2}x+3}

\bar doesn't work as expected

Hello there, I'm using the version 2.6.7

I try to use \bar to write complex number related equation.

\bar{z_1} = z_2

should produce something like

<math>
<mrow>
    <mover accent="true">
        <mrow>
            <msub>
                <mi>z</mi>
                <mn>1</mn>
            </msub>
        </mrow>
        <mo>&macr;</mo>
    </mover>
    <mo>=</mo>
    <msub>
        <mi>z</mi>
        <mn>2</mn>
    </msub>
</mrow>
</math>

instead I got

<math>
   <mrow>
       <mi>&#x00304;</mi>
       <mrow>
            <msub>
                <mi>z</mi>
                <mn>1</mn>
            </msub>
       </mrow>
       <mo>&#x0003D;</mo>
       <msub>
           <mi>z</mi>
           <mn>2</mn>
       </msub>
   </mrow>
</math>

Is there something I'm doing wrong?

Do you know how to get the content mathml?

this package is uesful for getting the presentation mathml! But anyone know how to convert latex or presetational mathml into content mathml in Python?? Any help will be Aprreciated!!

Redundant symbol "}"

converter.convert(r"\sqrt {\sqrt {\left( x^{3}\right) + v}}")
return:
<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><msqrt><mrow><msqrt><mrow><mrow><mo fence="true" form="prefix" stretchy="true">&#x00028;</mo><mrow><msup><mi>x</mi><mrow><mn>3</mn></mrow></msup></mrow><mo fence="true" form="postfix" stretchy="true">&#x00029;</mo></mrow></mrow></msqrt><mo>&#x0002B;</mo><mi>v</mi></mrow></msqrt><mi>}</mi></mrow></math>

'}' is retained

[Feature Request] \over

Here is an email that I received:

Hello Ronie,
I have tried latex2mathml v2.0.2 to convert following latex equation string but output matml code is not the correct one. Can you please guide me if this code can be used to fulfill my goal.

latex code (as per ck editor) : 

x = {-b \pm \sqrt{b^2-4ac} \over 2a}

Incorrect conversion when parentheses next to exponent

Awesome project. Saving me right now because of SymPy's incomplete MathML support.

Input:

\left(- x^{3} + 5\right)^{5}

Expected Output:

<mrow>
	<msup>
		<mrow>
			<mo form="prefix">(</mo>
			<mo>-</mo>
			<msup>
				<mi>x</mi>
				<mn>3</mn>
			</msup>
			<mo>+</mo>
			<mn>5</mn>
			<mo form="postfix">)</mo>
		</mrow>
		<mn>5</mn>
	</msup>
</mrow>

Actual Output:

<mrow>
        <mo fence="true" form="prefix" stretchy="true">(</mo>
        <mo>-</mo>
        <msup>
                <mi>x</mi>
                <mrow>
                        <mn>3</mn>
                </mrow>
        </msup>
        <mo>+</mo>
        <mn>5</mn>
        <mo fence="true" form="postfix" stretchy="true">^</mo>
        <mo>)</mo>
        <mrow>
                <mn>5</mn>
        </mrow>
</mrow>

latex2mathml Matrices transfer problem

I’ve faced a problem with latex2mathml in transferring a matrices

Environment
os: MacOS 10.13.6
python version 3.7.0
latex2mathml version 2.8.0
IDE using spyder 4.0.0

this is my code:
d = latex2mathml.converter.convert("\begin{matrix}a & b \ c & d \end{matrix}")
print(d)

missing β€œ>” in one of block.
latex2mathml Matrices

even I add ">", it is still different between the result and the example in "readme"
example

[Feature Request] \text

Is there a way to embed plain text into formula ?

Look's like \text should do the trick, but it doesn't seem to be implemented.

Thank you in advance :)

"\limits" can't convert

"\limits" can't convert.
When convert r"\int\limits_{0}^{\pi} f{\left(g{\left(x \right)},y \right)}, dx",

<math xmlns="http://www.w3.org/1998/Math/MathML"><mrow><mo>&#x0222B;</mo><msubsup><mi>limits</mi><mrow><mn>0</mn></mrow><mrow><mi>&#x003C0;</mi></mrow></msubsup><mi>f</mi><mrow><mrow><mo fence="true" form="prefix" stretchy="true">&#x00028;</mo><mrow><mi>g</mi><mrow><mrow><mo fence="true" form="prefix" stretchy="true">&#x00028;</mo><mrow><mi>x</mi></mrow><mo fence="true" form="postfix" stretchy="true">&#x00029;</mo></mrow></mrow><mi>,</mi><mi>y</mi></mrow><mo fence="true" form="postfix" stretchy="true">&#x00029;</mo></mrow></mrow><mspace width="0.167em" /><mi>d</mi><mi>x</mi></mrow></math>

Spacing

Right now it appears both \quad and \qquad convert to a 1/6 em space, but there should be some distinction here.

In addition, \hspace{1em} support would be nice for manually defining horizontal spacing.

ImportError: No module named 'aggregator'

when i install with pip or setup.py, i successd. but when import latex2mathml, then error:

ImportError: No module named 'aggregator'

please check this version for python3.4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.