3

I would like to find an easy way to adjust cells in my longtable to centered, while still using the p{} definition of the columns.

The code I'm using is:

\documentclass[a4paper]{article}
\usepackage{lipsum}
\usepackage{longtable}
\usepackage{colortbl}
\usepackage{xcolor}

\newlength\mylength

\begin{document}

\lipsum

\setlength\mylength{\dimexpr\textwidth-5\arrayrulewidth-8\tabcolsep}
\begin{longtable}{|p{.25\mylength}|p{.25\mylength}|p{.25\mylength}|p{.25\mylength}|}
\rowcolor{white}\caption{Test table\label{tab:test1}}
                \hline
                \rowcolor{blue}\textcolor[rgb]{1,1,1}{Term 1} & \textcolor[rgb]{1,1,1}{Term 2} & \textcolor[rgb]{1,1,1}{Term 3} & \textcolor[rgb]{1,1,1}{Term 4}\\
                \hline
                Test 1 & Test 2 & Test 3 & Test 4\\
                \hline
\end{longtable}

\end{document}

Which gives the following output:

enter image description here

How do I center the cells of the table? I would like to be able to perform both options:

  1. Center all the cells in the table
  2. Center some cells individually, and keep the others as they are
  • same as tabular, array package then >{\centering\arraybackslash}p{... – David Carlisle Sep 9 '15 at 13:31
  • Thank you for your fast response, David. I noticed you use > in your example code. I am not familiar with this. Will I be able to do this with the array rules I'm currently using? If I change the '|' with '>' I will lose my column array rules. – BenneB Sep 9 '15 at 13:39
3

For first question, you can define a new column type

\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

and use C instead of p.

\documentclass[a4paper]{article}
\usepackage{lipsum}
\usepackage{longtable,array}
\usepackage{colortbl}
\usepackage{xcolor}

\newlength\mylength
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}

\lipsum

\setlength\mylength{\dimexpr\textwidth-5\arrayrulewidth-8\tabcolsep}
\begin{longtable}{|C{.25\mylength}|C{.25\mylength}|C{.25\mylength}|C{.25\mylength}|}
\rowcolor{white}\caption{Test table\label{tab:test1}}\\
                \hline
                \rowcolor{blue}\textcolor[rgb]{1,1,1}{Term 1} & \textcolor[rgb]{1,1,1}{Term 2} & \textcolor[rgb]{1,1,1}{Term 3} & \textcolor[rgb]{1,1,1}{Term 4}\\
                \hline
                Test 1 & Test 2 & Test 3 & Test 4\\
                \hline
\end{longtable}

\end{document}

enter image description here

For centering individual cells, you can use \centering inside the cell like below:

\documentclass[a4paper]{article}
\usepackage{lipsum}
\usepackage{longtable,array}
\usepackage{colortbl}
\usepackage{xcolor}

\newlength\mylength
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}

\lipsum

\setlength\mylength{\dimexpr\textwidth-5\arrayrulewidth-8\tabcolsep}
\begin{longtable}{|p{.25\mylength}|C{.25\mylength}|C{.25\mylength}|p{.25\mylength}|}
\rowcolor{white}\caption{Test table\label{tab:test1}}\\
                \hline
                \rowcolor{blue}\centering\textcolor[rgb]{1,1,1}{Term 1} & \textcolor[rgb]{1,1,1}{Term 2} & \textcolor[rgb]{1,1,1}{Term 3} & \textcolor[rgb]{1,1,1}{Term 4}\\
                \hline
                Test 1 & Test 2 & Test 3 & \centering\arraybackslash Test 4\\
                \hline
\end{longtable}

\end{document}

enter image description here

Remember to add \arraybackslash if you are using the \centering in the last column.

  • Perfect. This is what I needed. – BenneB Sep 9 '15 at 16:32

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.