Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Сonverting expressions or strings to latex format #179

Closed
Iashenkovaias opened this issue Sep 24, 2023 · 5 comments
Closed

Сonverting expressions or strings to latex format #179

Iashenkovaias opened this issue Sep 24, 2023 · 5 comments
Assignees
Labels

Comments

@Iashenkovaias
Copy link

Сonverting expressions or strings to latex format

Yesterday I discovered your wonderful library and was inspired to use it in my code. However, I encountered the following problem: I work with linear equations and if I write them after return, the function works correctly. For example:

@latexify.expression
def find_latex(x):
  expr = -19*(4*x-11)
  return expr
print(str(find_latex))

This code works correctly and returns:
\begin{array}{l} expr = -{19} \left( {4} x - {11} \right) \\ expr \end{array}

But as soon as I try to take expression from the global environment, I recieve a problem. The code is as follows:

expr = -19*(4*x-11)
@latexify.expression
def find_latex(x, expr):
  return expr
print(str(find_latex))

Returns: expr

Perhaps I haven’t fully figured out how to use arguments in this function, but I would really like to use this library in my work.
I found issue (#108), but I also didn’t understand how to use arguments and showed methods to convert string expression to latex (this option is the most suitable for me).

I'am looking forward to your reply!

@odashi
Copy link
Collaborator

odashi commented Sep 24, 2023

@Iashenkovaias This is currently the expected behavior. @latexify.expression can compile only the expressions within the decorated function because it doesn't have ways to know the bound expression of the outer variables. It may require analyzing the whole source around the function, but the current implementation doesn't have such functionality.

Though it is not planned, I think the library may be able to provide some utilities to compile classes or modules with functions and other bound fields.

@Iashenkovaias
Copy link
Author

@odashi, thank you very much for your answer!
Perhaps you can tell me about your future plans with this library: would you add such functionality to latexify as reading strings and then generating expression in latex format? In my opinion, such function could be extremely popular :)

@odashi
Copy link
Collaborator

odashi commented Sep 24, 2023

Reading strings and then generating expression

It could be provided with some effort. However, Obtaining the "correct" expression of functions that depends on outer variables (I guess this is the topic of this issue) is somewhat not possible because the value of such variables is basically indeterminate.

Example:

c = 42

def fn(x):
  return x * c

def change_c(new_c):
  global c
  c = new_c

In this case f(10) returns 420 if change_c has never been invoked, but latexify couldn't determine such semantics by only looking at the def clause of fn and even the whole content of this source. I think this is one of the obvious limitation of this library (and similar tools based on the syntax tree).

@Iashenkovaias
Copy link
Author

@odashi, once again thank you very much for your answer! I understand you, I’ll try to apply method you described in my project.
Also, I apologize for my lack of understanding features of the library: I recently took up programming more or less seriously, so some aspects are new for me
All the best :)

@odashi
Copy link
Collaborator

odashi commented Sep 25, 2023

@Iashenkovaias No problem! If you get some ideas to work around this, please re-open the issue and let me know about your thoughts.

@odashi odashi closed this as completed Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants