1

How can I adjust the column spacing in a tabular environment and also center the contents?

The code I use is this:

\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}

\usepackage{amsmath}
\usepackage{unicode-math}

\begin{document}

\begin{center}

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

\hline

1 & 2\\
\hline
3 & 4\\
\hline

\end{tabular}

\end{center}

\end{document}

Edit I:

Also while I was working in that code trying to create a second table under the first one both with the same width I ended up with the second being larger, although the width I gave was the same. Why is that happening?

\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}

\usepackage{amsmath}
\usepackage{unicode-math}

\begin{document}

\begin{center}

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

\hline

1 & 2\\
\hline
3 & 4\\
\hline

\end{tabular}

\begin{tabular}{|p{3cm}|p{3cm}|p{4cm}|}

\hline
1 & 2 & 3\\
\hline
4 & 5 & 6\\
\hline


\end{tabular}

\end{center}

\end{document}
  • 1
    there is tabcolsep padding either side of each cell, and arrayrulewidth of rule so the first table is10cm+4\tabcolsep+3\arrayrulewidth and the second is 10cm+6\tabcolsep+4\arrayrulewidth – David Carlisle Mar 25 '14 at 19:32
  • use \centering\arraybackslash to center text – David Carlisle Mar 25 '14 at 19:34
  • Thank you. How can I make them to have the same width without trial and error? – Adam Mar 25 '14 at 21:04
  • 1
    if you \usepackage{calc} you can use infix arithmetic so p{3cm-2\tabcolsep-\arrayrulewidth}| is a column of exactly 3cm including padding and the rule. – David Carlisle Mar 25 '14 at 21:06
  • Also how do I use this command \centering\arraybackslash you mentioned earlier? – Adam Mar 25 '14 at 21:09
3

You can centre an individual entry (which is really just a \parbox) using

 1 &\centering 2 & 3\\

However \centering redefines \\ so in the last column you need to use \tabularnewline

1 &\centering 2 &\centering 3\tabularnewline

Usually though you want to do this for a whole column so use the array package and then a column specification of

>{\centering\arraybackslash}p{3cm}

applies centering and redefines \\ to end a tabular row, in every entry in that column.

enter image description here

In the first table I centre a single entry 4 in teh second I centre all entries in the middle column. Both tables 10cm wide.

\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}

\usepackage{amsmath}
\usepackage{array,calc}

\begin{document}

\begin{center}

\begin{tabular}{|p{8cm-2\tabcolsep-2\arrayrulewidth}|
                 p{2cm-2\tabcolsep-\arrayrulewidth}|}

\hline

1111 & 2222222\\
\hline
3 & \centering\arraybackslash 4\\
\hline

\end{tabular}

\begin{tabular}{|p{3cm-2\tabcolsep-\arrayrulewidth}|
                 >{\centering\arraybackslash}p{3cm-2\tabcolsep-\arrayrulewidth}|
                 p{4cm-2\tabcolsep-2\arrayrulewidth}|}

\hline
111 & 2222 & 3333\\
\hline
4 & 5 & 6\\
\hline


\end{tabular}

\end{center}

\end{document}
| improve this answer | |
  • I can't make the code to run. Where shall I place the code you have given me? – Adam Mar 26 '14 at 18:02
  • @Adam added some code and a picture – David Carlisle Mar 26 '14 at 18:41

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.