Skip to main content

A simple library that translates LaTeX to Markdown.

Project description

texmd

A small library that converts LaTeX to Markdown. This package uses pylatexenc to parse the LaTeX expressions.

Currently, it supports converting inlined mathematical equations $...$, equation blocks (equation, equation*, align, align*, array, matrix, eqnarray, multline), title \title, sections (\section, \subsection, subsubsection); abstract content \abstract{...} (supported by Markdown block quote); in-text quotations ``...''; equation numbered labels are also supported. More will be introduced in later versions.

Installation

Run pip install texmd in the terminal.

Usage

This package allows you to load a .tex file directly.

from texmd import tex # Import the package

parser = tex.TexParser()
file_path = "<PATH_TO_TEX_FILE>"
tex_file = parser.load_file(file_path) # Load the file

The loaded file tex_file is type of texmd.texmd.TexDocument.

If you want to parse the LaTeX string directly you can also do

tex_expr = "<TEX_EXPR>"
tex_file = parser.parse(tex_expr)

We can convert then it to Markdown by

document = parser.to_md(tex_file)

The output document is type of texmd.md.MdDocument. To output the document as Markdown syntax we can do

md = document.to_str()

and you can write it to a .md file.

Add BibTeX support

In order for the package to also process BibTeX we will have to load the .bib file.

parser.load_citations("<BIB_FILE_PATH>")

Customization

If you don't like the way the package write the Markdown, or you want to support custom LaTeX expressions, you can use the API parser.set_converter with a specific sub-type of texmd.tex.TexNode.

For example you want to set a new converter for text node.

class TextNodeConverter(Converter):
    """ A converter for LaTeX text nodes. """

    def __init__(self):
        super().__init__(None)

    def convert(self, node: TexTextNode) -> Generator[MdNode]:
        def _():
            yield MdText(text=node.text)
        return _()

converter = TextNodeConverter()
parser.set_converter(TexTextNode, '', converter)

The default Citation converter is implemented as follows,

class CiteConverter(Converter):
    """ A converter for LaTeX cite nodes. """

    def __init__(self, parser: 'TexParser'):
        super().__init__(parser)

    def convert(self, node: TexMacroNode) -> Generator[MdNode]:
        def write_author(author: bib.Author) -> str:
            first_abbr = author.first_name[0] + '.' if author.first_name else ''
            middle_abbr = author.middle_name[0] + '.' if author.middle_name else ''
            return f"{first_abbr} {middle_abbr} {author.last_name}"
        
        def write_entry(entry: bib.Entry) -> str:
            content: List[str] = [*(write_author(author) for author in entry.authors), entry.title, entry.year]
            return ", ".join(content)

        def _():
            chars: TexTextNode = node.children[0].children[0]
            cite_names = chars.text.replace(' ', '').split(',')
            citations = (self.parser._get_citation(name) for name in cite_names)
            citations = (write_entry(entry) for entry in citations if entry)
            yield MdText(text="(*" + ", ".join(citations) + "*)")
        return _()

If you want a different style you can implement a new one and load it via

class YourCiteConverter(Converter)
    ...

converter = YourCiteConverter(parser)
parser.set_converter(TexMacroNode, 'cite', converter)

And you can obtain the citation entries via texmd.tex.TexParser._get_citation in the format of texmd.bib.Entry.

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

texmd-0.1.1.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

texmd-0.1.1-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file texmd-0.1.1.tar.gz.

File metadata

  • Download URL: texmd-0.1.1.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0

File hashes

Hashes for texmd-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8294ceb0dbeaf482a117f097720b958572c68c89c4d2b916be3b2b29c47706b6
MD5 e0a7ded4c40b1bbbb9c7fe6f965ca806
BLAKE2b-256 e8f1b55068dc23d6d9053cbefffc201689e0cacb32546cf19ef0322cd52b3da0

See more details on using hashes here.

File details

Details for the file texmd-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: texmd-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0

File hashes

Hashes for texmd-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d14c333cfe16cb05e46686974d0065492231c1c392ec59eca64f0720c530328b
MD5 c41525efe9ed20002c6088387160adcc
BLAKE2b-256 7942eb7f0e4d3bb8a61e3fb2a6b1dae63797b47263db2aa57268013c3d8464d9

See more details on using hashes here.

Supported by

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