3

I am writing LaTeX code to generate a long table using longtable package and I want the first column color to be gray

Here is my code:

\documentclass[a4paper,11pt,english]{report}

\usepackage{longtable}
 \usepackage{multirow}
 \usepackage{graphicx}
 \usepackage[table,xcdraw]{xcolor}
\usepackage{lipsum} % just for dummy text- not needed for a longtable

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

\definecolor{lightgray}{HTML}{EFEFEF}

\begin{longtable}[H]{|
>{\columncolor{lightgray}}p{0.2\linewidth} | p{0.175\linewidth} |}
\hline
foo & bar \\ \hline 
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
foo & bar \\ \hline
\caption{Your caption here} % needs to go inside longtable environment
\label{tab:myfirstlongtable}
\end{longtable} 

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

The problem with the produced table is that horizontal lines doesn't appear in the colored area. Here is my output: enter image description here

I know that the problem is related to the pdf viewer and that my code works fine with other pdf viewers but I am obliged to use this pdf viewer

Note: I solved this problem before in other tables using NiceTabular but NiceTabular is not compatible with longtables so I don't know how to solve this problem in longtable environment

Any help ?

2
  • 1
    unrelated but longtable doesn't have a [H] option. Commented Sep 22, 2021 at 10:52
  • 2
    You should have a look at tabularray... Commented Sep 22, 2021 at 11:39

3 Answers 3

3

Here is a solution with longtblr environment of tabularray package: horizontal lines will not be covered by colored cells.

Also it is much better to put caption before a long table.

\documentclass[a4paper,11pt,english]{report}

\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{tabularray}

\begin{document}

\lipsum[1]
\lipsum[1]
\lipsum[1]

\begin{longtblr}[
  caption = {Your caption here},
  label = {tab:myfirstlongtable},
]{
  colspec = {|Q[0.25\linewidth,gray9]|Q[0.25\linewidth]|},
  hlines,
}
  foo & bar \\ 
  foo & bar \\
  foo & bar \\
  foo & bar \\
  foo & bar \\
  foo & bar \\
  foo & bar \\
  foo & bar \\
  foo & bar \\
  foo & bar \\
  foo & bar \\
\end{longtblr} 

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

\end{document}

enter image description here

0

Hard to be definite as the effects depend on the pdf reader but you can arrange that the hlines come above rather than next to the coloured panels, and also make them a bick thicker you may need to experiment with the exact spacing

enter image description here

\documentclass[a4paper,11pt,english]{report}

\usepackage{longtable}
 \usepackage{multirow}
 \usepackage{graphicx}
 \usepackage[table,xcdraw]{xcolor}
\usepackage{lipsum} % just for dummy text- not needed for a longtable

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

\definecolor{lightgray}{HTML}{EFEFEF}
\setlength\arrayrulewidth{.5pt}
\begin{longtable}{|
>{\columncolor{lightgray}}p{0.2\linewidth} | p{0.175\linewidth} |}
\hline
foo & bar \\[.5pt]\noalign{\nobreak\vskip-.5pt} \hline\noalign{\nobreak\vskip.5pt}
foo & bar \\[.5pt]\noalign{\nobreak\vskip-.5pt} \hline\noalign{\nobreak\vskip.5pt}
foo & bar \\[.5pt]\noalign{\nobreak\vskip-.5pt} \hline\noalign{\nobreak\vskip.5pt}
foo & bar \\[.5pt]\noalign{\nobreak\vskip-.5pt} \hline\noalign{\nobreak\vskip.5pt}
foo & bar \\[.5pt]\noalign{\nobreak\vskip-.5pt} \hline\noalign{\nobreak\vskip.5pt}
foo & bar \\[.5pt]\noalign{\nobreak\vskip-.5pt} \hline\noalign{\nobreak\vskip.5pt}
foo & bar \\[.5pt]\noalign{\nobreak\vskip-.5pt} \hline\noalign{\nobreak\vskip.5pt}
foo & bar \\[.5pt]\noalign{\nobreak\vskip-.5pt} \hline\noalign{\nobreak\vskip.5pt}
foo & bar \\[.5pt]\noalign{\nobreak\vskip-.5pt} \hline\noalign{\nobreak\vskip.5pt}
foo & bar \\[.5pt]\noalign{\nobreak\vskip-.5pt} \hline\noalign{\nobreak\vskip.5pt}
foo & bar \\[.5pt]\noalign{\nobreak\vskip-.5pt} \hline\noalign{\nobreak\vskip.5pt}
\caption{Your caption here} % needs to go inside longtable environment
\label{tab:myfirstlongtable}
\end{longtable} 

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

