5 ms·
Show HN: AlexCalc, a scientific calculator with LaTeX equation display
- axby 5y agoHi all, as a hobby I've been developing a scientific calculator with some neat features. Available on: * web (mobile or desktop): https://alexbarry.github.io/AlexCalc/ https://alexbarry.github.io/AlexCalc/ * Android: https://play.google.com/store/apps/details?id=net.alexbarry.calc_android https://play.google.com/store/apps/details?id=net.alexbarry.... Some neat features: * LaTeX equation display (I hated counting brackets on my old graphing calculators back in college, now I wouldn't have to if I wasn't ~10 years late in making this) * variables (you can store variables with `1 + 2 -> x`, then access them `x^2 - 3`) * unit operations, and unit conversions, e.g. `10 km / 2 minutes to mph`, or more fancily: `sqrt((3 kOhm + j*100 mH * 1000 kHz)/(1 S + j * 1 nF * 1000 kHz))` * complex numbers (specifically focused on making it easy to enter complex numbers in polar form, in degrees, e.g. `5 angle 90`, but press the "degree" button) * can enter input either via button presses or typing, so copying and pasting inputs works well The core calculator parsing, evaluating, and LaTeX generation is in C++, with CMake build files. Using emscripten[0], this compiles to webassembly for the web version, and I can also compile this for Android and use JNI. The library converting my generated LaTeX code to graphics is "MathJax"[1], a really nice browser library that I loved working with both in the web version and in the Android WebView. I'm happy to receive any feedback at all. One big weakness currently are the syntax error messages, I haven't yet implemented a way to point to a particular position where an error occurs. If there is some nice parsing library that I could use instead, I might consider switching to that if it isn't too difficult. Currently the parsing uses a bunch of (fairly simple) regular expressions (e.g. check for number, check for optional unit, check for binary operator, add to stack, collapse stack. This ends up with a tree of nodes, and then this tree can either be evaluated or converted to LaTeX.) I'm also interested in hearing any strategies for a decent cross platform UI. I didn't want to just include the HTML UI in a WebView on Android, since I figured it wouldn't be a great user experience. Originally I thought it would be small enough that I could just write two separate copies... but it turns out that there is a lot more UI code than I expected (automatically insert multiplication symbols on button presses, input history, storing "recently used units" when the button is pressed, etc...) [0]: https://emscripten.org/ https://emscripten.org/ [1]: https://www.mathjax.org/ https://www.mathjax.org/
- enriquto 5y agoHey, it looks great, I love it! Would it be feasible to support unicode variables? Having to spell the greek letters seems a bit clunky. If I type them directly it gives a parsing error. Notice that in modern LaTeX (say, xetex or luatex) you can do that. No need to type \'e to get é like we did in the nineties.
- axby 5y agoThanks a lot for trying it and the feedback! Assuming C++ standard regex supports unicode, then this should be easy enough. I never thought to try this. I'll see if I can implement it later today or this weekend. Do you think the spelled out "alpha" should map to the same thing as "<unicode alpha character>"? Also, do you mind if I ask how you are entering Greek letters directly? (e.g. are you a Greek speaker, using a Greek keyboard?) I'd like to better understand how people would like to use this. I'm happy to accommodate whatever I can, and I'm relying on people asking for it directly since I don't have any sort of tracking implemented. (The android version doesn't even request any INTERNET permission (or any permission).)
- enriquto 5y ago> Also, do you mind if I ask how you are entering Greek letters directly? I've setup my keyboard (on linux) so that the CAPS-LOCK key acts as a "dead greek" composer. If I press "CAPSLOCK+a" it types "α", and so on. > Do you think the spelled out "alpha" should map to the same thing as "<unicode alpha character>"? My unpopular opinion is that typing "alpha" should compute the product of the five values, equivalent to "a^2lph".
- axby 5y ago> I've setup my keyboard (on linux) so that the CAPS-LOCK key acts as a "dead greek" composer. If I press "CAPSLOCK+a" it types "α", and so on. Cool! Is this because you type a lot of Greek (or math)? > My unpopular opinion is that typing "alpha" should compute the product of the five values, equivalent to "a^2lph". I wouldn't call it an unpopular opinion, that's how my physical graphing calculator worked, though it only had single letter variable names. The programmer in me likes my current way, though the (nearly long gone) engineering student cringes when I have to write things like "i*2*pi*f". If people were mostly universally happy with single letter variable names then I could probably switch to this. If they were space separated then it would be a fairly simple change (except for the way units works, is "1 s" 1 times variable s, or 1 second? This is why I don't support 1 m/s to mean metre per second, it means "1 metre divided by variable s". 1 metre per second is "1 m s^-1"). I tried to compromise by making the UI automatically enter a multiplication symbol when entering tokens that aren't valid. Perhaps if I added an additional input layer when typing then this would be feasible. Maybe something like pressing a special character, then typing "alpha" will convert it to the alpha character, and non greek letter variables could be wrapped in quotes or square brackets or something. The one thing that would be lost is the ability to name a variable "x2". Maybe that's fine since "x_2" would still work. As an update on unicode parsing, I still think it should be easy, but I'm running into issues. I quickly tried https://stackoverflow.com/a/11255698/9596600 https://stackoverflow.com/a/11255698/9596600 but I'm seeing this: terminate called after throwing an instance of 'std::regex_error' what(): Unexpected escape character. I suspect I just need to dig in on how to use these extended regexes (and remove some of my existing regexes). This weekend I might have more time to try it.
- kragen 5y agoHey, this is great! Units and LaTeX formatting are two things I use a lot.
- axby 5y agoThanks! If you right click on any LaTeX you can access the raw TeX code. Admittedly this is just the default MathJax behaviour though haha. If people wanted it then I could probably make it easier to get the TeX in fewer clicks. I thought that it might be useful to use this application to generate TeX, so one feature I'm working on is to fine tune the output format (e.g. choosing the multiplication symbol, instead of \cdot, maybe \times or \,). Thanks for trying it!
- axby 5y agoTL;DR: try pressing the "vars" and "units" buttons. Also complex numbers by "i" button or pressing "alt" then the now shown "angle" button. For choosing the desired output unit, press "alt" and the now shown "to units" button. Then enter the desired unit.
- fish45 5y agoNice project. It's pretty similar to my project CalcuLaTeX (https://calcula.tech https://calcula.tech) although I think the intended usecase is slightly different
- axby 5y agoThanks! And that is so cool! It looks like the intended use case of this is to generate documents or short snippets of LaTeX, but not have to hard code the answers? I wish I had something like this back when I actually wrote reports and stuff in LaTeX. Now I barely have any use case for my own calculator, except occasional unit conversions. The use case that I had in mind for mine was electrical engineering students, where I had to often use complex numbers in polar form, in degrees, for circuit questions. My calculator at the time had no obvious way of entering polar complex numbers in degrees, I ended up typing things like `e^(i*angle/1r)`, where the r was "radian", and when the calculator was in degrees mode, "1 r" would evaluate to "180/pi". It was also cumbersome to write long expressions for. It showed something like 24 characters per line, enough that I'd often get multi line expressions. These expressions would be full of brackets, and sometimes I'd hit enter and not be sure if I missed a bracket. (Hence my goal of having a LaTeX display to show you what you're entering).
- fish45 5y agoYeah, I made it to do my chemistry homework where a lot of problems were duplicated with different numbers. The truth is that it's not well tested outside of that though so it's hard to trust for complicated stuff. I might end up using yours :)
- froh 5y agoSweet:) Nitpick: in dark theme, the fraction line didn't invert to white in 1/sqrt(2)
- axby 5y agoThanks for letting me know, is this in the web version? If so, what browser? I tried Chrome and Firefox and I don't see this issue. I use a plugin to make sites dark ("Dark Reader") and it seems to work for my web calculator in Firefox. Or is it Android? On my phone 1/sqrt(2) looks fine in "dark" and "very dark". If it doesn't for you, could you tell me what type of phone you have? I put some contact info in the "about" section of the app if you'd rather respond privately. For dark mode on Android I just set the color and background-color attributes of the HTML display elements. (I didn't actually fully implement dark mode in the HTML version, though there are some commented out CSS files). But it's possible that something else is needed to work with MathJax in some cases.
- froh 5y agoChrome on Android, web version, the phone is set to dark mode, the browser inherits it. Pixel 5.
- axby 5y agoThanks a lot! I see now, I am able to reproduce the issue if I go into chrome://flags, search for "Auto Dark Mode for Web Contents", and select "Enabled with simple HSL-based inversion". Just choosing "Enabled" seemed to break the LaTeX entirely. (Please let me know if that isn't roughly what you did) I don't know how I'll fix this (I've never really debugged mobile web specific stuff before), but I'll look into it this weekend. There must be a way to fix it, since I don't see the issue when going to https://www.mathjax.org/#demo https://www.mathjax.org/#demo and entering something like this: $$\frac{1}{\sqrt{2}}$$ EDIT: I actually have no idea why this would be happening, unless there is some sort of metadata that I should be providing on the page to tell chrome how to enable dark mode on my content. I don't think I'm messing with the styling on the web version at all.