0

I am trying to create a list in Python wherein the elements are Latex entries. I have the following code snippet. I have called "from IPython.display import display, Math, Latex".

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

I get ['$a_1$', '$a_2$', '$a_3$', '$a_4$'], but the elements are not "Latexed". Is there something I am missing?

Nanda
  • 361
  • 1
  • 5
  • 14

1 Answers1

2

I believe this can help: How to write LaTeX in IPython Notebook?

Specifically to your problem: you are trying to display a list of latex-formatted strings, which its not a latex formatted string itself; a possible workaround could be something like:

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))) 

Now it displays a <IPython.core.display.Math object>, instead of a list. Of course, how to format the list (i.e. what __str__ does) is up to you to decide!

  • 1
    Makes sense. Thank you! A minor comment: it's better to replace $a_1$ with $$a_1$$. That gives the subscripts correctly. I think '$a_1$' is not recognized. – Nanda Oct 25 '21 at 08:58