Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have tried to center text within a cell in my table by defining a new column type = \newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

Yet in my MWE, my contents are still not centering

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}
share|improve this question
    
Do you mean to have columns that are just two millimeters wide? Have you considered writing \multicolumn{17}{c}{Part One - Synonyms} for the header cell? –  Mico May 30 at 22:48
    
You are correct on the multicolumn. I forgot to change that after some adjustments I had made. Either way, I was speaking of the numbers established by the counters centered in their cells. –  Calhistorian May 30 at 22:56
1  
I think that they are centred relative to the width you have allowed. But 2mm is just not enough space for it to work properly. Could you not just use c in this case? –  cfr May 30 at 23:21
    
You are correct c does work, and fixes the problem. But the table is then too wide, past the textwidth. Would the solution be to change the environment type? I have switched it to 2.9mm (which keeps it within the text block), but there is still a slight discrepancy in center spacing in the double-digit numbers. –  Calhistorian May 30 at 23:26

1 Answer 1

You can obtain what you want with the tabularx package:

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

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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