5

I´m sorry by English mistakes. I´m looking for how to set of different sizes of cells in tabular enviroment, like picture (I would like the table to span the entire page width). How can I do this?

I´m using these packages

\documentclass[a4paper,brazil, 12pt]{report}
\usepackage{tikz}
\usepackage{geometry}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{float}
\usepackage{adjustbox}
\geometry{a4paper,
    top=1.5cm,  
    bottom=1.5cm,   
    left=1.5cm,
    right=1.5cm,
}


  \begin{document}
    \begin{tabular}{|c|c|c|c|c|c|c|}
    \hline
    \multirow{2}{*}{{Question}} & \multirow{2}{*}{a} & \multirow{2}{*}{b} & \multirow{2}{*}{c} & \multirow{2}{*}{d} & \multicolumn{2}{c|}{Scores} \\ 
    \cmidrule{6-7}
     &  &  &  &  & Individual & Team \\ 
    \midrule 
    1 &  &  &  &  &  &  \\ 
    \midrule  
    2 &  &  &  &  &  &  \\ 
    \midrule  
    3 &  &  &  &  &  &  \\ 
    \midrule  
    4 &  &  &  &  &  &  \\ 
    \midrule  
    5 &  &  &  &  &  &  \\ 
    \midrule  
    6 &  &  &  &  &  &  \\ 
    \midrule  
    7 &  &  &  &  &  &  \\ 
    \midrule  
    8 &  &  &  &  &  &  \\ 
    \midrule  
    9 &  &  &  &  &  &  \\ 
    \midrule  
    10 &  &  &  &  &  &  \\ 
    \midrule  
    \multicolumn{5}{|c|}{Total} &  &  \\ 
    \bottomrule 
    \end{tabular} 
    \end{document}

Thanks for everyone.

enter image description here

2
  • Could you please show what you have tried so far. Please also state what the problematic part is. Is it the colors, the alignment, the headers of the last two columns...?
    – leandriis
    May 22, 2019 at 15:58
  • I have been put above. Unfortunately this is my first "big table"
    – Moura
    May 22, 2019 at 19:12

3 Answers 3

5

With the help of the tabularx package you can ensure that the table is as wide as the textwidth. I have declared a new, centered column type for this. With xcolor and the table option the rowcolor and columncolor commands can be used. With >{\hsize=1.5\hsize} you can change the width of the C type columns. (Make sure that the sum of the values is equal to the number of C type columns used (e.g. 1.5+1.5+0.75+0.25=4 for 4 columns).

Lastly, I'd not recommend using the lines from the booktabs package in combination with vertical lines (as you did in your MWE). You might have noticed the small gaps in the vertical lines that are due to the fact that the booktabs lines are designed to be used in tables without vertical lines.

enter image description here

\documentclass[a4paper,12pt]{report}
\usepackage{geometry}
\usepackage{multirow}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\usepackage{ragged2e}
\usepackage{hhline}
\newcolumntype{C}{>{\Centering}X}

\begin{document}\noindent
\begin{tabularx}{\textwidth}{|>{\columncolor{lightgray}}c
                             |>{\hsize=1.5\hsize}C
                             |>{\hsize=1.5\hsize}C
                             |>{\hsize=.75\hsize}C
                             |>{\hsize=.25\hsize}C
                             |c|c|}
\hline
\rowcolor{lightgray}
& & & & & \multicolumn{2}{c|}{Scores}\\ \hhline{*5{>{\arrayrulecolor{lightgray}}-}*{2}{>{\arrayrulecolor{black}}-}}
\rowcolor{lightgray}
\multirow{-2.2}{*}{Question} & \multirow{-2.2}{*}{a)} & \multirow{-2.2}{*}{b)} & \multirow{-2.2}{*}{c)} & \multirow{-2.2}{*}{d)} & Individual & Team \\
\hline
1. & & & & & & \\
\hline
2. & & & & & & \\
\hline
\rowcolor{lightgray}
\multicolumn{5}{|c|}{TOTAL} & & \\
\hline
\end{tabularx}
\end{document}

If you wish to stick to the booktabs lines, I'd suggest a layout without vertical lines, with fewer horizontal lines and without color:

enter image description here

\documentclass[a4paper,12pt]{report}
\usepackage{geometry}
\usepackage{multirow}
\usepackage{tabularx}
\usepackage{ragged2e}
\newcolumntype{C}{>{\Centering}X}

\usepackage{booktabs}

\begin{document}
\noindent
\begin{tabularx}{\textwidth}{lCCCCcc}
\toprule
& & & & & \multicolumn{2}{c}{Scores}\\ \cmidrule{6-7}
\multirow{-2.2}{*}{Question} & \multirow{-2.2}{*}{a)} & \multirow{-2.2}{*}{b)} & \multirow{-2.2}{*}{c)} & \multirow{-2.2}{*}{d)} & Individual & Team \\
\midrule
1. & & & & & & \\
2. & & & & & & \\
\cmidrule{1-5}
\multicolumn{5}{l}{TOTAL} & & \\
\bottomrule
\end{tabularx}
\end{document}
7
  • I don´t know why, but your first set doesn´t work for me. But thanks for your help. I´m trying to fix here.
    – Moura
    May 22, 2019 at 20:20
  • @Moura: Do you get any error messages or just not the shwon output?
    – leandriis
    May 22, 2019 at 20:25
  • Your MWE works, but if you realize your picture above, if you notice, you will see that some lines disappear. Try again using 10 itens, please.
    – Moura
    May 22, 2019 at 20:42
  • @Moura: The disapprearing vertical line between d) and Scores is definitely a viewer issue. THe line will appear if you zoom in. This issue of disappearing lines in combination with colored cells is well known.
    – leandriis
    May 22, 2019 at 20:46
  • @Moura: Regarding the missing short horizontal line below Sources: This was a mistake. I have edited my anser to also include this line.
    – leandriis
    May 22, 2019 at 20:56
2

Use tabularx package:

\documentclass[a4paper,brazil, 12pt]{report}
\usepackage{tikz}
\usepackage{geometry}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{float}
\usepackage{adjustbox}
\geometry{a4paper,
    top=1.5cm,  
    bottom=1.5cm,   
    left=1.5cm,
    right=1.5cm,
}
\usepackage{tabularx}


  \begin{document}
    \begin{tabularx}{\linewidth}{|X|X|X|X|X|X|X|}
    \hline
    \multirow{2}{*}{{Question}} & \multirow{2}{*}{a} & \multirow{2}{*}{b} & \multirow{2}{*}{c} & \multirow{2}{*}{d} & \multicolumn{2}{c|}{Scores} \\ 
    \cmidrule{6-7}
     &  &  &  &  & Individual & Team \\ 
    \midrule 
    1 &  &  &  &  &  &  \\ 
    \midrule  
    2 &  &  &  &  &  &  \\ 
    \midrule  
    3 &  &  &  &  &  &  \\ 
    \midrule  
    4 &  &  &  &  &  &  \\ 
    \midrule  
    5 &  &  &  &  &  &  \\ 
    \midrule  
    6 &  &  &  &  &  &  \\ 
    \midrule  
    7 &  &  &  &  &  &  \\ 
    \midrule  
    8 &  &  &  &  &  &  \\ 
    \midrule  
    9 &  &  &  &  &  &  \\ 
    \midrule  
    10 &  &  &  &  &  &  \\ 
    \midrule  
    \multicolumn{5}{|c|}{Total} &  &  \\ 
    \bottomrule 
    \end{tabularx} 
    \end{document}
2

1. Requested layout

If you want to try a different solution, in particular if you build a class or template, here is a solution using cals:

\documentclass[a4paper,12pt]{report}
\usepackage[margin=1.5cm]{geometry}
\usepackage[table]{xcolor}
\usepackage{cals, calc}

\newlength{\bcw}
\thispagestyle{empty}

% Shortcuts
\let\nc=\nullcell
\let\sc=\spancontent

\begin{document}

\setlength{\bcw}{((\columnwidth / 30))} %  basic column width is half the width of narrowest column

\noindent\rule{\columnwidth}{2pt}\smallskip % checking that the tabular do not extend into the margin

