1

Hi guys I am new to LaTeX, I have a very large table with data in it which moves past the page. I have managed to fix it and adjust it according to the page but I lost the centering option for the data can you please tell me how to center the data while maintaining the specification I have provided for the length the columns I have attached the code I am using so you can understand me better

\begin{tabular}{ | p{5cm} | p{2cm} | p{2cm} | p{2cm} |}

I tried adding c to the code like | c p{5cm} | but it still wouldn't center the data inserted in the table. Thank you

  • You can repeat columns with *{3}{p{2cm}|}, small tip ;-) – user31729 Jul 24 '15 at 14:36
1

(1) Always post minimal examples that can be copied and compiled as is. This is a sniplet. MWEs makes it a lot easier to help

\usepackage{array}

 \begin{tabular}{ | 
  >{\centering\arraybackslash} p{5cm} 
   | >{\centering\arraybackslash}p{2cm} 
   | >{\centering\arraybackslash}p{2cm} 
   | >{\centering\arraybackslash}p{2cm} |}

array enables up to get the >{...} construction, so we insert \centering into each cell. But this messes with \\ so it nolonger means new row, \arraybackslash brings back the original table definition

And do consider if those lines are really necessary (they often make the tale much less readable)

1

Use the array package and define a new column type C which uses the p type and centers it.

It would be possible to write this directly in the tabular column definition, but the \newcolumntype is cleaner, especially if there are more than on columns to change.

\documentclass{article}

\usepackage{array}

\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}
\begin{tabular}{|C{5cm}|*{3}{C{2cm}|}}
\hline
a & b & c & d \tabularnewline
\hline
\end{tabular}
\end{document}

enter image description here

  • When you have the \arraybackslash is there then any reason to use \tabularnewline? – daleif Jul 24 '15 at 14:35
  • @daleif: No, my coding genes just force me to write \tabularnewline ;-) – user31729 Jul 24 '15 at 14:37
  • thank you so much guys ... issue resolved I will be soon posting a really complex table i made in word that i want to put in LaTeX... first one to solve it gets a cookie... let the games begin xD – Bakhtawar Khattak Jul 24 '15 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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