2

I want to create a longtable with headers centered, columns left or right aligned. Thanks to my previous question: Right aligning two columns next to each other in longtable and its great answer, I've crafted this example document:

\documentclass{article}
\usepackage{array}
\usepackage{seqsplit}
\usepackage{longtable}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}

\begin{document}
\begin{longtable}{|L{3.32cm}|R{3.97cm}|R{3.14cm}|L{3.57cm}|}
\hline
\multicolumn{1}{|c|}{ header1 } & \multicolumn{1}{c|}{header2} & \multicolumn{1}{c|}{ \seqsplit{The\_header3\_is\_longer\_than\_expected} } & \multicolumn{1}{c|}{What\_would\_happen\_if\_header4} \\
\hline
\endhead
\hline
some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment \\
some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment \\
some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment \\
some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment \\
\end{longtable}
\end{document}

The problem with this, is that seqsplit is not doing its job and splitting long words when width set is reached. See:

Latex example

2

You don't need \multicolumn because the columns are declared as p ones: use \centering and just remember to add \arraybackslash for the last one (or end the row with \tabularnewline.

\documentclass{article}
\usepackage{array}
\usepackage{seqsplit}
\usepackage{longtable}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}} % may come handy
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}

\begin{document}
\begin{longtable}{|L{3.32cm}|R{3.97cm}|R{3.14cm}|L{3.57cm}|}
\hline
\centering header1 &
  \centering header2 &
  \centering \seqsplit{The\_header3\_is\_longer\_than\_expected} & 
  \centering\arraybackslash \seqsplit{What\_would\_happen\_if\_header4} \\
\hline
\endhead
\hline
some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment \\
some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment \\
some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment \\
some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment & some text goes here to illustrate the alignment \\
\end{longtable}
\end{document}

enter image description here

1

This is not a problem with seqsplit. You explicitly told latex that your header is a centered line (c). It does what you asked. You need to make your multicolumn type p{...} for latex to split it.

  • isn't latex able to center text multiline? no way to achieve something like this, centered in a specific width? – maraujop Jun 17 '13 at 17:31
  • @maraujop: You can add \centering. – Heiko Oberdiek Jun 17 '13 at 18:06

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.