7

I have to put big matrices into my thesis and I cannot find a way to do it in a beautiful way. The problem is: these matrices are really sparse but I need them complete for analysis purposes.

So far, I have managed to represent them like this (leaving blank spaces where the value is 0): My ugly matrix

The thing is, the matrix is 14x14, but I need the first row/column to show the number 1->14.

Any idea how to improve it while keeping the completeness?

EDIT : Here is the MWE:

\documentclass[11pt,a4paper]{article}
\usepackage{array}
\begin{document}
\begin{table}[!h]
\centering
\begin{tabular}{|c>{\centering\arraybackslash}p{0.5cm}|>{\centering\arraybackslash}p{0.5cm}|>{\centering\arraybackslash}p{0.5cm}|>{\centering\arraybackslash}p{0.5cm}|>{\centering\arraybackslash}p{0.5cm}|>{\centering\arraybackslash}p{0.5cm}|>{\centering\arraybackslash}p{0.5cm}|>{\centering\arraybackslash}p{0.5cm}|>{\centering\arraybackslash}p{0.5cm}|>{\centering\arraybackslash}p{0.7cm}|>{\centering\arraybackslash}p{0.5cm}|>{\centering\arraybackslash}p{0.5cm}|>{\centering\arraybackslash}p{0.5cm}|>{\centering\arraybackslash}p{0.85cm}|}
\hline
{\bfseries SVM} & \multicolumn{1}{|c|}{1} & \multicolumn{1}{c|}{2} & \multicolumn{1}{c|}{3} & \multicolumn{1}{c|}{4} & \multicolumn{1}{c|}{5} & \multicolumn{1}{c|}{6} & \multicolumn{1}{c|}{7} & \multicolumn{1}{c|}{8} & \multicolumn{1}{c|}{9} & \multicolumn{1}{c|}{10} & \multicolumn{1}{c|}{11} & \multicolumn{1}{c|}{12} & \multicolumn{1}{c|}{13} & \multicolumn{1}{c|}{14} \\
\hline
\multicolumn{1}{|c|}{1} &   &   &   &   &   &   &   &   &   &   &   &   &   & 2.3 \\
\hline \multicolumn{1}{|c|}{2} &   &   &   &   &   &   &   &   &   &   &   &   &   & 0.4 \\  
\hline \multicolumn{1}{|c|}{3} & & & & & & & & & & & & & & \\  
\hline \multicolumn{1}{|c|}{4} & & & & & & & & & & & & & & 0.2 \\
\hline  \multicolumn{1}{|c|}{5} && & & & & & & & 0.7 & & & & & 1.2 \\                   
\hline \multicolumn{1}{|c|}{6} & & & & & & & & & & & & & & 0.3 \\  
\hline \multicolumn{1}{|c|}{7} & & & & & & & & & & & & & & 0.2 \\         
\hline \multicolumn{1}{|c|}{8} & & & & & & & & & & & & & & 0.1 \\  
\hline \multicolumn{1}{|c|}{9} & & & & & & & & & 3.4 & & & & & 1.5 \\  
\hline \multicolumn{1}{|c|}{10} & & & & & & & & & & 11.0 & & & & \\      
\hline  \multicolumn{1}{|c|}{11} & & & & & & & & & & & & & & 0.1 \\      
\hline \multicolumn{1}{|c|}{12} & & & & & & & & & & & & & & 2.6 \\  
\hline \multicolumn{1}{|c|}{13} & & & & & & & & & & & & & & 0.1 \\  
\hline \multicolumn{1}{|c|}{14} & & & & & & & & & 0.2 & 0.3 & & & & 200.0 \\ 
\hline
\end{tabular}
\caption{Mean confusion matrices for the first multi-class classification.}
\label{table6}
\end{table}
\end{document}
  • 3
    Welcome to TeX.SX! Please add the code that produced this output (in the form of a minimal working example (MWE)) so that other users have a starting point to work on. – Pier Paolo Apr 23 '15 at 16:00
  • would make no difference to your output but you can remove all the \multicolumn{1}{|c|} as they are not doing anything. – David Carlisle Apr 23 '15 at 16:27
  • Do you just want a way to represent the table in a better way or what exactly? Sorry I don't understand your goal. – Alenanno Apr 23 '15 at 16:42
  • @David, thanks! I know, but they are still there from earlier try. – Mathieu D. Apr 23 '15 at 16:46
  • @Alenno, I just think this table is very ugly, but it is strictly correct and shows what I want it to show. I am just looking for a way to make this table/matrix look better :-) – Mathieu D. Apr 23 '15 at 16:46
13

A couple of possibilities:

enter image description here

\documentclass[11pt,a4paper]{article}
\usepackage{dcolumn}
\newcolumntype{d}{D..{3.1}}
\begin{document}


\noindent X\dotfill X

