How to convert a text in Mathml to Latex using Pandoc?

Asked

Viewed 118 times

0

I’m trying to use the site http://pandoc.org/try/ to convert html text to latex. My python code is as follows::

import requests

mathml = "<math display='block'xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mfrac><mrow><mo>&#x2212;</mo><mi>b</mi><mo>&#x00B1;</mo><msqrt><mrow><msup><mi>b</mi><mn>2</mn></msup><mo>&#x2212;</mo><mn>4</mn><mi>a</mi><mi>c</mi></mrow></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac></mrow></math>"

def convert_url_pandoc_2_latex(url_pandoc='https://pandoc.org/try/', mathml=mathml):
    info = {'text': mathml, 'from': 'html', 'to': 'latex'}
    r = requests.get(url_pandoc, params=info)
    return r 

r = convert_url_pandoc_2_latex()

type(r) = requests.models.Response

When running r.content I get the following return:

b'<!doctype html>\n<html lang="en">\n<head>\n  <meta charset="utf-8">\n  <title>Try pandoc!</title>\n  <meta name="viewport" content="width=device-width, initial-scale=1.0">\n  <meta http-equiv="X-UA-Compatible" content="IE=edge">\n  <!--[if lt IE 9]>\n       <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>\n       <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>\n       <![endif]-->\n  <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>\n  <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">\n  <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>\n  <script type="text/javascript">\n(function($) { // http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values\n\t$.QueryString = (function(a) {\n\t    if (a == "") return {};\n\t    var b = {};\n\t    for (var i = 0; i < a.length; ++i)\n\t    {\n\t\tvar p=a[i].split(\'=\');\n\t\tif (p.length != 2) continue;\n\t\tb[p[0]] = decodeURIComponent(p[1].replace(/\\+/g, " "));\n\t    }\n\t    return b;\n        })(window.location.search.substr(1).split(\'&\'))\n})(jQuery);\n\nfunction newpage() {\n  var input = $("#text").val();\n  var from = $("#from").val();\n  var to = $("#to").val();\n  var href = window.location.href;\n  window.location.href = href.replace(/([?].*)?$/,"?" + $.param({text: input, from: from, to: to}));\n};\n\n$(document).ready(function() {\n    var text = $.QueryString["text"];\n    $("#text").val(text);\n    var from = $.QueryString["from"] || "markdown";\n    $("#from").val(from);\n    var to = $.QueryString["to"] || "html";\n    $("#to").val(to);\n    if (text && text != "") {\n       $.getJSON("/cgi-bin/trypandoc", { from: from, to: to, text: text },\n         function(res) {\n          $("#results").text(res.html);\n          $("#version").text(res.version);\n          $("#command").text("pandoc --from " + from + " --to " + to);\n         });\n    };\n    $("#convert").click(newpage);\n    $("#from").change(newpage);\n    $("#to").change(newpage);\n});\n  </script>\n  <style type="text/css">\n    h1 { margin-bottom: 1em; }\n    body { margin: auto; }\n    textarea { height: auto; width: 100%; font-family: monospace; margin-top: 15px; }\n    div.alert { margin: 1em; }\n    h3 { margin-top: 0; margin-bottom: 0; padding: 0; font-size: 100%; }\n    pre#results { width: 100%; margin-top: 15px; }\n    footer { color: #555; text-align: center; margin: 1em; }\n    p.version { color: #555; }\n    button#convert { vertical-align: bottom; }\n    pre#command { margin-top: 1em; background-color: transparent; border: none; }\n  </style>\n</head>\n<body>\n<div class="container-fluid">\n  <div class="row">\n    <div class="col-md-6">\n      <h1>Try pandoc!</h1>\n    </div>\n    <div class="col-md-6">\n      <pre id="command"></pre>\n    </div>\n  </div>\n  <div class="row">\n    <div class="col-md-6">\n      <button class="btn btn-primary btn-xs" id="convert">Convert</button>\n      &nbsp;\n      <label for="from">\n      from\n      </label>\n      <select id="from">\n        <option value="commonmark">CommonMark</option>\n        <option value="creole">Creole</option>\n        <option value="docbook">DocBook</option>\n        <option value="dokuwiki">DokuWiki</option>\n        <option value="fb2">FB2</option>\n        <option value="haddock">Haddock markup</option>\n        <option value="html">HTML</option>\n        <option value="jats">JATS</option>\n        <option value="ipynb">Jupyter Notebook (ipynb)</option>\n        <option value="latex">LaTeX</option>\n        <option value="man">Man</option>\n        <option value="markdown" selected>Markdown (pandoc)</option>\n        <option value="gfm">Markdown (GitHub-flavored)</option>\n        <option value="markdown_phpextra">Markdown (PHP Markdown Extra)</option>\n        <option value="markdown_strict">Markdown (strict)</option>\n        <option value="mediawiki">MediaWiki</option>\n        <option value="markdown_mmd">MultiMarkdown</option>\n        <option value="muse">Muse</option>\n        <option value="native">Native (Pandoc AST)</option>\n        <option value="opml">OPML</option>\n        <option value="org">Org Mode</option>\n        <option value="rst">reStructuredText</option>\n        <option value="t2t">Txt2Tags</option>\n        <option value="textile">Textile</option>\n        <option value="tikiwiki">TikiWiki</option>\n        <option value="twiki">TWiki</option>\n        <option value="vimwiki">Vimwiki</option>\n      </select>\n      <br/>\n      <textarea id="text" maxlength="3000" rows="15"></textarea>\n    </div>\n    <div class="col-md-6">\n      <label for="to">\n      to\n      </label>\n      <select id="to">\n        <option value="S5">S5</option>\n        <option value="asciidoc">AsciiDoc (original)</option>\n        <option value="asciidoctor">AsciiDoc (asciidoctor-flavored)</option>\n        <option value="beamer">LaTeX Beamer</option>\n        <option value="commonmark">CommonMark</option>\n        <option value="context">ConTeXt</option>\n        <option value="docbook4">DocBook v4</option>\n        <option value="docbook5">DocBook v5</option>\n        <option value="dokuwiki">DokuWiki</option>\n        <option value="dzslides">DZSlides</option>\n        <option value="haddock">Haddock markup</option>\n        <option value="html4">HTML 4</option>\n        <option value="html5" selected>HTML 5</option>\n        <option value="icml">ICML</option>\n        <option value="jats">JATS</option>\n        <option value="json">JSON</option>\n        <option value="ipynb">Jupyter Notebook (ipynb)</option>\n        <option value="latex">LaTeX</option>\n        <option value="man">Man</option>\n        <option value="ms">Ms</option>\n        <option value="markdown">Markdown (pandoc)</option>\n        <option value="gfm">Markdown (GitHub-flavored)</option>\n        <option value="markdown_phpextra">Markdown (PHP Markdown Extra)</option>\n        <option value="markdown_strict">Markdown (strict)</option>\n        <option value="mediawiki">MediaWiki</option>\n        <option value="markdown_mmd">MultiMarkdown</option>\n        <option value="muse">Muse</option>\n        <option value="native">Native (Pandoc AST)</option>\n        <option value="opendocument">OpenDocument</option>\n        <option value="opml">OPML</option>\n        <option value="org">Org Mode</option>\n        <option value="plain">Plain text</option>\n        <option value="revealjs">reveal.js</option>\n        <option value="rst">reStructuredText</option>\n        <option value="rtf">RTF</option>\n        <option value="slideous">Slideous</option>\n        <option value="slidy">Slidy</option>\n        <option value="tei">TEI</option>\n        <option value="texinfo">Texinfo</option>\n        <option value="textile">Textile</option>\n        <option value="zimwiki">ZimWiki</option>\n      </select>\n      <br/>\n      <pre id="results"></pre>\n    </div>\n  </div>\n</div>\n<footer>\n <p class="version">pandoc <span id="version"></span></p>\n <p><a href="https://pandoc.org">https://pandoc.org</a></p>\n</footer>\n</body>\n</html>\n'

The next passage comes empty:

<pre id="results"></pre>

But it should come in the following way:

<pre id="results">\[\frac{- b \pm \sqrt{b^{2} - 4ac}}{2a}\]</pre>

This last result I get when I do the procedure manually and access "Inspect" page.

How to modify my code to get [ frac{- b pm sqrt{b {2} - 4ac}}{2a}\] ?

1 answer

0


The URL:

http://pandoc.org/cgi-bin/trypandoc

Place this url in url_pandoc and will be resolved.

The explanation is simple. The page http://pandoc.org/try/ is dynamic and loads the elements via javascript. By pressing the button Convert the page uses javascript to send the text to the url http://pandoc.org/cgi-bin/trypandoc and receive the reply in json.

  • thank you so much for your reply. It worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.