Self-rendering Markdown + LaTeX documents
TeXMe is a lightweight JavaScript utility to create self-rendering Markdown + LaTeX documents.
Copy and paste the code below into an HTML file with .html as the
extension name:
<!DOCTYPE html><script src="https://cdn.jsdelivr.net/npm/[email protected]"></script><textarea>
# Euler's Identity
In mathematics, **Euler's identity** is the equality
$$ e^{i \pi} + 1 = 0. $$
## Explanation
Euler's identity is a special case of Euler's formula from complex
analysis, which states that for any real number $ x $,
$$ e^{ix} = \cos x + i \sin x. $$This file contains one line of HTML code followed by Markdown + LaTeX content.
Open this HTML file with a web browser. It renders itself to look like this: get-started.html. Here is a screenshot of the output:
There are three simple rules to remember while using TeXMe:
TeXMe removes any leading and trailing whitespace in the content before rendering the content to HTML.
TeXMe uses the first non-empty line of the content to set the page
title if no explicit <title> element is specified. Any leading and
trailing whitespace and hash (#) characters are removed while
setting the page title.
If there is a Markdown element such as code span/block or image with
LaTeX delimiters in it (e.g., $, $$, etc.), TeXMe may interpret
it as LaTeX which may lead to incorrect rendering of the document.
To prevent this issue, put such Markdown element within a special
purpose md environment supported by TeXMe, for example,
\begin{md}`echo $foo`\end{md}. If you do not have such Markdown
elements with LaTeX delimiters, you may ignore this rule. See the
Markdown Priority Environment
section to see more details about this. Note: For most documents,
you don't have to worry about this point.
If you do not like to start your document with HTML tags, you can write
your content first and add the <script> tag in the end but this
approach has some limitations. See the Content in
Body section for more details about it.
Use the following URL in the <script> tag to load version 1.2.2 (the
current version at this time) of TeXMe:
https://cdn.jsdelivr.net/npm/[email protected]Use the following URL in the <script> tag to always load the latest
version of TeXMe:
https://cdn.jsdelivr.net/npm/texmeIf you need something really easy to remember, use this URL to load the latest version of TeXMe:
https://unpkg.com/texmeThe Get Started section earlier shows how we can create
a self-rendering document with a single line of HTML code but this
brevity comes at the cost of standard conformance. For example, the
required <title> element is missing from the code. Further the
<textarea> element is not closed.
For the sake of completeness and correctness, here is a minimal but complete and valid HTML5 example:
<!DOCTYPE html>
<html lang="en">
<title>Notes on Euler's Identity</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<textarea>
# Euler's Identity
In mathematics, **Euler's identity** is the equality
$$ e^{i \pi} + 1 = 0. $$
## Explanation
Euler's identity is a special case of Euler's formula from complex
analysis, which states that for any real number $ x $,
$$ e^{ix} = \cos x + i \sin x. $$
</textarea>Here is the output: valid-html5.html.
It has a few more lines of code to ensure that this HTML5 code validates successfully at validator.w3.org. As a result, this example does not look as concise as the one in the previous section.
In case you are wondering, a valid HTML5 document does not require
explicit <head>, <body>, or the closing </html> tags, so they have
been omitted for the sake of brevity while maintaining completeness and
correctness.
In practice though, it is not necessary to write verbose code like this. All browsers follow the robustness principle, so they can render the shorter example in the Get Started section just fine.
TeXMe renders the document on a white pane against a gray background by
default. This is due to a configuration option named style that is set
to 'viewer' by default.
To render the document with a minimal style on a completely plain white
background, set the style configuration option to 'plain'. Here is
an example:
<!DOCTYPE html>
<script>window.texme = { style: 'plain' }</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script><textarea>
# Euler's Identity
In mathematics, **Euler's identity** is the equality
$$ e^{i \pi} + 1 = 0. $$
## Explanation
Euler's identity is a special case of Euler's formula from complex
analysis, which states that for any real number $ x $,
$$ e^{ix} = \cos x + i \sin x. $$Here is the output: style-plain.html.
To render the document with absolutely no style, set style to
'none'. The 'none' style option is useful to disable the default
'viewer' style set by TeXMe before defining a custom style with
regular CSS code. Here is an example:
<!DOCTYPE html>
<script>window.texme = { style: 'none' }</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<style>
body {
background: lightcyan;
}
main {
max-width: 20em;
padding: 1em;
border: medium double gray;
margin: 2em auto;
background: lightyellow;
}
</style>
<textarea>
# Euler's Identity
In mathematics, **Euler's identity** is the equality
$$ e^{i \pi} + 1 = 0. $$
## Explanation
Euler's identity is a special case of Euler's formula from complex
analysis, which states that for any real number $ x $,
$$ e^{ix} = \cos x + i \sin x. $$Here is the output: style-custom.html.
Note that the rendered content is displayed within a <main> element
inside the <body>. That is why these elements are being styled in the
above example.
To render Markdown-only content without any mathematical content at all,
set useMathJax and protectMath options to false:
<!DOCTYPE html>
<script>window.texme = { useMathJax: false, protectMath: false }</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script><textarea>
# Atomic Theory
**Atomic theory** is a scientific theory of the nature of matter, which
states that matter is composed of discrete units called *atoms*. It
began as a philosophical concept in ancient Greece and entered the
scientific mainstream in the early 19th century when discoveries in the
field of chemistry showed that matter did indeed behave as if it were
made up of atoms.Here is the output: markdown-only.html.
When TeXMe loads, it begins rendering the document automatically. This
automatic rendering may be skipped by setting renderOnLoad option to
false. Here is an example that disables automatic rendering and then
invokes rendering later on the click of a button by using the
texme.renderPage() function from the TeXMe API:
…Here is the output: skip-render.html.
When we load TeXMe with the <script> tag, it begins rendering the
document as soon as it loads. Therefore in the above examples, we define
the configuration options prior to loading TeXMe. We do this by defining
an object named window.texme with the configuration options defined as
properties in this project.
However if we set the renderOnLoad option to false, we prevent TeXMe
from rendering the document after it loads. We now have the control to
invoke the rendering at a later time, e.g., on the click of a button. In
this case, it is possible to set configuration options after loading
TeXMe with the texme.setOption() function. This function takes two
parameters: option name as a string and option value.
Here is an example that skips automatic rendering on load and sets the
style to 'plain' using this function:
…Here is the output: set-options.html.
If you do not like to start your document with HTML tags, you can
write your content first and add the <script> tag in the end like
this:
# Euler's Identity
In mathematics, **Euler's identity** is the equality
$$ e^{i \pi} + 1 = 0. $$
## Explanation
Euler's identity is a special case of Euler's formula from complex
analysis, which states that for any real number $ x $,
$$ e^{ix} = \cos x + i \sin x. $$
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>Here is the output: content-in-body.html.
Although, the code looks neater in this example, there is a limitation
associated with this form of writing content: Since the content is part
of the HTML <body> element (there is no <textarea> element in this
code), the content should be written carefully, so that it does not have
any HTML syntax error.
While using the content-in-body method of using TeXMe, an HTML syntax
error in the content can produced mangled output. For example, the
following input is not rendered as expected because the content is in
the <body> element, so the browser interprets this content as HTML and
encounters the beginning of a start tag that is not closed properly:
HNo open issues yet, or sync has not completed.