You are transported in a parallel universe where people write mathematical equations on computers as ASCII art by hand. As a LaTeX addict, this is totally unacceptable, and you ought to automate this process somewhat.
Your goal is to write a program that outputs an ASCII version of an equation inputed as a LaTeX math command.
Mandatory LaTeX commands to support
Sum: the LaTeX command for a sum is
\sum_{lower bound}^{upper bound}
The ASCII figure you have to use for sums is:
upper bound ___ \ ` /__, lower bound
Product: the LaTeX command for a product is
\prod_{lower bound}^{upper bound}
The ASCII figure you have to use for products is:
upper bound ____ | | | | lower bound
Fraction: the LaTeX command for fractions is
\frac{numerator}{denominator}
The ASCII figure you have to use for fractions is:
numerator ----------- denominator
Anything that is not one of those three commands is displayed as is. For example, \sum{i=3}^{e^10}\frac{3x+5}{2}
should be displayed as
e^10
___ 3x+5
\ ` ----
/__, 2
i=3
Inputs
The input is a LaTeX command passed as a string (or your language's equivalent to strings). LaTeX commands can be nested, for instance \frac{\frac{1}{2}}{3}
is a valid input. Inputs are supposed to be always correct (no need to check LaTeX's syntax in your code). Inputs will only consist of the three LaTeX commands presented above and 'text' that you won't need to format.
LaTeX commands will always come with the syntax presented above, i.e. sums and products will always have upper and lower bounds (although they can be empty) and there will always be a numerator and denominator for fractions.
We assume that the bounds of sums and products are at most 4 characters long (= the width of the sum and product symbols), so that you don't have to worry about possible overlap issues. For similar reasons, we assume that the bounds are just 'text' and will never be LaTeX commands, e.g. \sum_{\sum_{1}^{2}}^{1}
is not a valid input.
Outputs
Your program's output is the ASCII representation of the LaTeX command you were given as input.
Your program has to take horizontal alignment into account: for instance, the bounds of the sum or the product have to be horizontally aligned with the sum or product symbol (which are both 4 characters wide). If the bound has an odd number of characters, it does not matter whether it is one character off to the right or to left of the center, whichever is fine. The fraction's line has to be as long as the numerator or the denominator, whichever is the longest.
Your program has to take vertical alignment into account: for instance, \frac{\frac{1}{2}}{3} = \frac{1}{6}
should be displayed as
1
-
2 1
- = -
3 6
For sums and products, since the symbols are 4 characters high, the vertical center is assumed to be the second line from the top.
Horizontal spacing is assumed to be correct in the given input, i.e. the spaces in the input should be displayed in the output.
Test cases
Input
abc = 2
Output
abc = 2
Input
e = \sum_{n=0}^{+inf} \frac{1}{n!}
Output
+inf ___ 1 e = \ ` -- /__, n! n=0
Input
e^x = 1 + \frac{x}{1 - \frac{x}{2 + x - ...}}
Output
x e^x = 1 + --------------- x 1 - ----------- 2 + x - ...
Input
\prod_{i=1}^{n} \frac{\sum_{j=0}^{m} 2j}{i + 1}
Output
m ___ \ ` 2j n /__, ____ j=0 | | ------- | | i + 1 i=1
Input
\frac{sum}{prod} = \sum_{frac}^{prod} sum
Output
prod sum ___ ---- = \ ` sum prod /__, frac
Scoring
This is code-golf, so the shortest code wins.