# Colors in table

This is a nice table with the help of two of you (many thanks).

A = [ * 10,  +  * 9, [11.1] * 9 + ]
A.extend([ * 5 +  * 5, [50/2^n for n in range(10)]])
D = deepcopy(A)  # copie A dans D en conservant A intacte
# on arrondit uniquement les lignes qui en ont besoin
D = list(matrix(RDF, A).round(3))
D = list(matrix(RDF, A).round(3))
# on construit la table
hr = [r"${\color{red} n}$" for n in (1 .. 10)]
hc = ["", r"${\definecolor{energy}{RGB}{200, 8, 21}\color{energy}\text{Équirépartition}}$", r"${\definecolor{energy}{RGB}{200, 8, 21}\color{energy}\text{Tout pour un}}$", r"${\definecolor{energy}{RGB}{200, 8, 21}\color{energy}\text{Un déshérité}}$"]
hc += [r"${\definecolor{energy}{RGB}{200, 8, 21}\color{energy}\text{Injustice pour 1/2 population}}$", r"${\definecolor{energy}{RGB}{200, 8, 21}\color{energy}\text{Injustice croissante}}$"]
show(t)


I added colors in the header row. But adding them in the header column does not work since the elements of the header need to be formatted. So what can I do? And if I want some background colors in the cells and change the colors of the cell's fonts?

edit retag close merge delete

I don't know how to get \definecolor... to work, but you need to put braces {} around the text to be colored, as in \color{blue}{\text{...}}. (Note the "{" immediately before "\text".)

Sort by » oldest newest most voted

Presumably this takes place in a browser?

Maybe in Jupyter or in SageCell?

The following works in these contexts.

Making it work in the Sage REPL or in SageTeX would require some changes.

Not sure that fully answers your question, but hopefully it is a step.

A = [ * 10,  +  * 9, [100/9] * 9 + ]
A.extend([ * 5 +  * 5, [51200/1023/2^n for n in range(10)]])
f = lambda x: x if x in ZZ else round(x, 4)
D = [[f(a) for a in r] for r in A]
latex_req = r"$\require{color}$"
latex_def = r"$\definecolor{energy}{RGB}{200, 8, 21}$"
box = lambda t: r"$\color{energy}{\colorbox{yellow}" + fr"{{{t}}}" + "}$"
hr = [r"$\color{red}" + f"{{{n}}}$" for n in (1 .. 10)]
cc = ["Équirépartition", "Tout pour un", "Un déshérité"]
cc += ["Injustice pour 1/2 population", "Injustice croissante"]
hc = [latex_req + latex_def] + [box(c) for c in cc]
t


Illustration

Inspired by the answer to this previous question:

more