I have no problem with a correct code, using the \endfirsthead,\endhead and \endfoot, \endlastfoot sections. Usually, I solve the problems with coloured cells loading the hhline package.

Note that traditionally, tables have caption on top, precisely for the case of tables spreading over several pages.

\documentclass[a4paper,11pt,english]{report}
\usepackage{babel}
\usepackage{longtable}
 \usepackage{multirow}
 \usepackage{graphicx}
 \usepackage[table,xcdraw]{xcolor}
\usepackage{hhline} 
\usepackage{lipsum} % just for dummy text- not needed for a longtable

\begin{document}

\lipsum[1]
\lipsum[1]
\lipsum[1]

\definecolor{lightgray}{HTML}{EFEFEF}

\begin{longtable}{|>{\columncolor{lightgray}}p{0.2\linewidth} | p{0.175\linewidth} |}
\caption{Your caption here}% needs to go inside longtable environment
\label{tab:myfirstlongtable}
\endfirsthead
\caption{Your caption here (continued)}\\
\hhline{|--|}
\endhead
\multicolumn{2}{r@{}}{\scriptsize (to be continued)}
\endfoot 
\hhline{|--|}
 \endlastfoot
\hhline{|--|}
foo & bar \\ \hhline{|--|}
foo & bar \\ \hhline{|--|}
foo & bar \\ \hhline{|--|}
foo & bar \\ \hhline{|--|}
foo & bar \\ \hhline{|--|}
foo & bar \\ \hhline{|--|}
foo & bar \\ \hhline{|--|}
foo & bar \\ \hhline{|--|}
foo & bar \\ \hhline{|--|}
foo & bar \\ \hhline{|--|}
foo & bar \\
\end{longtable}

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

enter image description here

9
  • 1
    I have compiled your code and, in several PDF viewers (e.g. SumatraPDF and Adobe Reader), some rules seem to vanish at some levels of zoom. I'm not surprised since it's a well-known problem of the tabulars done with array, tabular, longtable, etc. in conjonction with colortbl. The problem is solved by nicematrix and (afaik) by tabularray. Commented Sep 22, 2021 at 13:09
  • 1
    This is mainly a problem of the pdfviewer (the screenshot I posted is from SumatraPDF). Note that a simple way to have no problem with rules in coloured cells is often to use \hhlines in the place of \hlines.
    – Bernard
    Commented Sep 22, 2021 at 13:14
  • You consider that it's a problem of the PDF's viewer. So you say. That's not my point of view. Nevertheless, the question of the OP was exactly that point and so, your answer is not an answer to the question. Sorry. Commented Sep 22, 2021 at 13:20
  • @F.Pantigny: The screenshots in my answer are from SumatraPDF. I know there can be problems (often with \cline) but usually, when zooming, the lines are here again. Other than that, the use of hhline generally solves the problems.
    – Bernard
    Commented Sep 22, 2021 at 13:43
  • The problem with \cline has nothing to to with that problem (the fact is that \cline protudes in the cell below but the other classical rules don't). For the problem of the OP, I have written {NiceTabular} in such a way that the rules won't never seem to vanish, whatever PDF viewer you use, at all the levels of zoom. However, as said by the OP, {NiceTabular} can't be broken between pages. That's why the is looking for another solution. Commented Sep 22, 2021 at 14:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .