5

I tried to apply the method proposed in How to get column alignment in tabularx? to right align cells in a ltablex. However I have a problem when the size of the table is just enough for my columns: some cells end up on two lines, with the first one empty. Here is a minimal example:

\documentclass[a4paper, 12pt]{report}

\usepackage[french]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{etoolbox}
\AtBeginEnvironment{tabular}{\footnotesize}
\AtBeginEnvironment{tabularx}{\footnotesize}
\AtBeginEnvironment{longtable}{\footnotesize}

\usepackage{multirow}
\usepackage{ltablex}
\usepackage{array}
\newcolumntype{Y}{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}X}
\newcolumntype{Z}{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}X}


\begin{document}

%\renewcommand{\tabcolsep}{3pt}

\begin{tabularx}{160pt}{ X  Y  Z }
% head
  \hline 
  Name & 
  Long~name & 
  Name  \\
  & [unit] & [$longunit$]  \\* 
  \hline\hline 
\endhead
% foot
  \hline
\endfoot 
% body
\multirow{3}{1.6cm}{Text on two lines} & 0 & 0 \\*
 & 0 & 0   \\*
 & 0 & 0   \\
\end{tabularx}


\end{document}

I get this result:

Compilation result

I found out that removing the \hspace{0pt} in the Y and Z column type definition solves the problem, but as it might be useful in other situations I would like to know if there is a better way to deal with this ?

Also, if I uncomment the line that redefined the tabcolsep, the Y and Z columns become left aligned again. Is there a way to have right aligned or center columns with a lower tabcolsep?

3

The linebreak you can fix with

\newcolumntype{Y}{>{\centering\let\newline\\\arraybackslash\nobreak\hspace{0pt}}X}
\newcolumntype{Z}{>{\raggedleft\let\newline\\\arraybackslash\nobreak\hspace{0pt}}X}

\tabcolsep should be set with \setlength not \renewcommand (but it still behaves oddly here, out of time now...)

| improve this answer | |
  • The difference between the two cases (with the setting of \tabcolsep and without) seems to lie in the different branches taken by \ifdim \wd\@tempboxa <\TX@target in \TX@endtabularx. – Dan Oct 8 '13 at 22:05
  • Thank you for your answers. Any news regarding the \setlength of \tabcolsep ? – Don't panic Oct 11 '13 at 8:06
  • @Don'tpanic been busy,perhaps at the weekend... – David Carlisle Oct 11 '13 at 8:46

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.