表格单元格中的文本居中

表格单元格中的文本居中

我尝试通过定义新的列类型将文本置于表格单元格的中心 =\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

然而,在我的 MWE 中,我的内容仍然没有集中

梅威瑟:

\documentclass{article}

\usepackage{multirow}
\usepackage{multicol}
\usepackage{array}

\newcounter{vonecount}
\setcounter{vonecount}{0}
\newcommand{\vone}{\stepcounter{vonecount}\thevonecount}

\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\begin{tabular}{r|C{2mm}|C{2mm}|C{2mm}|C{2mm}|C{2mm}|C{2mm}|C{2mm}|C{2mm}|C{2mm}|C{2mm}|C{2mm}|C{2mm}|C{2mm}|C{2mm}|C{2mm}|C{2mm}|C{2mm}|C{2mm}} 
\cline{1-18}
\multicolumn{1}{|r|}{Section One}                           &   
\multicolumn{17}{>{\centering}p{4in}|}{Part One - Synonyms} \\ 
\cline{1-18}
Question \#     &   
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone \\
\cline{2-18}
Correct Answer  & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A \\
Your Answer     & & & & & & & & & & & & & & & & & &  \\
\cline{1-18}
\multicolumn{1}{|r|}{Section One}                                       &   
\multicolumn{17}{>{\centering}p{4in}|}{Part Two - Sentence Completions} \\ 
\cline{1-18}
Question \#     &   
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone & 
\vone \\
\cline{2-18}
Correct Answer  &   
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A & 
A \\
Your Answer     & & & & & & & & & & & & & & & & & &  \\
\hline
\multirow{2}{*}{Scores}                 &   
\multicolumn{4}{c|}{Correct}            &   
\multicolumn{4}{c|}{Total}              &   
\multicolumn{4}{c|}{Stanine}            &   
\multicolumn{5}{c|}{Percentile Rank}    \\
\cline{2-18}
                        &   
\multicolumn{4}{c|}{}   &   
\multicolumn{4}{c|}{}   &   
\multicolumn{4}{c|}{}   &   
\multicolumn{5}{c|}{}   \\
\cline{2-18}
\end{tabular}

\end{document}

答案1

你可以通过这个包获得你想要的东西tabularx

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{multirow}
\usepackage{array, colortbl, tabularx}

\newcommand\redhline{\arrayrulecolor{red}\hline\arrayrulecolor{black}}
\newcommand\vredrule{\color{red}\vrule}
\newcounter{vonecount}
\setcounter{vonecount}{0}
\newcommand{\vone}{\stepcounter{vonecount}\thevonecount}

\newcolumntype{C}[1]{>{\centering\arraybackslash}X}

\usepackage{makecell}
\setcellgapes{5pt}
\makegapedcells

\begin{document}

Text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text. \\
%
%\begin{center}
%\setlength\tabcolsep{5pt}
%\begin{tabular}{r|*{17}{c|}}
%\redhline
%\multicolumn{1}{!{\vredrule}r!{\vredrule}}{Section One} &
%\multicolumn{17}{c!{\vredrule}}{Part One -- Synonyms} \\
%\redhline
%Question \# &
%\vone & \vone & \vone & \vone & \vone & \vone & \vone & \vone & \vone
%& \vone & \vone & \vone & \vone & \vone & \vone & \vone & \vone \\
%\cline{2-18}
%Correct Answer &
%A & A &A & A & A & A & A & A & A &
%A & A & A & A & A & A & A & A \\
%Your Answer & & & & & & & & & & & & & & & & & \\
%\redhline
%\multicolumn{1}{!{\vredrule}r!{\vredrule}}{Section One} &
%\multicolumn{17}{c!{\vredrule}}{Part Two -- Sentence Completions} \\
%\arrayrulecolor{red}\hline\arrayrulecolor{black}
%Question \# &
%\vone & \vone & \vone & \vone & \vone & \vone & \vone & \vone & \vone
%& \vone & \vone & \vone & \vone & \vone & \vone & \vone & \vone \\
%\cline{2-18}
%Correct Answer &
%A & A & A & A & A & A & A & A & A &
%A & A & A & A & A & A & A & A \\
%Your Answer & & & & & & & & & & & & & & & & & \\
%\redhline
%\multicolumn{1}{!{\vredrule}r!{\vredrule}}{\multirow{2}{*}{Scores}}&
%\multicolumn{4}{c|}{Correct} &
%\multicolumn{4}{c|}{Total} &
%\multicolumn{4}{c|}{Stanine} &
%\multicolumn{5}{c!{\vredrule}}{Percentile Rank} \\
%\cline{2-18}
%\multicolumn{1}{!{\vredrule}r!{\vredrule}}{}&
%\multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} & \multicolumn{5}{c!{\vredrule}}{} \\
%\redhline
%\end{tabular}
%\end{center}

\noindent
\begin{tabularx}{\linewidth}{r|*{17}{X|}}
\redhline
\multicolumn{1}{!{\vredrule}r!{\vredrule}}{Section One} &
\multicolumn{17}{c!{\vredrule}}{Part One -- Synonyms} \\
\redhline
Question \# &
\vone & \vone & \vone & \vone & \vone & \vone & \vone & \vone & \vone
& \vone & \vone & \vone & \vone & \vone & \vone & \vone & \vone \\
\cline{2-18}
Correct Answer &
A & A &A & A & A & A & A & A & A &
A & A & A & A & A & A & A & A \\
Your Answer & & & & & & & & & & & & & & & & & \\
\redhline
\multicolumn{1}{!{\vredrule}r!{\vredrule}}{Section One} &
\multicolumn{17}{c!{\vredrule}}{Part Two -- Sentence Completions} \\
\arrayrulecolor{red}\hline\arrayrulecolor{black}
Question \# &
\vone & \vone & \vone & \vone & \vone & \vone & \vone & \vone & \vone
& \vone & \vone & \vone & \vone & \vone & \vone & \vone & \vone \\
\cline{2-18}
Correct Answer &
A & A & A & A & A & A & A & A & A &
A & A & A & A & A & A & A & A \\
Your Answer & & & & & & & & & & & & & & & & & \\
\redhline
\multicolumn{1}{!{\vredrule}r!{\vredrule}}{\multirow{2}{*}{Scores}}&
\multicolumn{4}{c|}{Correct} &
\multicolumn{4}{c|}{Total} &
\multicolumn{4}{c|}{Stanine} &
\multicolumn{5}{c!{\vredrule}}{Percentile Rank} \\
\cline{2-18}
\multicolumn{1}{!{\vredrule}r!{\vredrule}}{}&
\multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} & \multicolumn{5}{c!{\vredrule}}{} \\
\redhline
\end{tabularx}
\end{document} 

在此处输入图片描述

相关内容