[Tex/LaTex] \hline not aligned and move table to the left

tables

I used column type to define the width of a table, but it shows an error message of "\hline not aligned". If I just use \begin{tabular}{| l | c | c | c | c |}, the error message goes away but the columns are really narrow. Also, how can I move the table to the left of the document. Thanks so much for any help.

————- code —————–

\documentclass{article}
\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\begin{document}

\begin{table}[!h]
\begin{tabular}{| L{4.2cm} | C{2.6cm}}  | C{2.6cm} | C{2.6cm} | C{2.61cm} |} 
\hline 
& {\bf 1988} & {\bf 1989} & {\bf 1990} & {\bf Change} \\
\hline
Score & 249 & 234 & 266 & +17 \\
\hline
Percent High & 14 & 9 & 26 & +12 \\
\hline
\end{tabular}
\end{table}

\end{document}

Best Answer

You had an additional spurious closing brace in the first C{2.6cm} column; delete it. I used a \makebox to center the table with respect to the text area:

\documentclass{article}
\usepackage{array}

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

\begin{document}

\begin{table}
\makebox[\linewidth][c]{\begin{tabular}{| L{4.2cm} | C{2.6cm}  | C{2.6cm} | C{2.6cm} | C{2.61cm} |} 
\hline 
& \bfseries 1988 & \bfseries 1989 & \bfseries 1990 & \bfseries Change \\
\hline
Score & 249 & 234 & 266 & +17 \\
\hline
Percent High & 14 & 9 & 26 & +12 \\
\hline
\end{tabular}}
\end{table}

\end{document}

Some comments:

  • I changed the definitions of your columns, suppressing the \let and \hspace commands.

  • \bf is an old command that shouldn't be used anymore; use \bfseries instead; since cells form a group there's no need for explicit grouping.

  • As a suggestion, don't use the too restrictive option [!h] as placement specifier; use a less restrictive one (or don't use any at all).

  • Please (as a suggestion), consider using the booktabs package for your tables (they will look a lot better; no vertical rules, though).

  • Since your table will have numeric values, you could consider using the siunitx for possible alignment.

Just for comparison, the original table and the same table using booktabs:

\documentclass{article}
\usepackage{array}
\usepackage{booktabs}

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

\begin{document}

\begin{table}
\makebox[\linewidth][c]{\begin{tabular}{| L{4.2cm} | C{2.6cm}  | C{2.6cm} | C{2.6cm} | C{2.61cm} |} 
\hline 
& \bfseries 1988 & \bfseries 1989 & \bfseries 1990 & \bfseries Change \\
\hline
Score & 249 & 234 & 266 & +17 \\
\hline
Percent High & 14 & 9 & 26 & +12 \\
\hline
\end{tabular}}
\end{table}

\begin{table}[!ht]% [!ht] used just for the example
\makebox[\linewidth][c]{\begin{tabular}{ L{4.2cm} C{2.6cm} C{2.6cm} C{2.6cm} C{2.61cm}} 
\toprule
& \bfseries 1988 & \bfseries 1989 & \bfseries 1990 & \bfseries Change \\
\midrule
Score & 249 & 234 & 266 & +17 \\
Percent High & 14 & 9 & 26 & +12 \\
\bottomrule
\end{tabular}}
\end{table}

\end{document}

enter image description here

Related Question