帮助表格。如何在表格环境中设置不同大小的单元格?

帮助表格。如何在表格环境中设置不同大小的单元格?

抱歉,我犯了英文错误。我正在寻找如何在表格环境中设置不同大小的单元格,例如图片(我希望表格跨越整个页面宽度)。我该怎么做?

我正在使用这些包

\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}

谢谢大家。

在此处输入图片描述

答案1

借助该tabularx包,您可以确保表格的宽度与文本宽度相同。我为此声明了一个新的居中列类型。使用xcolortable选项可以使用rowcolor和命令。使用您可以更改类型列的宽度。(确保值的总和等于使用的类型列数(例如,对于 4 列,1.5+1.5+0.75+0.25=4)。columncolor>{\hsize=1.5\hsize}CC

最后,我不建议将包中的线条booktabs与垂直线结合使用(就像您在 MWE 中所做的那样)。您可能已经注意到垂直线中的小间隙,这是因为 booktabs 线设计用于没有垂直线的表格中。

在此处输入图片描述

\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}

如果您希望坚持使用booktabs线条,我建议采用没有垂直线、较少水平线和没有颜色的布局:

在此处输入图片描述

\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}

答案2

使用tabularx包:

\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}

答案3

1. 要求的布局

如果你想尝试不同的解决方案,特别是如果你建立一个类或模板,这里有一个使用的解决方案卡路里

\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}

在此处输入图片描述

2.'booktabs' 版本

以下是我尝试编写一个“booktabs”版本的表格:

\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}

结果如下:

在此处输入图片描述

相关内容