Code Monkey home page Code Monkey logo

markdown-katex's Introduction

markdown-katex

This is an extension for Python Markdown which adds KaTeX support.

```math
f(x) = \int_{-\infty}^\infty
    \hat f(\xi)\,e^{2 \pi i \xi x}
    \,d\xi
```

Project/Repo:

MIT License Supported Python Versions CalVer v202112.1034 PyPI Version PyPI Downloads

Code Quality/CI:

GitHub CI Status GitLab CI Status Type Checked with mypy Code Coverage Code Style: sjfmt

Name role since until
Manuel Barkhau ([email protected]) author/maintainer 2019-05 -

Install

$ pip install markdown-katex
...
$ python -m markdown_katex --version
markdown-katex version:  v202112.1034 (using binary: /usr/local/bin/npx --no-install katex)
0.15.1

This package includes the following binaries:

  • katex_v0.15.1_node10_x86_64_Linux _ katex_v0.15.1_node10_x86_64_Darwin _ katex_v0.15.1_node12_x86_64_Windows

If you are on a different platform, or want to use a more recent version of katex-cli, you will need to install it via npm.

$ npx katex
$ npx katex --version
0.15.1

This extension will always use the locally installed version of KaTeX if it is available, instead of using the implementation bundled with this package.

No JavaScript is required to render the resulting HTML, so it can be used with more limited renderers (which don't support JavaScript) such as WeasyPrint .

Usage

Formulas can be created and edited interactively using the editor on katex.org. They also have some good documentation for the subset of LaTeX that is supported. When embedding these in your Markdown files, they must be marked with a special syntax in order to be rendered using KaTeX. There are many syntax extensions for Markdown that allow LaTeX formulas to be embedded, however this package only supports the syntax introduced by Gitlab:

  • For inline mode formulas: $`...`$
  • For display mode formulas: ```math

Here is an example that uses this syntax.

There are two main advantages of this syntax:

  1. Gitlab has an existing Markdown renderer that can be used without the need to download any software. This implementation also uses KaTeX, so the output should be exactly the same as this extension.
  2. The fallback behaviour of other Markdown renderers is to render the raw LaTeX as inline code or a code block. This means that they won't inadvertently parse a LaTeX formula as Markdown syntax.

Hopefully other renderers will also adopt support for this syntax as:

  1. Rendering is done in the browser using KaTeX so implementation effort and should be minimal.
  2. The false positive rate for existing Markdown documents is negligible (i.e. existing alternate use of $` syntax is minimal to non-existent).

Options

  • no_inline_svg: Replace inline <svg> with <img data:image/svg+xml;base64.."> tags.
  • insert_fonts_css: Insert font loading stylesheet (default: True).

Development/Testing

$ git clone https://gitlab.com/mbarkhau/markdown-katex
$ cd markdown-katex
$ make conda
$ make lint mypy test

MkDocs Integration

In your mkdocs.yml add this to markdown_extensions.

# mkdocs.yml
markdown_extensions:
  - markdown_katex:
      no_inline_svg: True
      insert_fonts_css: True
      macro-file: macros.tex

The macro-file might looks something like this:

% macros.tex
\mymacro:\text{prefix #1 suffix}

WeasyPrint Integration

When you generate html that is to be consumed by WeasyPrint, you need to use the no_inline_svg=True option. This is due to a long standing limitation of WeasyPrint. Without this option, some KaTeX formulas will not render properly, e.g. \sqrt

md_ctx = markdown.Markdown(
    extensions=[
        'markdown.extensions.toc',
        'markdown.extensions.extra',
        'markdown.extensions.abbr',
        ...
        'markdown_katex',
    ],
    extension_configs={
        'markdown_katex': {
            'no_inline_svg': True,      # fix for WeasyPrint
            'insert_fonts_css': True,
        },
    }
)
raw_html_text = md_ctx.convert(md_text)

You can also use markdown-katex for the conversion of individual formulas from tex to html:

from markdown_katex.extension import tex2html

tex_text = r"""
\frac{1}{\left(\sqrt{\phi\sqrt{5}}-\phi\right)e^{\frac{2}{5}\pi}}=
 1+\frac{e^{-2\pi}} {
   1+\frac{e^{-4\pi}} {
     1+\frac{e^{-6\pi}} {
       1+\frac{e^{-8\pi}} {
         1+\cdots
       }
     }
   }
}
"""
options = {'no_inline_svg': True, 'insert_fonts_css': False}
html = tex2html(tex_text, options)

markdown-katex's People

Contributors

deining avatar jnsdrtlf avatar julianstirling avatar mbarkhau avatar pawamoy avatar peppyhare avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

markdown-katex's Issues

Inline equation paragraph stripping

Hi,

Currently if you try to use an inline equation on it's own the postprocessor will strip the paragraph tags added by the markdown processing.
e.g.

$`e=mc^2`$

$`y=mx+c`$

will have the paragraph tags stripped and render on the same line.

Changing line 300 in extension.py to if wrapped_marker in text and marker.startswith('tmp_block'): appears to be a simple fix and works at least for my current usage.

Math rendering below the Fenced Code Blocks

Actual Behavior

Math blocks don't render at all if placed below the Fenced Code Block wrapped with ```` but works fine with another fenced code block. For example:

````
````
$`a^2`$

doesn't render the equation properly, but the following example works correctly:

~~~
~~~
$`a^2`$

Expected Behavior

The markdown block as follows has to render equation:

````
````
$`a^2`$

References

No such file or directory: /tmp/mdkatex/adcebc8e8ae41063cc456af7bc8c63e5b2023b67b78beea322358d8b8b4409d5.tex

Whenever I run md.convert(), I get hit with some variation of the error No such file or directory: /tmp/mdkatex/adcebc8e8ae41063cc456af7bc8c63e5b2023b67b78beea322358d8b8b4409d5.tex (line 234 in _write_tex2html of wrapper.py).
Looking in the code, it looks like the problem is caused by trying to delete a temporary tex file that was created but doesn't exist anymore for whatever reason. I'm confused because the file is explicitly created earlier on in _write_tex2html, and if there's any issues with the file creation it should just raise an error then.

The markdown-katex version:
markdown-katex version: v202112.1034 (using binary: /usr/bin/npx --no-install katex) 0.16.8

Can I config sytax?

Inline math sytax: $...$ and block math sytax $$...$$ are so common in markdown.
If we can config it, that will be awesome.

Rendering of square root in weasy print

Hi,

I am having problems with square roots, even when I try to follow the instructions in the README. I am just running the test formula

$$f = \sqrt{\frac{2}{3}}$$

In the browser the square root only appears if I don't set 'no_inline_svg': True,. In WeasyPrint the square root does not appear for no_inline_svg being True or False.

Details

OS
Kubuntu 20.04

Python
Python 3.7.10

Packages

WeasyPrint==52.5
Markdown==3.3.2
markdown-katex==202105.1031

Edited to add

It seems that the issue is my CSS. Specifically object-fit being set to contain. It would be good to not have to add classes to every other image as this ruins the simplicity of markdown. The fix I can do in the browser is to change the katex css to

.katex img {
    object-fit: fill;
    padding: unset;
}

I will open an MR adding these to the inline CSS.

Can't import markdown_katex after installing

I installed markdown_katex using pip, but when I run import markdown_katex or run python -m markdown_katex --version I get the error No module named markdown_katex. The package is successfully installed, and pip recognizes it as present. The folder for the module is present in the correct directory. But I am unable to import it. Does anyone know why this might happen or what I might do to fix this?

I am on MacOS Big Sur running Python 3.9.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.