r/dailyprogrammer • u/Elite6809 1 1 • Jun 05 '15
[2015-06-05] Challenge #217 [Practical Exercise] TeXSCII
(Practical Exercise): TeXSCII
LaTeX is a typesetting utility based on the TeX typesetting and macro system which can be used to output mathematical formulae to display or print. For example, the LaTeX code \frac{-b\pm\sqrt{b^{2}-4ac}}{2a} will be transformed into this when typeset.
The syntax of LaTeX formulae is fairly simple; commands begin with a backslash \, followed by the command name, followed by its arguments in curly braces, such as \sqrt{-1} (square-root of -1) or \frac{1}{3} (1/3 as a fraction). Subscript and superscript are also supported, with the _ and ^ characters respectively, followed by the script in curly braces - for example, x^{2} outputs x2. Everything else is output as plain text.
In today's challenge, you'll implement a simplified subset of LaTeX which outputs the resulting formula as ASCII.
Formal Inputs and Outputs
Input Specification
You'll be given a LaTeX equation on one line. The commands you need to support are:
\frac{top}{bottom}: A fraction with the given top and bottom pieces\sqrt{content}: A square-root sign\root{power}{content}: A root sign with an arbitrary power (eg. cube-root, where the power 3 is at the top-left of the radical symbol)_{sub}: Subscript^{sup}: Superscript_{sub}^{sup}: Subscript and superscript (one on top of the other)\pi: Output the greek symbol for pi
Feel free to extend your solution to support any additional structures such as integral signs.
Output Description
Output the formula with ASCII symbols in the appropriate locations. You're free to pick the output style that looks most appropriate to you. One possible way might be something like this:
3_
√x
y=--
3
Sample Inputs and Outputs
Subscripts and Superscripts
Input
log_{e}(e^{x})=x
Output
x
log (e )=x
e
Stacked Scripts
Input
F_{21}^{3}=2^{5}*7^{3}-30
Output
3 5 3
F =2 *7 -30
21
Fractions
Input
sin^{3}(\frac{1}{3}\pi)=\frac{3}{8}\sqrt{3}
Output
3 1 3 _
sin (-π)=-√3
3 8
Quadratic Formula
Input
x=\frac{-b+\sqrt{b^{2}-4ac}}{2a}
Output
______
/ 2
-b+√ b -4ac
x=-----------
2a
Cubic Formula
(I hope)
Input
x=\frac{\root{3}{-2b^{3}+9abc-27a^{2}d+\sqrt{4(-b^{2}+3ac)^{3}+(-2b^{3}+9abc-27a^{2}d)^{2}}}}{3\root{3}{2}a} - \frac{b}{3a} - \frac{\root{3}{2}(-b^{2}+3ac)}{3a\root{3}{-2b^{3}+9abc-27a^{2}d+\sqrt{4(-b^{2}+3ac)^{3}+(-2b^{3}+9abc-27a^{2}d)^{2}}}}
Output
3________________________________________________
/ ______________________________
/ 3 2 / 2 3 3 2 2 3_ 2
√ -2b +9abc-27a d+√ 4(-b +3ac) +(-2b +9abc-27a d) b √2(-b +3ac)
x=--------------------------------------------------- - -- - -----------------------------------------------------
3_ 3a 3________________________________________________
3√2a / ______________________________
/ 3 2 / 2 3 3 2 2
3a√ -2b +9abc-27a d+√ 4(-b +3ac) +(-2b +9abc-27a d)
Notes and Further Reading
Solutions have a recommended order of new again - feel free to change it back if you prefer best. If you want to play around some with LaTeX, try this online tool.
Got any cool challenge ideas? Submit them to /r/DailyProgrammer_Ideas!
4
u/Elite6809 1 1 Jun 05 '15 edited Jun 05 '15
I decided to go for an object-oriented Ruby solution. It's available here. It also supports integral signs (with a slightly different syntax than usual), for example:
Gets turned into:
And this:
Gets turned into:
It uses em-dashes rather than hyphens for the fraction line.
3
u/adrian17 1 4 Jun 05 '15
Python 3, also OOP. Quite ugly though.
Downsides:
_{}^{}(they are just drawn next to to each other)SqrtSymbolandRootSymbol).Code: https://gist.github.com/adrian17/4a25ebd482f32330c187
Outputs:
3
u/13467 1 1 Jun 05 '15 edited Jun 05 '15
I made a very tiny Ruby solution (60 lines):
Outputs are here.
2
u/Damiii99 Jun 06 '15
How am i able to output like this so nicely ? I don't even know how am i able to do that... Anyone may explain me ?
2
u/sir_JAmazon Jun 08 '15
C++ https://github.com/JonAmazon/LatexLite
This is the first difficult challenge I've done from Daily Programmer and would really love as much feedback as possible. The general structure of the program is to use an FSM parser to step through the input string and signal when it has found a new 'glyph.' The glyphs use a composition patterns where compound symbols can be generated recursively by adding child glyphs.
The renderer is just a large 2D character array that each of the glyphs is rastered to. Then this is flushed to a file to see the results.
3
u/super_pimms Jun 08 '15
C++ https://github.com/pimms/lowtex
Cool task! I really enjoyed solving this one :)
I solved this by first tokenizing the entire input sequence. Using stacks and whatnot, the token list is then used to generate a tree structure with all commands and raw string literals. This tree is then rendered recursively using dynamic 2D character arrays.
I did not figure out a way to properly add padding between elements, which causes the nasty output shown below. The solution also doesn't use the root-radical or pi-symbol. "V" and "pi" are used instead.
Edit: The nasty output in question happens in cases where one fraction is subtracted from another fraction.
Input:
Output:
Input:
Output:
2
u/lukz 2 0 Jun 06 '15 edited Jun 06 '15
vbscript in IE
I started on this challenge yesterday, but could not finish it. So today it is ready.
It handles stacking subscripts and superscripts
I format the roots a bit differently, not drawing the leg at 45 degree, but going straight up, like this:
Handles roots of arbitrary long powers
And here is the big expression
Code:
P.S. I also want to say this is a very nice challenge. And what is the idea of this being [practical exercise] instead of [hard]?
2
u/[deleted] Jun 10 '15
Hey guys. This challenge is a lot of fun! Definitely one of the more advanced things I've done so far. I'm stuck on two things, though. I'm trying to handle nested expressions as well as outputting the ASCII. I haven't put much time into the outputs so I bet I could figure it out given time. The nested expressions I'm stumped on. Could you take a look at my python 3 code and point me in the right direction? Thanks.
1
u/Kametrixom Jul 22 '15
There is almost the same challenge here: http://codegolf.stackexchange.com/questions/52314/poor-mans-latex