Code Monkey home page Code Monkey logo

calculatex's Introduction

CalcuLaTeX

Test Status Crates.io

Try it out at https://calcula.tech/app

Example

'''
Given a rock with mass $m_{rock}$ thrown by a force of $F_0$ acting over a duration $dt_{force}$,
calculate its airtime and the max height it achieves.
'''

''' Given: '''
g = 9.81 m/s^2
m_{rock} = 10 kg
F_0 = 1000 N
dt_{force} = 0.1 s


''' Solution: '''
y_{vel} = F_0 / m_{rock} * dt_{force} = ?

airtime = y_{vel} / g * 2 = ?
height_{max} = y_{vel} * airtime / 4 = ? cm

Outputs:

example 1

''' 
CalcuLaTeX:

Write an expression followed by = ? to evaluate it:
'''

50 g + 1 kg = ?

'''
You can suggest a unit if it outputs the wrong one. \\
Make sure it's correct otherwise the document won't \\
compile!
'''

5 m/s^2 * 3 kg = ?
5 m/s^2 * 3 kg = ? N

'''
Assign variables with =

They support some LaTeX formatting
'''
a_b = 5
c = 10 kg
a_b * c = ?
z = a_b kg + c = ?

'''
You can also add aliases for hard to type variables
'''
alias dx \frac{dx}{dt}
alias dt \Delta{t}

dx = 10 m/s
dt = 14 days
x = dx * dt = ?

'''
Set the output digits or use scientific notation \\
by using !digits \{n\} or !scientific 
'''

1 / 3 = ?

!digits 10
1 / 3 = ?

!digits 1
!scientific
1 kg = ? mg

'''
Basic mathematical functions are supported.

Additionally, builtin constants include $\pi$ and $e$.
'''

sin(\pi) = ?
cos(\pi) = ?
e = ?

'''
Escape raw latex with three single quotes.

There's still plenty of bugs, so feel free to \\
open an issue at \\
https://github.com/mkhan45/CalcuLaTeX
'''

Outputs:

tutorial


CLI Usage

Assuming the binary is called calculatex, running calculatex [input] [output.pdf] will watch the input file and output to output.pdf on change. This is meant for use with a PDF viewer which updates in realtime.

To run the CalcuLaTeX cli, you'll also need pandoc and a texlive installation.


Compiling

To build CalcuLaTeX, you need a nightly Rust compiler and Cargo, preferably through rustup. After that, it's as simple as:

git clone [email protected]:mkhan45/CalcuLaTeX.git
cd CalcuLaTeX

# both of these output to target/release/
cargo build --release # builds the library only
cargo build --features build-binary --release # builds the CLI

Contributing

If you'd like to contribute, feel free to open an issue or send me an email. The code base is very messy right now but it's small enough to be pretty understandable. Performance suggestions are welcome, but only if they don't introduce any extra complexity. I haven't benchmarked anything but LaTeX compilation is pretty much guaranteed to take an order of magnitude longer than the interpreter.

For guidelines, read CONTRIBUTING.md

calculatex's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

calculatex's Issues

Redo error handling

Remove Other, make proper descriptive enum variants for each type of error.

Add a Located<T> error wrapper or something for the line number

Non math statements

  1. Display comments
    • Probably prefixed with a #, I think it should just be plain latex so latex does all the hard stuff
  2. Non display comments
    • Prefixed with //? Just code comments
  3. Formatting directives
    • The main thing is that it should be possible to put multiple statements on the same line.

Smart parenthesis placement

Parentheses are currently erased during parsing and added back later between any two added/subtracted terms. With exponentiation I think they might even have to be added between multiplied/divided terms to be correct. There should be a smarter way to add them.

Redo `Val` arithmetic

In Val, num should be constrained to [-10, 10]. This way, it becomes much easier to handle Add and Sub impls.

Tests failing

main.rs/tests::test_basic fails at this assertion

        assert_eq!(
            full_eval("5 kilograms + 4 grams").to_string(),
            "5004 g".to_string()
        );

With the following message:

thread 'tests::test_basic' panicked at 'assertion failed: `(left == right)`
  left: `"9000 g"`,
 right: `"5004 g"`', src/main.rs:57:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Use as a pandoc filter plugin

It would be really awesome to be able to use CalcuLaTeX as a pandoc filter plugin.

