1

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}
ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
  • Welcome to TeX.SX! A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). You can also use backticks ``` to mark your inline code as I did in my edit. – Adam Liter Sep 19 '13 at 03:02
  • Please when quoting error messages always give exactly the error given, There is no \hline not aligned error in LaTeX, which made your question initially very confusing. The error you got is ! Misplaced \noalign. \hline ->\noalign – David Carlisle Sep 19 '13 at 08:23

1 Answers1

2

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

Gonzalo Medina
  • 505,128
  • Thank you! This works. Do you have any suggestions on a less restrictive placement specifier - it would be great if I can take more control of the placement of the table. – user36923 Sep 19 '13 at 04:17
  • @user36923 You're welcome! What exactly do you mean with "take more control"? Do you want to suppress flotation at all (i.e., that the table appears exactly where you write it in the code)? For a nice explanation about floating (in particular, the placement specifiers) you can see http://tex.stackexchange.com/q/39017/3954 and Also, don't forget to accept the answer, if you consider it solved your problem, by clicking the checkmark to its left. In case of doubt, please see How do you accept an answer?. – Gonzalo Medina Sep 19 '13 at 04:25
  • Hi, I tried siunitx using the following code. I was able to get the alignment that I wanted, but lost the vertical line. Any suggestions? Thanks in advance, \documentclass{article} \usepackage{array} \usepackage{siunitx} \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} S[table-format=9.6] | C{2.6cm} | C{2.6cm} | C{2.6cm} |} \hline & \bfseries 1988 & {\centering} {\bfseries 1989} & \bfseries 1990 & \bfseries Change \\hli – user36923 Sep 19 '13 at 04:43
  • @user36923 since this is not directly related to the original issue, please open a fresh new question; ideally in this site, there should be one question per post. – Gonzalo Medina Sep 19 '13 at 04:51