5

I have a large table, and I don't want it to exceed the page limit. Please see the code below.

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

\begin{document}    
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l 
>{\columncolor[HTML]{EFEFEF}}l }
\hline
DM    & Option   & States &   &   &   &   &   &   &   &   &    &    &    &    &    &    &    &    &    &    &    &    &    &    &    &    &    \\ \hline
X     & Option A & N      & Y & N & N & Y & N & N & Y & N & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  \\
      & Option B & N      & N & Y & N & N & Y & N & N & N & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  \\
Y     & Option C & N      & N & N & Y & Y & Y & N & N & N & N  & N  & Y  & Y  & Y  & N  & N  & N  & N  & N  & N  & Y  & Y  & Y  & N  & N  & N  \\
      & Option D & N      & N & N & N & N & N & Y & Y & N & N  & N  & N  & N  & N  & Y  & Y  & Y  & N  & N  & N  & N  & N  & N  & Y  & Y  & Y  \\
Z     & Option E & Y      & Y & Y & Y & Y & Y & Y & Y & N & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  \\
      & Option F & N      & N & N & N & N & N & N & N & Y & Y  & Y  & Y  & Y  & Y  & Y  & Y  & Y  & N  & N  & N  & N  & N  & N  & N  & N  & N  \\
      & Option G & N      & N & N & N & N & N & N & N & N & N  & N  & N  & N  & N  & N  & N  & N  & Y  & Y  & Y  & Y  & Y  & Y  & Y  & Y  & Y  \\ \hline
Label &          & 1      & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25 & 26 \\ \hline
\end{tabular}
\end{table}
\end{document}
4
  • Welcome to TeX.SX! Commented Aug 26, 2016 at 3:43
  • 2
    I feel like there's probably a better way of getting a shaded background to your table, but still, at least as a point of interest, you might like to know that you can optimise that code with *{28}{>{\columncolor[HTML]{EFEFEF}}l} The *{n}{x} syntax means n copies of the column specifier. So *{5}{l} would mean 5 left-aligned columns. Used in specifying table columns, and possibly some other places, but not generally. Can be used multiple times, e.g. *{2}{c}rl*{5}{p{1cm}}
    – Au101
    Commented Aug 26, 2016 at 3:52
  • Please state the page width, the width of the text block, the document font size, and the font you use (if not Computer Modern).
    – Mico
    Commented Aug 26, 2016 at 4:55
  • 1
    This has to be a duplicate...
    – luchonacho
    Commented Aug 27, 2016 at 7:55

3 Answers 3

10

The table is much too wide for a normal portrait page.

  • Therefore the following example rotates it via environment sidewaystable of package rotating.

  • The width of the table is still too large, therefore it is decreased by reducing the space between columns via setting \tabcolsep to a smaller value.

  • The background color is set by a simple \colorbox instead of coloring each column individually with the same color. \colorbox uses an additional margin, which is controlled by \fboxsep, which is set to zero here locally.

  • The lines are set by the macros of package booktabs, which have a nicer spacing behavior.

  • Package caption fixes the vertical spacing around table caption, which appear on top of the tabular.

Example file:

