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}
TeXnician
  • 33,589
Balaji
  • 2,282
  • 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 05 '18 at 08:10

3 Answers3

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?

CarLaTeX
  • 62,716
  • Or of course there is always the documentation: texdoc grfguide footnote on page 6:-) – David Carlisle Mar 05 '18 at 07: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 05 '18 at 07: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 05 '18 at 07:52
  • @DavidCarlisle xcolor documentation only cite \leavevmode when it talks about \textcolor. – CarLaTeX Mar 05 '18 at 08: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 05 '18 at 09:46
  • @Balaji Can't you use longtable instead of supertabular? This works: `\documentclass{book} \usepackage[table]{xcolor} \usepackage{longtable} \begin{document}

    \begin{longtable}{ll>{\color{red}}p{4pc}ll>{\color{red}}p{4pc}} a &1 &Test &a &1 &Test\ a &1 &Test &a &1 &Test\ \end{longtable}

    \end{document}`

    – CarLaTeX Mar 05 '18 at 10:45
  • @CarLaTeX After changing longtable working fine, thanks a lot... – Balaji Mar 05 '18 at 14:16
  • @Balaji You're welcome, thank you for accepting my answer! – CarLaTeX Mar 05 '18 at 14:17
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}
David Carlisle
  • 757,742
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

javadr
  • 2,404
  • 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 05 '18 at 07:46
  • @DavidCarlisle Thanks for the explanation, but why does m work?! – javadr Mar 05 '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 05 '18 at 14:35