\bigskip
\begin{table}[htp]% not ever [!h]
\centering

\setlength\tabcolsep{.37pt}
\small
\begin{tabular}{@{}|D..{2.0}|*{14}{d|}@{}}
\hline
\multicolumn{1}{|c|}{\footnotesize\bfseries SVM} &
 \multicolumn{1}{c|}{1}
 & \multicolumn{1}{c|}{2}
 & \multicolumn{1}{c|}{3}
 & \multicolumn{1}{c|}{4}
 & \multicolumn{1}{c|}{5}
 & \multicolumn{1}{c|}{6}
 & \multicolumn{1}{c|}{7}
 & \multicolumn{1}{c|}{8}
 & \multicolumn{1}{c|}{9}
 & \multicolumn{1}{c|}{10}
 & \multicolumn{1}{c|}{11}
 & \multicolumn{1}{c|}{12}
 & \multicolumn{1}{c|}{13}
 & \multicolumn{1}{c|}{14}
 \\
\hline
1 &   &   &   &   &   &   &   &   &   &   &   &   &   & 2.3 \\
\hline 2 &   &   &   &   &   &   &   &   &   &   &   &   &   & 0.4 \\  
\hline 3 & & & & & & & & & & & & & & \\  
\hline 4 & & & & & & & & & & & & & & 0.2 \\
\hline  5 && & & & & & & & 0.7 & & & & & 1.2 \\                   
\hline 6 & & & & & & & & & & & & & & 0.3 \\  
\hline 7 & & & & & & & & & & & & & & 0.2 \\         
\hline 8 & & & & & & & & & & & & & & 0.1 \\  
\hline 9 & & & & & & & & & 3.4 & & & & & 1.5 \\  
\hline 10 & & & & & & & & & & 11.0 & & & & \\      
\hline  11 & & & & & & & & & & & & & & 0.1 \\      
\hline 12 & & & & & & & & & & & & & & 2.6 \\  
\hline 13 & & & & & & & & & & & & & & 0.1 \\  
\hline 14 & & & & & & & & & 0.2 & 0.3 & & & & 200.0 \\ 
\hline
\end{tabular}
\caption{Mean confusion matrices for the first multi-class classification.}
\label{table6}
\end{table}

\begin{table}[htp]% not ever [!h]
\centering

\setlength\tabcolsep{.58pt}
\small
\def\z{\multicolumn{1}{D.\cdot{2.2}}{.}}

\begin{tabular}{@{}D..{2.0}|*{14}{d}@{}}

\multicolumn{1}{c|}{\footnotesize\bfseries SVM} &
 \multicolumn{1}{c}{1}
 & \multicolumn{1}{c}{2}
 & \multicolumn{1}{c}{3}
 & \multicolumn{1}{c}{4}
 & \multicolumn{1}{c}{5}
 & \multicolumn{1}{c}{6}
 & \multicolumn{1}{c}{7}
 & \multicolumn{1}{c}{8}
 & \multicolumn{1}{c}{9}
 & \multicolumn{1}{c}{10}
 & \multicolumn{1}{c}{11}
 & \multicolumn{1}{c}{12}
 & \multicolumn{1}{c}{13}
 & \multicolumn{1}{c}{14}
 \\
\hline
1 &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z & 2.3 \\
 2 &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z & 0.4 \\  
 3 &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z & \\  
 4 &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z & 0.2 \\
  5 &\z &\z &\z &\z &\z &\z &\z &\z & 0.7 &\z &\z &\z &\z & 1.2 \\                   
 6 &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z & 0.3 \\  
 7 &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z & 0.2 \\         
 8 &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z & 0.1 \\  
 9 &\z &\z &\z &\z &\z &\z &\z &\z & 3.4 &\z &\z &\z &\z & 1.5 \\  
 10 &\z &\z &\z &\z &\z &\z &\z &\z &\z & 11.0 &\z &\z &\z & \\      
  11 &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z & 0.1 \\      
 12 &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z & 2.6 \\  
 13 &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z &\z & 0.1 \\  
 14 &\z &\z &\z &\z &\z &\z &\z &\z & 0.2 & 0.3 &\z &\z &\z & 200.0 \\ 

\end{tabular}
\caption{Mean confusion matrices for the first multi-class classification.}
\label{tablezz}
\end{table}

\end{document}
  • 2nd table is missing a couple of \z in the last column.... – David Carlisle Apr 23 '15 at 16:54
  • 4
    Well, I like the look of the 2nd table, but all those \z's put me to sleep. – Steven B. Segletes Apr 23 '15 at 16:56
  • 2
    @StevenB.Segletes perhaps I should have called it \STACK then you would have woken up – David Carlisle Apr 23 '15 at 17:41
12

Here's how I'd change it. The beauty of a table can be subjective but I think that the lines only add noise and make it "harder" to read the table comfortably. I prefer to use only (sparse) horizontal lines, or alternate row colours, like in this case.

Any added packages or commands have been commented in the code as a quick explanation. Post a comment if you have specific questions.


Edit: I have added the proper alignment of the numerical values according to the decimal separator.

figure without vertical rules

\documentclass{article}
\usepackage{booktabs, dcolumn} % required for \toprule command. Others are \midrule, and \bottomrule, second one required for aligning numerals by decimal separator
\usepackage{array,ragged2e}
\usepackage[table]{xcolor}    % loads also »colortbl«, required for the alternate row colours

\makeatletter
\newcommand*{\centerfloat}{%
  \parindent \z@
  \leftskip \z@ \@plus 1fil \@minus \textwidth
  \rightskip\leftskip
  \parfillskip \z@skip}
\makeatother % this one is required for the \centerfloat command to work

\newcolumntype{.}{D{.}{.}{-1}}
%\newcolumntype{z}[1]{D{.}{.}{#1}} % so your top tabular doesn't look as messy

\begin{document}

\begin{figure}[!htbp]
     \centerfloat
\rowcolors{2}{gray!10}{white}
\begin{tabular}{.*{15}{.}} % *{num}{form}, the format form is repeated num times.


\multicolumn{1}{c}{{\bfseries SVM}} &
\multicolumn{1}{c}{1} &
\multicolumn{1}{c}{2} &
\multicolumn{1}{c}{3} &
\multicolumn{1}{c}{4} &
\multicolumn{1}{c}{5} &
\multicolumn{1}{c}{6} &
\multicolumn{1}{c}{7} &
\multicolumn{1}{c}{8} &
\multicolumn{1}{c}{9} &
\multicolumn{1}{c}{10} &
\multicolumn{1}{c}{11} &
\multicolumn{1}{c}{12} &
\multicolumn{1}{c}{13} &
\multicolumn{1}{c}{14} \\

\toprule

\multicolumn{1}{c}{1} &   &   &   &   &   &   &   &   &   &   &   &   &   & 2.3 \\
\multicolumn{1}{c}{2} &   &   &   &   &   &   &   &   &   &   &   &   &   & 11110.4 \\
\multicolumn{1}{c}{3} & & & & & & & & & & & & & & \\
\multicolumn{1}{c}{4} & & & & & & & & & & & & & & 0.2 \\
\multicolumn{1}{c}{5} && & & & & & & & 0.7 & & & & & 1.2 \\
\multicolumn{1}{c}{6} & & & & & & & & & & & & & & 0.3 \\
\multicolumn{1}{c}{7} & & & & & & & & & & & & & & 0.2 \\
\multicolumn{1}{c}{8} & & & & & & & & & & & & & & 0.1 \\
\multicolumn{1}{c}{9} & & & & & & & & & 3.4 & & & & & 1.5 \\
\multicolumn{1}{c}{10} & & & & & & & & & & 11.0 & & & & \\
\multicolumn{1}{c}{11} & & & & & & & & & & & & & & 0.1 \\
\multicolumn{1}{c}{12} & & & & & & & & & & & & & & 2.6 \\
\multicolumn{1}{c}{13} & & & & & & & & & & & & & & 0.1 \\
\multicolumn{1}{c}{14} & & & & & & & & & 0.2 & 0.3 & & & & 200.0 \\

\end{tabular}
\caption{Mean confusion matrices for the first multi-class classification.}
\label{table6}
\end{figure}
\end{document}

If you really want the vertical lines, you can add \arrayrulecolor{gray!30} right below \rowcolors{2}{gray!10}{white}. Also replace the tabular header with this: \begin{tabular}{.*{13}{.|}{.}}, this way, the vertical rules appear only in the cells and not the sides of the table.

figure with vertical rules

  • 1
    I like this one, but I'd have faint/light grey vertical lines for clarity of the columns. – Griffin Apr 23 '15 at 18:50
  • I really like this one too, but I agree with Griffin, is there a way to have a very small/light column separator? – Mathieu D. Apr 23 '15 at 18:57
  • @MathieuD. once the table is full you wouldn't need vertical lines. If you look at the content in the last column, it's quite clear how the column goes. There is an easy way for providing column separators but I advise against it. Do you have the complete set of data for the table? – Alenanno Apr 23 '15 at 23:39
  • @MathieuD. Is this the final version of the table? Let me know. :) In any way, if you really want the vertical lines, you can add \arrayrulecolor{gray!30} right below \rowcolors{2}{gray!10}{white}. – Alenanno Apr 23 '15 at 23:41
  • @Alenanno, yes this is the final version. But I will need to use the same kind of table for different matrices, some of them will be less sparse :-) Thanks for the solution! – Mathieu D. Apr 24 '15 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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