Skip to main content

Forked from Andrew's masterpiece:https://github.com/ajtulloch/LaTeX2Markdown, An AMS-LaTeX compatible converter that maps a subset of LaTeX to Markdown/MathJaX.

Project description

An AMS-LaTeX compatible converter from (a subset of) LaTeX to MathJaX compatible Markdown.

Anyone who writes LaTeX documents using the AMS-LaTeX packages (amsmath, amsthm, amssymb) and wants to convert these documents to Markdown format to use with MathJaX. These Markdown files can then be easily added to any web platform - Jekyll blogs, Wordpress, basic HTML sites, etc.

In short, if you seek to use MathJaX to view your LaTeX documents online, then you might be interested in this.

Demonstration

Check out tullo.ch/projects/LaTeX2Markdown for a live demonstration of the converter.

Getting Started

Installation

The project is available on PyPI, so getting it is as simple as using

pip install latex2markdown-bbsmp

or

easy_install latex2markdown-bbsmp

Usage

The utility can be called from the command line, or from within a Python script.

For the command line, the syntax to convert a LaTeX file to a Markdown file is as follows:

python -m latex2markdown-bbsmp path/to/latex/file path/to/output/markdown/file t

For example, to compile a LaTeX file sample.tex into a Markdown file sample.md, call

python -m latex2markdown-bbsmp sample.tex sample.md

To use it within a Python script (to extend it, modify output, etc.), you can use it as follows:

import latex2markdown
with open("latex_file.tex", "r") as f:
    latex_string = f.read()

l2m = latex2markdown.LaTeX2Markdown(latex_string)

markdown_string = l2m.to_markdown()

with open("markdown_file.md", "w") as f:
    f.write(markdown_string)

Finally, add the following snippet to your HTML when loading this document.

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        extensions: ["tex2jax.js", "AMSmath.js"],
        jax: ["input/TeX", "output/HTML-CSS"],
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"] ],
            displayMath: [ ['$$','$$'], ["\[","\]"] ],
            processEscapes: true
        },
    });
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

For a working example, have a look at the source of the tullo.ch homepage here.

Why not use Pandoc?

Pandoc is an excellent document converter for less complex LaTeX documents. Indeed, I’ve used it to convert this README document to a reST version for use on PyPI.

Unfortunately, it is not designed to deal with documents that use the AMSTeX extensions - which include the theorem, lemma, proof, and exercise environments that are heavily used for typesetting papers, lecture notes, and other documents.

As neither Pandoc nor MathJaX can deal with these documents, I hacked together a set of regular expressions that can convert a subset of LaTeX to Markdown, and used a few more to convert the sMarkdown to MathJaX-convertible Markdown.

Example

As an example, the following LaTeX code:

\section{Example Section}
\begin{thm}[Euclid]
    There are infinitely many primes.
\end{thm}

\begin{proof}
    Suppose that $p_1 < p_2 < \dots < p_n$ are all of the primes.
    Let $P = 1 + \prod_{i=1}^n p_i$ and let $p$ be a prime dividing $P$.

    Then $p$ can not be any of $p_i$, for otherwise $p$ would divide the
    difference $P - \left(\prod_{i=1}^n p_i \right) - 1$, which is impossible.
    So this prime $p$ is still another prime, and $p_1, p_2, \dots p_n$
    cannot be all of the primes.
\end{proof}

is converted into the following Markdown:

###  Example Section
#### Theorem 1 (Euclid)

> There are infinitely many primes.

#### Proof

Suppose that $p_1 < p_2 < \dots < p_n$ are all of the primes.
Let $P = 1 + \prod_{i=1}^n p_i$ and let $p$ be a prime dividing $P$.

Then $p$ can not be any of $p_i$, for otherwise $p$ would divide the difference
$P - \left(\prod_{i=1}^n p_i \right) - 1$, which is impossible. So this prime
$p$ is still another prime, and $p_1, p_2, \dots p_n$ cannot be all of the primes.

Supported LaTeX/AMSTeX Environments

  • emph, textbf, texttt

  • thm

  • prop

  • lem

  • exer

  • proof

  • chapter

  • section

  • subsection

  • itemize

  • enumerate

along with everything supported by MathJax - list available online.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

latex2markdown-bbsmp-0.0.3.9.tar.gz (41.5 kB view details)

Uploaded Source

File details

Details for the file latex2markdown-bbsmp-0.0.3.9.tar.gz.

File metadata

  • Download URL: latex2markdown-bbsmp-0.0.3.9.tar.gz
  • Upload date:
  • Size: 41.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/36.0.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/2.7.12

File hashes

Hashes for latex2markdown-bbsmp-0.0.3.9.tar.gz
Algorithm Hash digest
SHA256 9e3b88978fc9c60bcf25947b2779c4bcd97a2f7a06082a92b5cdaae756b30545
MD5 87f2e7882a965726a3b1237a03913f77
BLAKE2b-256 a654b696e9a5ce6c0d18082a3b0bc0c6cc200a48fbde46e5fec990e79d14cfd2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page