Is it possible to color a string in a list ?
is there a way to color a string inside a list say
L = ["1","2","3"]
and I want to display 2 in red
If you're using an interface that supports LaTeX output, such as a Jupyter notebook:
sage: show(["1", LatexExpr(r"{\color{red} 2}"), "3"])
$$\displaystyle \left[1, {\color{red} 2}, 3\right]$$
To automate it, you could do e.g.
sage: show([LatexExpr(r"{\color{red} " + latex(x) + "}") if x == "2" else x for x in L])
or
sage: show([LatexExpr(r"{\color{red} " + latex(x) + "}") if k == 1 else x for k, x in enumerate(L)])
Asked: 2024-03-15 10:20:50 +0200
Seen: 157 times
Last updated: Mar 15