0

In this question, we learned how to create a table span over several pages.

I want to extend upon this by centreing the contents of column 1 and 3, whilst retaining column 2 with its left alignment. Take here a simple MWE

\documentclass{article}

\usepackage{longtable}
\usepackage{lipsum} % just for dummy text- not needed for a longtable
\usepackage{booktabs}

\begin{document}
\lipsum[1]
\lipsum[1]
\lipsum[1]

%\begin{table}[h] 
%\centering
\begin{longtable}{ p{.10\textwidth}  p{.60\textwidth} p{.10\textwidth}} 
    %\begin{adjustbox}{width=0.95\textwidth}
    \toprule
ID & Severity & Yes \\ 
\midrule
foo & bar & . \\ 
foo & bar & . \\ 
foo & bar &  \\ 
foo & bar & . \\ 
foo & bar & . \\ 
foo & bar &  \\ 
foo & bar &  \\ 
foo & bar &  \\ 
foo & bar &  \\ 
foo & bar &  \\ 
\bottomrule
%\end{adjustbox}
\caption{Your caption here} % needs to go inside longtable environment
\label{tab:myfirstlongtable}
\end{longtable}
%\end{table} 

Table \ref{tab:myfirstlongtable} shows my first longtable.
\end{document}

Now where we have the

\begin{longtable}{ p{.10\textwidth} p{.60\textwidth} p{.10\textwidth}}

command, I have tried

\begin{longtable}{ c{.10\textwidth} p{.60\textwidth} c{.10\textwidth}}

and

\begin{longtable}{clc}{ p{.10\textwidth} p{.60\textwidth} p{.10\textwidth}}

however not yet availed....

1

Use the array package and inject \centering into the cell. In the last cell you then should also set the meaning of \\ back to the tabular meaning (\centering changes it):

\documentclass{article}

\usepackage{longtable,array}
\usepackage{lipsum} % just for dummy text- not needed for a longtable
\usepackage{booktabs}

\begin{document}
\lipsum[1]
\lipsum[1]
\lipsum[1]

%\begin{table}[h]
%\centering
\begin{longtable}{>{\centering}p{.10\textwidth}  p{.60\textwidth} >{\centering\arraybackslash}p{.10\textwidth}}
    %\begin{adjustbox}{width=0.95\textwidth}
    \toprule
ID & Severity & Yes \\
\midrule
foo & bar & . \\
foo & bar & . \\
foo & bar &  \\
foo & bar & . \\
foo & bar & . \\
foo & bar &  \\
foo & bar &  \\
foo & bar &  \\
foo & bar &  \\
foo & bar &  \\
\bottomrule
%\end{adjustbox}
\caption{Your caption here} % needs to go inside longtable environment
\label{tab:myfirstlongtable}
\end{longtable}
%\end{table}

Table \ref{tab:myfirstlongtable} shows my first longtable.
\end{document}

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.