latexify_py
latexify_py copied to clipboard
Supporting Set operations and comprehensions
It would be nice to support some more set operations and comprehensions, since they're pretty easy to use in python. (I assume this is going to be fairly difficult though, since it requires a fair amount of information from the python ast)
For example, this code:
@latexify.with_latex
def solve():
return {x ** 2 for x in range(1, 10)}
Returns this currently:
\displaystyle \mathrm{solve}() \triangleq <ast.SetComp object at 0x7f6920238160>
It would be nice to show something like this:
\displaystyle \mathrm{solve}() \triangleq \{x\mid x^2\in\Bbb \(1..10\)\}
$$ \displaystyle \mathrm{solve}() \triangleq {x\mid x^2\in\Bbb (1..10)} $$
And operations on sets:
@latexify.with_latex
def solve(x: set, y: set):
return x | y
Returns this currently:
\displaystyle \mathrm{solve}(x, y) \triangleq \mathrm{unknown\_binop}(x, y)
It could be something like this:
\displaystyle \mathrm{solve}(x, y) \triangleq x \cup y
$$ \displaystyle \mathrm{solve}(x, y) \triangleq x \cup y $$
in
and not in
could also be supported, like x in A
or x not in A
like this:
x\in A, x\notin A
$$ \displaystyle \mathrm{solve}() \triangleq x\in A $$
$$ \displaystyle \mathrm{solve}() \triangleq x\notin A $$
Tahnks! I think this is reasonable proposal.
To clarify, this includes 3 distinct features as follows:
- [x] Support comprehension:
{f(x) for x in X}
-> $\{f(x) | x \in X\}$ --> #84 - [x] Support binary operators on sets:
x | y
-> $x \cup y$ --> #94 - [x] Support affiliation operators:
x in y
-> $x \in y$ --> #61
We need to take care about the second case because set operations shares the same syntax with bit operations and it is not essentially distinguishable by the syntax tree. We may need something like:
- Add argument to tell which set/bit operations are preferred
- Rely on typing information (this is stretchy, not applicable for now)
I will work on this issue this week. As for comprehension, we can support it similarly to that of sum/prod
operations. Since binary/set operations are not trivial to distinguish, it may be good to provide a flag to switch these operators by users.
All are now supported. Since it is hard to disambiguate set/bit operations, it is now controlled by a flag.