\documentclass{article}
\usepackage{booktabs}
\usepackage{caption}
\usepackage[table,xcdraw]{xcolor}
\usepackage{rotating}
\begin{document}
  \begin{sidewaystable}
    \caption{My caption}
    \label{my-label}
    \setlength{\fboxsep}{0pt}%
    \setlength{\tabcolsep}{.73\tabcolsep}%
    \colorbox[HTML]{EFEFEF}{%
      \begin{tabular}{*{28}{l}}%
        \toprule
        DM    & Option   & \multicolumn{26}{c}{States} \\
        \midrule
        X     & Option A & N      & Y & N & N & Y & N & N & Y & N & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  \\
              & Option B & N      & N & Y & N & N & Y & N & N & N & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  \\
        Y     & Option C & N      & N & N & Y & Y & Y & N & N & N & N  & N  & Y  & Y  & Y  & N  & N  & N  & N  & N  & N  & Y  & Y  & Y  & N  & N  & N  \\
              & Option D & N      & N & N & N & N & N & Y & Y & N & N  & N  & N  & N  & N  & Y  & Y  & Y  & N  & N  & N  & N  & N  & N  & Y  & Y  & Y  \\
        Z     & Option E & Y      & Y & Y & Y & Y & Y & Y & Y & N & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  \\
              & Option F & N      & N & N & N & N & N & N & N & Y & Y  & Y  & Y  & Y  & Y  & Y  & Y  & Y  & N  & N  & N  & N  & N  & N  & N  & N  & N  \\
              & Option G & N      & N & N & N & N & N & N & N & N & N  & N  & N  & N  & N  & N  & N  & N  & Y  & Y  & Y  & Y  & Y  & Y  & Y  & Y  & Y  \\
        \midrule
        Label &          & 1      & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25 & 26 \\
        \bottomrule
      \end{tabular}%
    }%
  \end{sidewaystable}
\end{document}

Result

4
  • 2
    +1 for the use of \colorbox and \fboxsep=0pt to simplify the code that colors the entire tabular environment.
    – Mico
    Commented Aug 26, 2016 at 4:41
  • (+1) Maybe a \multicolumn{3}{l}{States}would also reduce the tablewidth, and look nicer?
    – Bernard
    Commented Aug 26, 2016 at 8:22
  • 1
    @Bernard - I suspect that the word "States" should actually be centered across columns 3 thru 28. (That, at least, is the choice I made in my answer...)
    – Mico
    Commented Aug 26, 2016 at 10:04
  • @Mico Yes, fixed. Commented Aug 26, 2016 at 16:14
8

Assuming your document uses a "normal" paper size (A4 or US letter), "normal" page margin (say, 1 inch), and font size of 10pt, it is possible (though only just) to make the table fit inside the text block without rotating it into "landscape" mode. I suggest you

  • reorganize the table's header by combining the header and footer lines and center the label "States" over columns 3 thru 28,

  • eliminate some redundancies in the column labelled "Option".

  • use a tabular* environment to set the width of the table equal to \textwidth, and

Separately, I would also like to suggest that you use the line-drawing macros of the booktabs package (instead of \hline) and add a bit of whitespace between some of the rows. These changes will help a reader's eye glide over the copious material.

enter image description here

\documentclass{article}
\usepackage[a4paper,margin=2.5cm]{geometry} % set page and text block parameters
\usepackage{booktabs}
\usepackage[skip=0.5\baselineskip]{caption}
\usepackage[table]{xcolor} 
\begin{document}
\begin{table}
\caption{My caption}
\label{my-label}
\setlength{\fboxsep}{0pt}
\setlength{\tabcolsep}{0pt} % let "tabular*" figure out intercolumn widths
\colorbox[HTML]{EFEFEF}{% % see http://tex.stackexchange.com/a/326578/5001
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}*{28}{l}}
\toprule
DM    & Option   & \multicolumn{26}{c}{States}\\
\cmidrule{3-28}
      &   & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & 22 & 23 & 24 & 25 & 26 \\
\midrule
X     & A & N & Y & N & N & Y & N & N & Y & N & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  \\
      & B & N & N & Y & N & N & Y & N & N & N & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  & N  & N  & Y  \\ 
\addlinespace
Y     & C & N & N & N & Y & Y & Y & N & N & N & N  & N  & Y  & Y  & Y  & N  & N  & N  & N  & N  & N  & Y  & Y  & Y  & N  & N  & N  \\
      & D & N & N & N & N & N & N & Y & Y & N & N  & N  & N  & N  & N  & Y  & Y  & Y  & N  & N  & N  & N  & N  & N  & Y  & Y  & Y  \\ 