\begin{calstable}
\makeatletter
\colwidths{{3.5\bcw}{5.5\bcw}{5.5\bcw}{4\bcw}{3.5\bcw}{5.5\bcw}{2.5\bcw}{0pt}} % Sum should be 30
\def\cals@framers@width{0.4pt}
\def\cals@framecs@width{0.4pt}

%
\brow
    \def\cals@bgcolor{lightgray} % Color command is in effect until reset
    \nc{lrt}
    \nc{lrt}
    \nc{lrt}
    \nc{lrt}
    \nc{lrt}
    \nc{ltb}
    \nc{rtb}\alignC\sc{\textbf{Scores}}
    \cell{}
\erow
%
\brow
    \nc{lrb}\alignC\sc{\vfil \textbf{Question}} % Alignment command works until reset by a `\alignL` or `\alignR`
    \nc{lrb}\sc{\vfil a)}
    \nc{lrb}\sc{\vfil b)}
    \nc{lrb}\sc{\vfil c)}
    \nc{lrb}\sc{\vfil d)}
    \cell{Individual}
    \cell{Team}
    \cell{}
\erow
%
\brow
    \cell{1.}
    \def\cals@bgcolor{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
%
\brow
    \def\cals@bgcolor{lightgray}
    \cell{2.}
    \def\cals@bgcolor{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
%
\brow
    \def\cals@bgcolor{lightgray}
    \cell{3.}
    \def\cals@bgcolor{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
%
\brow
    \def\cals@bgcolor{lightgray}
    \cell{4.}
    \def\cals@bgcolor{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
%
\brow
    \def\cals@bgcolor{lightgray}
    \cell{5.}
    \def\cals@bgcolor{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
%
\brow
    \def\cals@bgcolor{lightgray}
    \cell{6.}
    \def\cals@bgcolor{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
%
\brow
    \def\cals@bgcolor{lightgray}
    \cell{7.}
    \def\cals@bgcolor{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
%
\brow
    \def\cals@bgcolor{lightgray}
    \cell{8.}
    \def\cals@bgcolor{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
%
\brow
    \def\cals@bgcolor{lightgray}
    \cell{9.}
    \def\cals@bgcolor{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
%
\brow
    \def\cals@bgcolor{lightgray}
    \cell{10.}
    \def\cals@bgcolor{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
\erow
%
\brow
    \def\cals@bgcolor{lightgray}
    \nc{ltb}
    \nc{tb}
    \nc{tb}
    \nc{tb}
    \nc{rtb}\alignR\sc{\textbf{TOTAL}}
    \cell{}
    \cell{}
    \cell{}
    \def\cals@bgcolor{}
\erow
\makeatother
\end{calstable}
\end{document}

enter image description here

2.‘booktabs’ version

Here is my attempt to code a ‘booktabs’-version of the tabular:

\documentclass[a4paper,12pt]{report}
\usepackage[margin=1.5cm]{geometry}
\usepackage{cals, calc}
\newlength{\bcw}
\thispagestyle{empty}

% Shortcuts
\let\nc=\nullcell
\let\sc=\spancontent

\begin{document}

\setlength{\bcw}{((\columnwidth / 30))} %  basic column width is half the width of narrowest column

\noindent\rule{\columnwidth}{2pt}\bigskip % checking that the tabular do not extend into the margin

\begin{calstable}
\colwidths{{1.25\bcw}{2.75\bcw}{5\bcw}{5\bcw}{4\bcw}{4\bcw}{5\bcw}{3\bcw}} % Sum should be 30
\makeatletter
\def\cals@framers@width{0.8pt}
\def\cals@framecs@width{0pt}
\cals@setpadding{Ag}
\cals@setcellprevdepth{Al}
\def\cals@cs@width{0pt}
\def\cals@rs@width{0.4pt}

%
\setlength\cals@paddingR{0pt}
\thead{\bfseries\selectfont%
\brow
    \setlength\cals@paddingL{0pt}
    \nc{lt}
    \cals@setpadding{Ag}
    \nc{rt}
    \nc{lrt}
    \nc{lrt}
    \nc{lrt}
    \nc{lrt}
    \nc{ltb}
    \setlength\cals@paddingR{0pt}
    \nc{rtb}\alignC\sc{Scores}
    \cals@setpadding{Ag}    
\erow
%
\brow
    \setlength\cals@paddingL{0pt}
    \nc{lb}
    \cals@setpadding{Ag}
    \nc{rb}\alignL\sc{\vfil Question} % Alignment command works until reset by a `\alignL` or `\alignR`
    \nc{lrb}\alignC\sc{\vfil a)}
    \nc{lrb}\sc{\vfil b)}
    \nc{lrb}\sc{\vfil c)}
    \nc{lrb}\sc{\vfil d)}
    \cell{Individual}
    \setlength\cals@paddingR{0pt}
    \cell{Team}
    \cals@setpadding{Ag}    
\erow
\mdseries}
%
\brow
    \setlength\cals@paddingL{0pt}
    \alignR\cell{1.}
    \cals@setpadding{Ag}
    \cell{}
    \alignC\cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \setlength\cals@paddingR{0pt}
    \cell{}
    \cals@setpadding{Ag}    
\erow
%
\brow
    \def\cals@rs@width{0pt}
    \setlength\cals@paddingL{0pt}
    \alignR\cell{2.}
    \cals@setpadding{Ag}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \setlength\cals@paddingR{0pt}
    \cell{}
    \cals@setpadding{Ag}    
\erow
%
\brow
    \setlength\cals@paddingL{0pt}
    \alignR\cell{3.}
    \cals@setpadding{Ag}
    \cell{}
    \alignC\cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \setlength\cals@paddingR{0pt}
    \cell{}
    \cals@setpadding{Ag}    
\erow
%
\brow
    \setlength\cals@paddingL{0pt}
    \alignR\cell{4.}
    \cals@setpadding{Ag}
    \cell{}
    \alignC\cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \setlength\cals@paddingR{0pt}
    \cell{}
    \cals@setpadding{Ag}    
\erow
%
\brow
    \setlength\cals@paddingL{0pt}
    \alignR\cell{5.}
    \cals@setpadding{Ag}
    \cell{}
    \alignC\cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \setlength\cals@paddingR{0pt}
    \cell{}
    \cals@setpadding{Ag}    
\erow
%
\brow
    \setlength\cals@paddingL{0pt}
    \alignR\cell{6.}
    \cals@setpadding{Ag}
    \cell{}
    \alignC\cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \setlength\cals@paddingR{0pt}
    \cell{}
    \cals@setpadding{Ag}    
\erow
%
\brow
    \setlength\cals@paddingL{0pt}
    \alignR\cell{7.}
    \cals@setpadding{Ag}
    \cell{}
    \alignC\cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \setlength\cals@paddingR{0pt}
    \cell{}
    \cals@setpadding{Ag}    
\erow
%
\brow
    \setlength\cals@paddingL{0pt}
    \alignR\cell{8.}
    \cals@setpadding{Ag}
    \cell{}
    \alignC\cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \setlength\cals@paddingR{0pt}
    \cell{}
    \cals@setpadding{Ag}    
\erow
%
\brow
    \setlength\cals@paddingL{0pt}
    \alignR\cell{9.}
    \cals@setpadding{Ag}
    \cell{}
    \alignC\cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \setlength\cals@paddingR{0pt}
    \cell{}
    \cals@setpadding{Ag}    
\erow
%
\brow
    \setlength\cals@paddingL{0pt}
    \alignR\cell{10.}
    \cals@setpadding{Ag}
    \cell{}
    \alignC\cell{}
    \cell{}
    \cell{}
    \cell{}
    \cell{}
    \setlength\cals@paddingR{0pt}
    \cell{}
    \cals@setpadding{Ag}    
\erow
%
\brow
    \setlength\cals@paddingL{0pt}
    \nc{ltb}
    \nc{tb}
    \nc{tb}
    \nc{tb}
    \nc{tb}
    \nc{rtb}\alignL\sc{\textbf{TOTAL}}
    \cals@setpadding{Ag}
    \cell{}
    \setlength\cals@paddingR{0pt}
    \cell{}
    \cals@setpadding{Ag}    
    \def\cals@rs@width{0.4pt}
\erow\makeatother
\end{calstable}

\end{document}

Here is the result:

enter image description here

You must log in to answer this question.

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