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.
Metadata
Release files for texmd 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| texmd-0.1.2.tar.gz | 14.0 kB | Without provenance | Details for texmd-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| texmd-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for texmd-0.1.2-py3-none-any.whl |
Total release size: 29.8 kB
Release files / texmd-0.1.2.tar.gz
| Download URL | texmd-0.1.2.tar.gz |
|---|---|
| Size | 14.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
416fbf0ff7aaf6371683df2361e7948b73a669279699741091d685482f316696
|
|
BLAKE2b-256 checksum How to use checksums |
744df759b31b27d9391bac8aa0921008b369340940be9e8e6515c389884c82e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.13.0
|
Release files / texmd-0.1.2-py3-none-any.whl
| Download URL | texmd-0.1.2-py3-none-any.whl |
|---|---|
| Size | 15.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a8e8583670949156f94be1fcfd39f5dfff4079a0199411669325b32ca54c8656
|
|
BLAKE2b-256 checksum How to use checksums |
5456dbc25a0b976769a0ee8bff95cd9f044c161a93892535123eda69fd1f0339
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.13.0
|
Security for texmd 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.