\addlinespace
Z     & E & Y & Y & Y & Y & Y & Y & Y & Y & N & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  & N  \\
      & F & N & N & N & N & N & N & N & N & Y & Y  & Y  & Y  & Y  & Y  & Y  & Y  & Y  & N  & N  & N  & N  & N  & N  & N  & N  & N  \\
      & G & N & N & N & N & N & N & N & N & N & N  & N  & N  & N  & N  & N  & N  & N  & Y  & Y  & Y  & Y  & Y  & Y  & Y  & Y  & Y  \\
\bottomrule
\end{tabular*}}
\end{table}
\end{document}
2
  • very nice answer and good explanation. Unortunately I can't vote twice for it :-(. I store it into my local "bank" of very good and usable answers ...
    – Zarko
    Commented Aug 29, 2016 at 10:49
  • @Zarko - Many thanks for the compliment! :-)
    – Mico
    Commented Aug 29, 2016 at 13:09
6

Why not swap rows and columns? Here is a way to do it:

\documentclass{article}
\usepackage[showframe]{geometry} 
\usepackage[table,xcdraw, svgnames]{xcolor}
\usepackage{booktabs, caption, multirow, stackengine}
\setstackEOL{\\}

\begin{document}
\begin{table}[]
  \centering
  \caption{My caption}
  \label{my-label}
  \setlength\extrarowheight{\belowrulesep}
  \rowcolors{1}{white}{Lavender!90!lightgray}
  \setlength\belowrulesep{0pt}
  \begin{tabular}{crl*{9}{c}c}
    \toprule
   \rowcolor{white} & DM: & \multicolumn{2}{c}{X}  & & \multicolumn{2}{c}{Y}  &  & \multicolumn{3}{c}{Z} \\
    \cmidrule {3-4} \cmidrule{6-7} \cmidrule{9-11}
    \rowcolor{white} Label & Options: & A & B &   & C & D &   & E & F & G \\
    \midrule
    1 & & N & N &   & N & N &   & Y & N & N \\
    2 &  & Y & N & &  N & N &  & Y & N & N \\
    3 &  & N & Y  & &  N & N &  & Y & N & N \\
    4 &  & N & N & &  Y & N &  & Y & N & N \\ 
    5 &  & Y & N & &  Y & N &  & Y & N & N \\
    6 &  & N & Y & &  Y & N &  & Y & N & N \\
    7 &  & N & N & &  N & Y &  & Y & N & N \\
    8 &  & Y & N & &  N & Y &  & Y & N & N \\
    9 &  & N & N &   & N & N &   & N & Y & N \\
  10 &  & Y & N & &  N & N &  & N & Y & N \\
  11 &  & N & Y  & &  N & N &  & N & Y & N \\
  12 &  & N & N & &  Y & N &  & N & Y  & N \\
  13 &  & Y & N & &  Y & N &  & N & Y & N \\
  14 &  & N & Y & &  Y & N &  & N & Y & N \\
  15 &  & N & N & &  N & Y &  & N & Y & N \\
  16 &  & Y & N & &  N & Y &  & N & Y & N \\
  17 &  & N & Y & &  N & Y &  & N & Y & N \\
  18 &  & N & N &   & N & N &   & N & N& Y \\
  19 &  & Y & N &   & N & N &   & N & N& Y \\
  20 &  & N & Y &   & N & N &   & N & N& Y \\
  21 &  & N & N &   & Y & N &   & N & N& Y \\
  22 &  & Y & N &   & Y & N &   & N & N& Y \\
  23 &  & N & Y &   & Y & N &   & N & N& Y \\
  24 &  & N & N &   & N & Y &   & N & N& Y \\ 
  25 &  & Y & N &   & N & Y &   & N & N& Y \\
  26 &  \multirow{-26}{*}[2ex]{\setlength\fboxrule{0.8pt}\fcolorbox{ VioletRed!80!}{white}{\scshape \Shortstack{s\\t\\a\\t\\e\\s}}} %
  & N & Y &   & N & Y &   & N & N& Y \\  
    \bottomrule
  \end{tabular}
\end{table}
\end{document} 

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .