8

I have a table such as the one below

\begin{tiny}\begin{tabular}{|l|c|c|c|}
\hline
&\textbf{ciao}&\textbf{ciao2}&\textbf{ciao3}\\\hline
\textbf{ciao}&0.815&0.913&0.278\\\hline
\textbf{ciao2}&0.906&0.632&0.547\\\hline
\textbf{ciao3}&0.127&0.098&0.958\\\hline
\end{tabular}
\end{tiny}

How can I highlight the diagonal, the first row and the first column with different colours?

  • Package colortable or \usepackage[table]{xcolor} and \cellcolor command and \begin{tiny}...\end{tiny} does work, but it's 'wrong' –  Dec 21 '15 at 21:52
  • I can't understand how this can be considered a duplicate of http://tex.stackexchange.com/questions/69713/matrix-change-row-or-column-background – egreg Dec 22 '15 at 10:08

3 Answers3

7

Easy with colortbl (loaded via xcolor):

\documentclass{article}
\usepackage[table]{xcolor}

\begin{document}

\begin{tabular}{|>{\columncolor{red!20}}l|c|c|c|}
\hline
\rowcolor{blue!20}%
&\textbf{ciao}&\textbf{ciao2}&\textbf{ciao3}\\\hline
\textbf{ciao}&\cellcolor{green}0.815&0.913&0.278\\\hline
\textbf{ciao2}&0.906&\cellcolor{green}0.632&0.547\\\hline
\textbf{ciao3}&0.127&0.098&\cellcolor{green}0.958\\\hline
\end{tabular}

\end{document}

enter image description here

For getting automatically what you want you just need to use the following code; it's necessary to change the name of the environment, though.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{xparse,environ}

\ExplSyntaxOn
\NewEnviron{matlabtabular}[1]
 {
  \tl_set:Nx \l_angelino_matlab_preamble_tl 
   {
    \tl_tail:n { #1 }
   }
  \tl_put_left:Nn \l_angelino_matlab_preamble_tl { | >{\columncolor{red!20}} }
  \angelino_matlabtab:VV \l_angelino_matlab_preamble_tl \BODY
 }

\cs_new_protected:Nn \angelino_matlabtab:nn
 {
  \seq_set_split:Nnn \l_angelino_matlabtab_rows_seq { \hline } { #2 }
  % the first item is empty
  \seq_pop_left:NN \l_angelino_matlabtab_rows_seq \l_angelino_matlabtab_body_tl
  % the last item is empty
  \seq_pop_right:NN \l_angelino_matlabtab_rows_seq \l_tmpa_tl
  % separate the first row
  \seq_pop_left:NN \l_angelino_matlabtab_rows_seq \l_angelino_matlabtab_body_tl
  % fix the first row
  \tl_put_left:Nn \l_angelino_matlabtab_body_tl { \hline\rowcolor{blue!20} }
  \tl_put_right:Nn \l_angelino_matlabtab_body_tl { \hline }
  \seq_map_inline:Nn \l_angelino_matlabtab_rows_seq
   {
    \int_incr:N \l_angelino_matlab_currentrow_int
    \int_zero:N \l_angelino_matlab_currentcol_int
    \seq_set_split:Nnn \l_angelino_matlabtab_row_seq { & } { ##1 }
    \seq_pop_left:NN \l_angelino_matlabtab_row_seq \l_angelino_matlabtab_first_tl
    \tl_put_right:NV \l_angelino_matlabtab_body_tl \l_angelino_matlabtab_first_tl
    \seq_map_inline:Nn \l_angelino_matlabtab_row_seq
     {
      \int_incr:N \l_angelino_matlab_currentcol_int
      \int_compare:nTF { \l_angelino_matlab_currentcol_int == \l_angelino_matlab_currentrow_int }
       {
        \tl_put_right:Nn \l_angelino_matlabtab_body_tl { & \cellcolor{green} ####1 }
       }
       {
        \tl_put_right:Nn \l_angelino_matlabtab_body_tl { & ####1 }
       }
     }
    \tl_put_right:Nn \l_angelino_matlabtab_body_tl { \hline }
   }
  \begin{tabular}{#1}
  \l_angelino_matlabtab_body_tl
  \end{tabular}
 }
\cs_generate_variant:Nn \angelino_matlabtab:nn { VV }
\int_new:N \l_angelino_matlab_currentcol_int
\int_new:N \l_angelino_matlab_currentrow_int
\seq_new:N \l_angelino_matlabtab_rows_seq
\seq_new:N \l_angelino_matlabtab_row_seq
\tl_new:N \l_angelino_matlabtab_body_tl
\tl_new:N \l_angelino_matlabtab_first_tl
\tl_new:N \l_angelino_matlab_preamble_tl
\ExplSyntaxOff

\begin{document}

\begin{matlabtabular}{|l|c|c|c|}
\hline
&\textbf{ciao}&\textbf{ciao2}&\textbf{ciao3}\\\hline
\textbf{ciao}&0.815&0.913&0.278\\\hline
\textbf{ciao2}&0.906&0.632&0.547\\\hline
\textbf{ciao3}&0.127&0.098&0.958\\\hline
\end{matlabtabular}

\end{document}

The output is the same as before.

Note that this makes some assumptions on the environment's contents. The argument should begin with | that's changed into |>{\columncolor{red!20}}. It is also assumed that every row ends with \\\hline.

egreg
  • 1,121,712
3

For comparison, here is how to do this in ConTeXt. I think that ConTeXt provides a much nicer separation of content and presentation for tables.

\definecolor[lightblue]  [b=1,r=0.8,g=0.8]
\definecolor[lightred]   [r=1,b=0.8,g=0.8]
\definecolor[lightgreen] [g=1,r=0.8,b=0.8]

\startsetups diagonal
  \setupTABLE[offset=0.5em]
  \dorecurse{20}% Assuming there are no more than 20 rows
    {\setupTABLE[\recurselevel][\recurselevel][background=color,backgroundcolor=lightgreen]}
  \setupTABLE[column][first][background=color, backgroundcolor=lightred, style=bold]
  \setupTABLE[row][first][background=color, backgroundcolor=lightblue, style=bold]
\stopsetups

\starttext

\startTABLE[setups=diagonal]
  \NC       \NC ciao  \NC ciao2 \NC ciao3 \NC \NR
  \NC ciao  \NC 0.815 \NC 0.913 \NC 0.287 \NC \NR
  \NC ciao2 \NC 0.906 \NC 0.632 \NC 0.547 \NC \NR
  \NC ciao3 \NC 0.127 \NC 0.098 \NC 0.958 \NC \NR
\stopTABLE

\stoptext

enter image description here

Aditya
  • 62,301
2

Here is a way to do that in an automatic way by using the environment {NiceTabular} of nicematrix.

Moreover, you will have a perfect output: the rules won't seem to disappear in some PDF viewers at somme levels of zoom.

\documentclass{article}
\usepackage{xcolor}

\usepackage{nicematrix} \usepackage{pgffor}

\NewDocumentEnvironment { matlabtabular } { O { } m ! O { } } { \begin{NiceTabular}[#1, #3, hvlines] { #2 } \CodeBefore
\columncolor{red!15}{1} \rowcolor{blue!15}{1} \foreach \x in { 2,...,\arabic{iRow} } { \cellcolor{green!15}{\x-\x} } \Body } { \end{NiceTabular} }

\begin{document}

\begin{matlabtabular}{lccc} &\textbf{ciao}&\textbf{ciao2}&\textbf{ciao3}\ \textbf{ciao}&0.815&0.913&0.278\ \textbf{ciao2}&0.906&0.632&0.547\ \textbf{ciao3}&0.127&0.098&0.958\ \end{matlabtabular}

\end{document}

You need several compilations because nicematrix uses PGF/Tikz nodes under the hood.

Output of the above code

F. Pantigny
  • 40,250
  • Could it be possible to also apply the \textbfmacro automatically? – projetmbc Mar 27 '21 at 11:09
  • It's possible to put >{\bfseries}l instead of l in the preamble of the array (for the first column) but, as of now, there is nothing in nicematrix to specify formatting for a whole row. I will think of that for a new version of nicematrix. – F. Pantigny Mar 27 '21 at 11:30
  • Good idea. This could give a taste of CSS formating to nicematrix. – projetmbc Mar 27 '21 at 12:33