A document might look something like this:

Given a rock with mass $m_{rock}$ thrown by a force of $F_0$ acting over a duration $dt_{force}$,
calculate its airtime and the max height it achieves.

Given:

~~~ {calculatex #eq:facts}
g = 9.81 m/s^2
m_{rock} = 10 kg
F_0 = 1000 N
dt_{force} = 0.1 s
~~~

Solution:

~~~ {calculatex #eq:velocity}
y_{vel} = F_0 / m_{rock} * dt_{force} = ?
~~~
~~~ {calculatex #eq:airtime}
airtime = y_{vel} / g * 2 = ?
~~~
~~~ {calculatex #eq:heightmax}
height_{max} = y_{vel} * airtime / 4 = ? cm
~~~

and the output would essentially be this:

Given a rock with mass $m_{rock}$ thrown by a force of $F_0$ acting over a duration $dt_{force}$,
calculate its airtime and the max height it achieves.

Given:

$$
g = 9.81 m/s^2
m_{rock} = 10 kg
F_0 = 1000 N
dt_{force} = 0.1 s
$$ {#eq:facts}

Solution:

$$
y_{vel} = F_0 / m_{rock} * dt_{force} = 10 m/s
$$ {#eq:velocity}

$$
airtime = y_{vel} / g * 2 = 2.039 s
$$ {#eq:airtime}

$$
height_{max} = y_{vel} * airtime / 4 = 5.097 m
$$  {#eq:heightmax}

Passing additional code block classes through to the generated equation block enables compatibility with pandoc-crossref, too.

It'd be assumed that the calculatex block creates one environment per document. However, I'll bet you could add some kind of class with a key-value attribute that can assign calculatex code blocks to a certain environment or clear the state of the default.

derived unit coercion

LaTeX output should be able to show derived units. The print expression should then look like

expr = ? (Unit1, Unit2)

100 m/s = 0 km/s

100 m/s = ? m/s - correctly outputs 100 m/s
100 m/s = ? m/hour - correctly outputs 360 km/hour

This is likely a LaTeX output bug that has something to do with integer division

Variable aliases

CalcuLaTeX supports declaring a variable using latex syntax, e.g. m_0 and \frac{x}{y} are valid variable names and will be rendered properly, but they're annoying to type. It should be possible to alias a variable to LaTeX.

This is probably related to #2 since I'd imagine we use a separate statement to set a variable's display LaTeX

Demo website's output is unhighlightable

It seems like the demo is constantly regenerating the output. Leading to the text being unhighlightable. It would be better if you only generated onBlur or maybe on keyUp.

Compiling/usage informaion in README

Would it be possible to add to the README (or elsewhere) information about how to compile and run this program for those who aren't familiar, and what libraries / tools are required to do so? Thank you!

Truth table generator

This is super long term since CalcuLaTeX doesn't even support booleans yet, but I wanted to write out a general plan.

Basically, user defined functions need to have an option to inline themselves in the LaTeX. We also need LaTeX string interpolation to work. This way, truth tables can be implemented as just syntax sugar and just expand to a table.

What is the rendener of math?

Thanks for this interesting work.
I'm curious about how you render the math.
Do you use some external library like pandoc? Or it's implement in the code?

Unit typesetting

According to standards, units must be typeset in upright font. Consider typesetting units using \mathrm{} or even better, siunitx package if possible.

See for example SI Brochure:

Unit symbols are written using roman (upright) type, regardless of the type used in the surrounding text

Derived unit multiplicities

Some units, e.g. Newtons, include a multiplicity (kilograms). To fix this, I think UnitPow should be the default everywhere

CalcuLaTeX-Web Crashes

Reproduce

Change x = 5 under "Assign variables with =" to x = 5 kg or change y = 10 kg to y = 10, boom!

Proper CLI

Right now cargo run [input] [output.pdf] watches the pdf and updates it on file change. The default should probably be just a one time compile, and the --watch option should result in the current behavior. I'm not sure if something as full featured as clap is fully necessary and I'd like to keep binary size as small as possible, but I'd like to use a solid library instead of doing it manually.

Float exponentiation

num can still be plainly exponentiated
the Unit desc is a bit harder but it already supports rationals
I'm not sure what to do about Unit exp.
I guess mult should also be plainly exponentiated

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.