Git Product home page Git Product logo

asciimath's Introduction

Build Status

AsciiMath

AsciiMath is a compiler from the amazing AsciiMath language to LaTeX.

The aim of that project is to provide a Haskell Library and a command line tool making possible to use asciimath everywhere, for example in interaction with pandoc or for preprocessing on your website.

Install

Use stack.

If you already have stack set up and working, you just have to type stack install at the root of the repository.
If you want the binaries to be stored in a different folder from the default $HOME/.local/bin/ you should add the option --local-bin-path $YOUR_BIN_DIR.

If you just installed stack, stack may ask you to run stack setup first to install GHC and set up an isolated environment (either globally in your home or in a dedicated folder just for this repository)

Usage

The executable asciimath reads AsciiMath code from its standard input, compile it and prints the resulting LaTeX code on its standard output. For example:

> echo "sum_(i=1)^n i^3=((n(n+1))/2)^2" | ./asciimath
\sum_{i=1}^{n}i^{3}=\left(\frac{n\left(n+1\right)}{2}\right)^{2}

or in "interactive mode"

> asciimath
sin(2x) = 2sin(x)cos(x) ^D
\sin \left(2x\right)=2\sin \left(x\right)\cos \left(x\right)

The executable pandoc-asciimath is a pandoc filter. An example of use would be

> pandoc --standalone -t latex --filter pandoc-asciimath file.md -o file.pdf

The Asciimath module also provide four functions:

  • readAscii :: String -> Either LexicalError Ast.Code
  • writeTeX :: Ast.Code -> String
  • compile :: String -> Either LexicalError String
  • run :: String -> String

which can be used in any Haskell program to play with the AST or anything else.

The run function do the same work as compile but raises a system error if it fails whereas compile returns either a LexicalError or the expected compiled String which is much more convenient for error handling.

Grammar

Here is the grammar used to parse AsciiMath. It is a little different from the original one defined here but I think the changes I made respect the original idea.

c ::= [a-zA-Z] | numbers | greek letters | standard functions | , | other symbols (see list)

u ::= sqrt | text | bb | bbb | cc | tt | fr | sf | hat | bar | ul | vec | dot | ddot

b ::= frac | root | stackrel

l ::= ( | [ | { | (: | {:

r ::= ) | ] | } | :) | :}

S ::= c | l Code r | u S | b S S | " any text "

E ::= S | S / S | S _ S | S ^ S | S _ S ^ S

Code ::= [ E ]

Rendering rules

  • The constants are converted to their LaTeX equivalent
  • sqrt, hat, bar, vec, dot and ddot are prefixed with a \
  • text and ul correspond to the \textrm and underline functions
  • bb, bbb, cc, tt, fr and sf correspond to the \boldsymbol, \mathbb, \mathcal, \texttt, \mathfrak and \textsf functions
  • frac is rendered as a fraction, root n x as the n-th root of x and stackrel x y displays x upon y
  • Any text placed between a pair of " is rendered in the same font as normal text.
  • / stands for a fraction. The _ and ^ tokens have the same behaviour as in LaTeX but the subscript must be placed before the superscript if they are both present

Delimiters

Left and right delimiters are preceded by the \left and \right commands to be well-sized. (: and :) are chevrons. {: and :} are invisible delimiters like LaTeX's {. The other delimiters are rendered as expected.

Useless delimiters are automatically removed in expressions like

  • (...)/(...)
  • (...)_(...), (...)^(...) and the combination of sub- and superscript
  • u (...), b (...) (...) where u and b are unary and binary operators

If you want them to be rendered, you have to double them, for example : ((x+y))/2 or {: (x+y) :}/2

Matrices

An expression of the form ((a,b),(c,d),(e,f)) is interpreted and rendered as a matrix. More precisely :

  • The outer brackets must be (,) or [,] and they determine whether the rendered matrix is a pamtrix or a bmatrix.
  • The inner brackets must be (,) or [,] too. They have to be all the same but they can be different from the outer brackets.
  • The matrix can have any size but cannot be empty
  • In the example, (a,b), (c,d) and (e,f) are the lines of the matrix
  • The lines must have the same lengths

License

The code of AsciiMath is released under the MIT license

asciimath's People

Contributors

kerl13 avatar phyks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

asciimath's Issues

d in integrals

In integrals (e.g. int_0^1 f(x) dx) , dx should not be rendered as $dx$ but as $\mathrm{d},x$ afaik.

Braces along multiple equations

Something like

\left\{\begin{array}{c}
    eq1 \\
    eq2 \\
\end{array}\right.

is widely used in LaTeX, it would be really cool to have it here.

Ugly "..." parsing

The structure " ... " should be handled using alex's startcodes and not the current hack

hbar

Should be really helpful if hbar could be used to produce a \hbar (same as greek letters).

Complex conjugate

Due to the fact that * gets translated in middle dot (as an operator), it is also the case when it is in an exponent.

Then, to write z^* (complex conjugate), one has to write z^(**).

Support new keywords

It would be great to be able to support new keywords.

Among them, I can see:

  • \hbar
  • \dagger
  • transposition of a matrix
  • \left| x \right> for kets in quantum mechanics. Conversely, \left< x \right| for bras.

Ambiguous grammar

In the current grammar, "cos(x)/2" can be reduced the following two ways

  1. Frac (cos ( x )) (2)
  2. cos, Frac (( x )) (2)
    The official asciimath parser seems to chose the first option whereas this one chose the second…

Missing HUnit dependency with cabal

(cd filters/asciimath; cabal update; make filter-only)
    Downloading the latest package list from hackage.haskell.org
    Skipping download: local and remote files match.
    cabal install --only-dependencies
    Resolving dependencies...
    All the requested packages are already installed:
    Use --reinstall if you want to reinstall anyway.
    cabal configure --enable-optimization --enable-test
    Resolving dependencies...
    Warning: solver failed to find a solution:
    Could not resolve dependencies:
    rejecting: AsciiMath-0.1.0.0:!test (constraint from config file, command line
    flag, or user target requires opposite flag selection)
    trying: AsciiMath-0.1.0.0:*test
    unknown package: HUnit (dependency of AsciiMath-0.1.0.0:*test)
    Dependency tree exhaustively searched.
    Trying configure anyway.
    Configuring AsciiMath-0.1.0.0...
    cabal: Encountered missing dependencies:
    HUnit -any

Support fonts

cc "L" is a valid Asciimath font command according to their homepage, but is not supported by AsciiMath.

H_Kerr versus H_(Kerr)

Why not considering that $H_Kerr$ is the same as $H_(Kerr)$?

And force a user who wishes to write $H_K err$ as such?

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.