# Misplaced noalign when using >{\centering} [duplicate]

I have the below code, and I want to center the col. elemnts so I used {||>{\centering}p{0.5\textwidth}||>{\centering}>p{0.3\textwidth}|}, but I got this error "\misplaced noalign" for the \hline command .. any idea how to fix the problem?.

\begin{table}[h]
\label{1}
\begin{tabular}{|p{0.5\textwidth}|>p{0.3\textwidth}|}
\hline
\rowcolor{Gainsboro!60}
Student Name &Student ID \myhash \\
\hline
& \\
\hline
\end{tabular}
\end{table}

• Replace \centering with \centering\arraybackslash and add the array package. – leandriis Aug 8 '20 at 16:10
• it worked thanks – Diana Aug 8 '20 at 16:13

In order to horizontally center the contents of a p type column, use \centering\arraybackslash instead of just \centering. Additionally, and in order to use the >{...} syntax, you will also have to load the array package.

Thus, the following example should give you the desired result:

\documentclass{article}
\usepackage{array}
\usepackage[table,svgnames]{xcolor}
\begin{document}

\begin{table}[h]
\label{1}
\begin{tabular}{|p{0.5\textwidth}|>{\centering\arraybackslash}p{0.3\textwidth}|}
\hline
\rowcolor{Gainsboro!60}
Student Name &Student ID  \\
\hline
& \\
\hline
\end{tabular}
\end{table}
\end{document}


Since I assume student ames and their IDs are short enough to not require linebreaks within the corresponding table cells, you could also go for the w type columns that are available when loading the array package instead.

\begin{tabular}{|wl{0.5\textwidth}|wc{0.3\textwidth}|}
\hline
\rowcolor{Gainsboro!60}
Student Name &Student ID  \\
\hline
& \\
\hline
\end{tabular}


If you want your table to automaticaly adapt to the width of the widest contents without adding additional wite space, you can use a simple

\begin{tabular}{|l|c|}


A remark on the \label and the use of table: \label only really makes sense in combination with a corresponding \caption and if you later on want to reference the table. (e.g. as in "For an overview see table 1"). The table environment only makes sense if you want your table to float. If you don't want this behaviour, don't use table.