mathnet / mathnet-symbolics

Math.NET Symbolics
http://symbolics.mathdotnet.com
MIT License
349 stars 66 forks source link

Latex #88

Closed troelschristensen closed 3 years ago

troelschristensen commented 3 years ago

Hi.

I'm writing my code in visual basic. I can get the code to work, when trying to simplify algebra strings. This works. But I can't get it to stop simplifying/reducing, when "converting" to latex. Is there any way to make an expression (like Infix.ParseOrThrow("2/5q+2/5q"), that not gets simplifyed (to 4/5*q)?

Eg. Dim str = "2/5q+2/5q" Dim str_latex = LaTeX.Format(str ) *!! I know this does not work!! Dim str_simplify = Infix.ParseOrThrow(str) Dim str_simplify_latex = LaTeX.Format(str_simplify )

dim str_new = str_latex & " = " & str_simplify_latex

I know that this does not work, but it explains, what I'm trying to do. I dont always want it to simplify the expression, but can't do LaTeX.Format(str ).

If I use the str_simplify_latex, then it shows ' 4/5*q But I really want it to show \frac{2}{5}q+\frac{2}{5}q

Can anyone help me? Thx.

FoggyFinder commented 3 years ago

I think it's impossible currently (unsure since I didn't use the library for a while)

But this feature is planned, see

https://github.com/mathnet/mathnet-symbolics/issues/79#issuecomment-716107858

for details.

troelschristensen commented 3 years ago

Thank you @FoggyFinder.👍 Ok, then I must wait for it to be added. Could be so great.👌🙌

Sent with GitHawk

FoggyFinder commented 3 years ago

@troelschristensen actually there is support for it already.

You can use Infix.parseVisual function to create VisualExpression and turn it to the latex via LaTeX.formatVisual.

Sample:

open MathNet.Symbolics

let visual = Infix.parseVisual "2/5 * q + 2/5 * q"

let latex = 
    visual
    |> Result.map(fun ve -> LaTeX.formatVisual ve)

match latex with
| Result.Ok l -> printfn "Ok: %s" l
| Result.Error err -> printfn "Error: %s" err

Output

Ok: \frac{2}{5}q + \frac{2}{5}q
troelschristensen commented 3 years ago

Thank you, @FoggyFinder .

Sadly I don't understand F# and therefore can't try your code. Do you by any chance think that you could write this in vb.net (or c#) ?

I would be very gratefull. This would really be of great help.

I tried this, but this is not working: Dim strvisual = Infix.ParseVisual("2/5 q + 2/5 q") Dim strlatex = LaTeX.FormatVisual(strvisual)

It says error (LaTeX.FormatVisual(strvisual)) Value of type 'FSharpResult(Of VisualExpression, String)' cannot be converted to 'VisualExpression'.

FoggyFinder commented 3 years ago

Sure

Imports MathNet.Symbolics
Module Program
    Sub Main(args As String())
        Dim visual = Infix.ParseVisual("2/5 * q + 2/5 * q")
        If visual.IsOk Then
            Dim strlatex = LaTeX.FormatVisual(visual.ResultValue)
            Console.WriteLine(strlatex)
        End If
    End Sub
End Module
troelschristensen commented 3 years ago

GREAT @FoggyFinder !! Now it works. Thank you for your help.

Only thing I can't get to "work" now is for the multiplication sign to be shown on some occasions.

Eg.

2( 3 +3x) displays 2(3+3x) I would like it to display 2*(3+3x) or 2×(3+3x) or 2⋅(3+3x)

My users on my website can choose between these 3 types of multiplication signs ( *, × and ⋅).

Can this be done? Can I somehow define a symbol in the string, that the latex.formatvisual function skips?

FoggyFinder commented 3 years ago

Can I somehow define a symbol in the string, that the latex.formatvisual function skips ?

I'd suggest creating a separate issue for this.

You could try to copy the part of code that produces the output from the source and change it appropriately.

troelschristensen commented 3 years ago

Ok, thanks @FoggyFinder .

I'm not that experienced. That would be way over my head. :) But I close the issue and mark as solved. Starting a new issue after that.