3

I'm from another Stack Exchange site where we've just had MathJax enabled. There's a table in this answer that looks like this (actual decimals just typed in for the example), which looks rather pretty, except the rows and columns are unlabelled:

\begin{array}{r|lll}
  & 0 & 1 & 2 \\
\hline
0 & 0.125 & 0.250 & 0.168 \\
1 & 0.125 & 0.250 & 0.168 \\
2 & 0.125 & 0.250 & 0.168
\end{array}

If I want to label the headings and columns, my attempts go awry due to stretching out the first column:

\begin{array}{r|lll}
& \text{number of foo} \\
\text{number of bar} & 0 & 1 & 2 \\
\hline

... clearly that's not the way to do it. Googling has turned up \multicolumn, but that's a thing for tables which aren't available in the Stack's MathJax.

I've considered I could just use a bunch of spacing to finagle the label into just the right position. It looks nice, but I don't know if there's a way I "should" be doing it or could bear in mind for the future:

\,\,\quad\qquad\qquad \text{number of foo} \rightarrow \\
\begin{array}{r|lll}
\text{number of bar}  & 0 & 1 & 2 \\
\hline

Is there an appropriate, robust and good-looking way to place a label across a set of columns? I'm open to using an environment other than array, if it does the job of displaying that data in a tabular form and works within our Stack Exchange MathJax environment.

closed as off-topic by Mico, egreg, Maarten Dhondt, Stefan Pinnow, Frank Mittelbach Jun 8 '16 at 6:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not fall within the scope of TeX, LaTeX or related typesetting systems as defined in the help center." – Mico, egreg, Maarten Dhondt, Stefan Pinnow, Frank Mittelbach
If this question can be reworded to fit the rules in the help center, please edit the question.

  • As your posting appears to be about MathJax rather than TeX, LaTeX, and friends, it may be off-topic for this site. MathJaX has syntax elements that are borrowed from LaTeX, but it uses a completely different rendering engine. – Mico Jun 8 '16 at 5:37
  • @MIco Oh, thanks, now I see the last part of that tag's summary. Thanks for letting me know. – doppelgreener Jun 8 '16 at 5:38
6

You can put the column labels inside a right overlap, which effectively puts it inside a zero-width box that is left-aligned:

$$
  \begin{array}{r|lll}
    & \rlap{\text{number of foo}} \\
    \text{number of bar} & 0 & 1 & 2 \\
    \hline
    0 & 0.125 & 0.250 & 0.168 \\
    1 & 0.125 & 0.250 & 0.168 \\
    2 & 0.125 & 0.250 & 0.168
  \end{array}
$$

Here is the output on Math.SE:

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.