Ask Your Question
1

Present a LaTeX table in ask.sagemath.org's markdown

asked 2022-10-22 15:05:59 +0100

Emmanuel Charpentier gravatar image

updated 2023-07-07 08:49:22 +0100

FrédéricC gravatar image

This is more an HTML question than a Sage question, and specific to this site.

My problem is to present a table in (an anwer to) a question asked here. Sage's table objects' LaTeX representation use \tabular environment which, somehow, are not accepted by (the current version of) Markdown used here ; @slelievre suggestion to double backslashes do not work either.

@slelievre suggested here to use html representation instead, which only partially works : this representation lacks line breaks between table rows.

Adding <br> to each </tr> is not enough : lines are indeed separated, but each line is formatted separately, and header lines are lost.

This might be a bug in ask.sagemath.org supporting software...

Hints ?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2022-10-26 03:13:14 +0100

Juanjo gravatar image

updated 2022-10-30 16:33:06 +0100

It seems that tabular is not an environment supported by MathJax, as can be seen here. So, a possible solution is to convert the tabular environment to array. Assuming that all the cells are in math mode, this can be done by the function to_array defined as follows:

def to_array(tabular):
    return tabular.replace("tabular","array").replace("$","").replace(r"\\",r"\\\\")

Note that backslashes in the newline commands should be doubled so that Markdown can process them correctly. Let us test it using the table in the question asked here:

variables = flatten([[var(f"alpha_{i}_{j}", latex_name=fr"\alpha_{{{i} {j}}}") 
                    for j in [0..2]] for i in [0,1]])
table_object = table([[f(v) for v in variables] for f in [sin, cos, tan]],
               header_row=variables, header_column=[function("f"), sin, cos, tan])
tabular_env = latex(table_object)
print(to_array(tabular_env))

Now, one can copy the output and paste it in the text box to post or answer a question:

$$\begin{array}{l|llllll} f & {\alpha_{0 0}} & {\alpha_{0 1}} & {\alpha_{0 2}} & {\alpha_{1 0}} & {\alpha_{1 1}} & {\alpha_{1 2}} \\ \hline \sin & \sin\left({\alpha_{0 0}}\right) & \sin\left({\alpha_{0 1}}\right) & \sin\left({\alpha_{0 2}}\right) & \sin\left({\alpha_{1 0}}\right) & \sin\left({\alpha_{1 1}}\right) & \sin\left({\alpha_{1 2}}\right) \\ \cos & \cos\left({\alpha_{0 0}}\right) & \cos\left({\alpha_{0 1}}\right) & \cos\left({\alpha_{0 2}}\right) & \cos\left({\alpha_{1 0}}\right) & \cos\left({\alpha_{1 1}}\right) & \cos\left({\alpha_{1 2}}\right) \\ \tan & \tan\left({\alpha_{0 0}}\right) & \tan\left({\alpha_{0 1}}\right) & \tan\left({\alpha_{0 2}}\right) & \tan\left({\alpha_{1 0}}\right) & \tan\left({\alpha_{1 1}}\right) & \tan\left({\alpha_{1 2}}\right) \\ \end{array}$$

Edit. Corrected the header_column option.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-10-22 15:05:59 +0100

Seen: 135 times

Last updated: Oct 30 '22