在ipython notebook中的sympy输出中使用符号作为乘法运算符

时间:2021-04-27 21:24:32

In ipython notebook I would like to be able to use a symbol (\times or \cdot) for the multiplication operator in the latex/mathjax output of an expression. Is this possible?

在ipython笔记本中,我希望能够在表达式的latex / mathjax输出中为乘法运算符使用符号(\ times或\ cdot)。这可能吗?

sympy.latex() has an option mul_symbol but I can't see how to use it to change the latex in an output cell.

sympy.latex()有一个选项mul_symbol但是我看不到如何用它来改变输出单元格中的乳胶。

3 个解决方案

#1


2  

Try the following code:

请尝试以下代码:

from IPython.display import display, Math
from sympy import *
x1 = var('x_1')
x2 = var('x_2')
display(Math(latex(x1*x2,mul_symbol='dot')))

You can change dot to times or ldot.

您可以将点更改为时间或ldot。

This makes IPython render the latex using the desired symbol.

这使得IPython使用所需的符号渲染乳胶。

If you don't want that last line messing up all your code just define a function:

如果您不希望最后一行弄乱所有代码,只需定义一个函数:

def Latex(expr):
    return display(Math(latex(expr,mul_symbol='dot')))

and now you can just use:

现在你可以使用:

Latex(x1*x2)

#2


2  

init_printing() has an argument latex_printer, so you can do something like

init_printing()有一个参数latex_printer,所以你可以做类似的事情

init_printing(latex_printer=lambda *args, **kwargs: latex(*args, mul_symbol='dot', **kwargs))

#3


0  

I have a function I keep in a library to create Latex printers:

我有一个函数,我保留在库中创建Latex打印机:

def makeLatexPrinter(**localsets):
    def mylatex(expr,**settings):
        settings.update(localsets)
        return latex(expr,**settings)
    return mylatex

Then in my notebook I use:

然后在我的笔记本中我使用:

init_printing(use_latex=True,latex_printer=makeLatexPrinter(mul_symbol='dot'))

It would be cleaner if init_printing() passed keyword arguments like mul_symbol through itself, though.

如果init_printing()通过自身传递像mul_symbol这样的关键字参数会更清晰。

#1


2  

Try the following code:

请尝试以下代码:

from IPython.display import display, Math
from sympy import *
x1 = var('x_1')
x2 = var('x_2')
display(Math(latex(x1*x2,mul_symbol='dot')))

You can change dot to times or ldot.

您可以将点更改为时间或ldot。

This makes IPython render the latex using the desired symbol.

这使得IPython使用所需的符号渲染乳胶。

If you don't want that last line messing up all your code just define a function:

如果您不希望最后一行弄乱所有代码,只需定义一个函数:

def Latex(expr):
    return display(Math(latex(expr,mul_symbol='dot')))

and now you can just use:

现在你可以使用:

Latex(x1*x2)

#2


2  

init_printing() has an argument latex_printer, so you can do something like

init_printing()有一个参数latex_printer,所以你可以做类似的事情

init_printing(latex_printer=lambda *args, **kwargs: latex(*args, mul_symbol='dot', **kwargs))

#3


0  

I have a function I keep in a library to create Latex printers:

我有一个函数,我保留在库中创建Latex打印机:

def makeLatexPrinter(**localsets):
    def mylatex(expr,**settings):
        settings.update(localsets)
        return latex(expr,**settings)
    return mylatex

Then in my notebook I use:

然后在我的笔记本中我使用:

init_printing(use_latex=True,latex_printer=makeLatexPrinter(mul_symbol='dot'))

It would be cleaner if init_printing() passed keyword arguments like mul_symbol through itself, though.

如果init_printing()通过自身传递像mul_symbol这样的关键字参数会更清晰。