Skip to main content

Like HTML, but not LaTeX? HtmLaTeX compiles HTML code to LaTeX.

Project description

HtmLaTeX

Work in Progress

Like HTML, but not LaTeX? HtmLaTeX compiles HTML code to LaTeX.

For example, the following HTML code:

<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>My Paper</title>
    <meta name="date" content="2022-09-02">
    <meta name="author" content="Linus">
    <link rel="icon" type="image/png" href="logo.png">
</head>
<body>
<div>
    <a href="#">link</a>
</div>
<h1 id="section-1">Hello World</h1>
<p>This is a paragraph.</p>

<img src="a">

</body>
</html>

...compiles to the following LaTeX code:

%%%%%% HEAD START %%%%%%

	\title{My Paper}

	\date{2022-09-02}

	\author{Linus}

%%%%%% HEAD END %%%%%%

%%%%%% MAIN DOCUMENT START %%%%%%
\begin{document}
\maketitle

	\section{Hello World}
	\label{section-1}

	This is a paragraph.

	\includegraphics{a}

\end{document}
%%%%%% MAIN DOCUMENT END %%%%%%

Installation

$ pip install HtmLaTeX

Usage

As binary

$ python3 -m htmlatex paper.html

This will create a file called paper.tex in the current directory.

As library

import io
import bs4
from htmlatex import Handlers, htmlatex

# Declare handlers for HTML tags

# Collection of handlers for HTML tags
handlers = Handlers()


# Register a handler for the <h1> tag
@handlers.register()
def h1(node: bs4.element.Tag):
    section_name = node.string
    section_id = node.attrs['id']
    yield [
        r"\section{%s}" % section_name,
        r"\label{%s}" % section_id,
    ]


# ... more handlers ...

# Destination for generated LaTeX
generated_latex = io.StringIO()
# HTML to be converted
html = "..."
# And compile
htmlatex(html, generated_latex, handlers=handlers)

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

HtmLaTeX-0.1.0.tar.gz (3.3 kB view hashes)

Uploaded Source

Built Distribution

HtmLaTeX-0.1.0-py3-none-any.whl (3.7 kB view hashes)

Uploaded Python 3

Supported by

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