3

My code follows:

\documentclass{book}
\usepackage{color,colortbl}
\begin{document}

\begin{tabular}{ll>{\color{red}}p{20pc}}
a &1 &Test
\end{tabular}

\end{document}

The output was:

enter image description here

Some unnecessary vertical space came before the last column. If I remove the tag \color{red} then everything works fine, how can I fix this?

Above said issue was fixed based on the suggestion given by David Carlisle and CarLaTeX, but the same problem was there if I use the supertabular environment, I used supertabular package for auto breaking table and the code follows:

\documentclass{book}
\usepackage{color,colortbl,supertabular}
\begin{document}

\begin{supertabular}{ll>{\leavevmode\color{red}}p{4pc}>{\leavevmode\color{black}}l>{\leavevmode\color{black}}l>{\leavevmode\color{red}}p{4pc}}
a &1 &Test &a &1 &Test\\
a &1 &Test &a &1 &Test\\
\end{supertabular}

\end{document}
  • Off-topic: The instruction \usepackage{color,colortbl} isn't quite correct. You should use \usepackage[table]{xcolor} to maximize the interoperability of the [x]color and colortbl packages. – Mico Mar 5 '18 at 8:10
2

Put \leavevmode before \color{red}, to ensure that the vertical mode is ended and the horizontal one is entered.

For more info, see: Function and usage of \leavevmode or read the footnote on page 6 of "Packages in the 'graphics' bundle". If you have TeX Live, you'll easily find it with texdoc grfguide or texdoc color.

\documentclass{book}
\usepackage{color}
\usepackage{array}

\begin{document}

    \begin{tabular}{ll>{\leavevmode\color{red}}p{20pc}}
        a &1 & Test \\
    \end{tabular}   

\end{document}

enter image description here

P.S. = I found the solution in David Carlisles's comment to this question: How do I color the font in just one row/column without the text jumping a line?

  • Or of course there is always the documentation: texdoc grfguide footnote on page 6:-) – David Carlisle Mar 5 '18 at 7:47
  • @DavidCarlisle Is there some who reads the documentation? :):):) However, in this case, it is a bit difficult to link the problem with that document... – CarLaTeX Mar 5 '18 at 7:50
  • texdoc color also works, of course texdoc xcolor probably works as well but the documentation there is longer (and naturally I haven't read that:-) – David Carlisle Mar 5 '18 at 7:52
  • @DavidCarlisle xcolor documentation only cite \leavevmode when it talks about \textcolor. – CarLaTeX Mar 5 '18 at 8:03
  • @DavidCarlisle and CarLaTeX both of your suggestion works fine for normal tabular, if I use supertabular then the same problem happens, please refer the modified question... – Balaji Mar 5 '18 at 9:46
2

supertablular apparently changes the table preamble not to be colour safe, adding extra groups fixes that (but adds additional vertical space unless you add negative space at the end of p columns)

\documentclass{book}
\usepackage{color,colortbl,supertabular}
\begin{document}

\begin{supertabular}{
l
l
>{\bgroup\leavevmode\color{red}}p{4pc}<{\endgraf\egroup}
>{\bgroup\leavevmode\color{black}}l<{\egroup}
>{\bgroup\leavevmode\color{black}}l<{\egroup}
>{\bgroup\leavevmode\color{red}}p{4pc}<{\endgraf\egroup}
}
a &1 &Test &a &1 &Test\\
a &1 &Test &a &1 &Test\\
\end{supertabular}

\end{document}
  • Is it documented? LOL (btw, +1, of course) – CarLaTeX Mar 5 '18 at 15:41
  • @CarLaTeX I doubt it (but didn't look:-) – David Carlisle Mar 5 '18 at 15:45
1

I've no idea why it happened! but using the m option as defined in array package will solve the issue.

\documentclass{book}
\usepackage{color,colortbl}
\begin{document}

\begin{tabular}{ll>{\color{red}}m{20pc}}
a &1 &Test\\
\end{tabular}

\end{document}

enter image description here

  • 1
    no this does not solve the issue, and causes another issue that the baseline of Test is no longer the same as that of a 1. The issue is documented in the main graphics package documentaion texdoc grfguide footnote page 6. – David Carlisle Mar 5 '18 at 7:46
  • @DavidCarlisle Thanks for the explanation, but why does m work?! – javadr Mar 5 '18 at 14:21
  • m does not work, as I said in the initial comment, it vertically centres Test so it is no longer baseline aligned with "a" and "1" (b would be better than m) The alignment changes for p as the first item in the cell is a colour node not the first line of text. – David Carlisle Mar 5 '18 at 14:35

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.