【问题标题】:Latex expressions in lists列表中的乳胶表达式
【发布时间】:2021-12-10 16:52:14
【问题描述】:

我正在尝试在 Python 中创建一个列表,其中元素是 Latex 条目。我有以下代码sn-p。我已经调用了“从 IPython.display 导入显示、数学、乳胶”。

lst = [r'$a_1$', r'$a_2$', r'$a_3$', r'$a_4$']
print(lst)

我得到 ['$a_1$', '$a_2$', '$a_3$', '$a_4$'],但元素不是“Latexed”。有什么我遗漏的吗?

【问题讨论】:

    标签: python latex ipython


    【解决方案1】:

    我相信这会有所帮助:How to write LaTeX in IPython Notebook?

    特别针对您的问题:您正在尝试显示乳胶格式字符串的 list,其 不是 乳胶格式字符串本身;一个可能的解决方法可能是这样的:

    from IPython.display import display, Math, Latex
    
    class LatexList:
    
        def __init__(self, lst) :
            self.lst = lst
    
        def __str__(self) :
            fl = "["
            for exp in self.lst :
                fl += exp+","
            fl += "]"
            return fl
    
    latex_list = LatexList([r'$a_1$', r'$a_2$', r'$a_3$', r'$a_4$'])
    
    display(Math(str(latex_list))) 
    

    现在它显示<IPython.core.display.Math object>,而不是列表。当然,如何格式化列表(即__str__ 做什么)由您决定!

    【讨论】:

    • 有道理。谢谢!一个小评论:最好用 $$a_1$$ 替换 $a_1$。这正确地给出了下标。我认为 '$a_1$' 无法识别。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 2023-03-13
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多