# Table alignment with inner table and hline

There are 3 issues with this table. After much experimenting, I can sort of resolve 1 or 2 of them but never all 3 at the same time. I have a very picky customer (aren't they all) and I have to get this right.

Here is the code:

\PassOptionsToPackage{table}{xcolor}
\documentclass[twoside]{article}
\usepackage{longtable}
\usepackage{multirow}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[table]{xcolor}
\usepackage[a4paper,top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry}
\usepackage{pbox}

\begin{document}

%Colours used in the table
\newcommand{\titlecolor}{\rowcolor{gray!50}} %Color of titlbar
\newcommand{\raa}{\rowcolor{gray!30}}  % alternate set a rows
\newcommand{\rab}{\rowcolor{gray!15}}
\newcommand{\rba}{\rowcolor{gray!20}}  % alternate set b rows
\newcommand{\rbb}{\rowcolor{gray!15}}
\newcommand{\altrows}{\rowcolors{2}{gray!20}{gray!30}}

%Column types for main table
\newcolumntype{I}{
>{\textbf\bgroup}p{0.2\linewidth-2\tabcolsep}<{\egroup}} %Item
\newcolumntype{T}{@{}p{0.8\linewidth-2\tabcolsep}} %Inner table
%Column types for inner tables
\newcolumntype{D}{@{}p{0.43\linewidth-2\tabcolsep}} %Description
\newcolumntype{Q}{>{\hfill}p{0.07\linewidth-2\tabcolsep}} %Qty
\newcolumntype{N}{p{0.50\linewidth-\arrayrulewidth}} %Notes
%The title bar for the main tables
\begin{tabular}{@{}D p{0.07\linewidth-2\tabcolsep} N}
\textbf{Description} & \textbf{Qty} & \textbf{Notes} \\
\end{tabular} } \\
}

\altrows
\begin{longtable}[t] {I T}

\titlebar \hline
\multicolumn{2}{l}{\textit{Continued from previous page}}\\
\titlebar \hline

\rowcolor{white}\multicolumn{2}{r}{\textit{Continued on next page}} \\
\endfoot
\endlastfoot

\begin{tabular}{D Q N}
\raa Green sprockets &  1 &  \\
\rab Painted Magnolia &  & not green\newline not old \\
\raa Yellow Legumes & 3 &  old and faded blue things which don't wash well.\\
\rab Red Carnations & &  \\
\end{tabular}} \\ \hline

\begin{tabular}{D Q N}
\rba Green sprockets & 1 & Army green\\
\rbb Painted Magnolia & & very messy\\
\rba Yellow Legumes & 3 &
another line\newline
yet more lines\\
\rbb Red Carnations & & odour free\\
\end{tabular}} \\ \hline

\begin{tabular}{D Q N}
\raa Green sprockets &  1 &  \\
\rab Painted Magnolia &  & very messy\\
\raa Yellow Legumes & 3 &  old and faded blue things which don't wash well.\\
\rab Red Carnations & &  \\
\end{tabular}} \\ \hline

\end{longtable}

\end{document}


And here is what it produces:

Issue 1: I cannot get the \hline's to exactly line up with the edge of the table.

Issue 2: I need the text (Green sprockets, Painted Magnolia, etc) to start exactly at the point where the alternate shading starts and not have any padding.

Issue 3: When viewed on acrobat at 100%, or on an iPad or Android device, the \hline's in between the major rows are not visible.

Here is a screenshot of that.

Of course, as a beginner to LaTeX, I may be doing this all wrong and there may be a much better way of achieving my goals.

-
Welcome to TeX.SE. On some viewers the \hlines do not get displayed depending on the zoom. One way to "fix" that is to use a thicker rules something like: \setlength\arrayrulewidth{1pt}. – Peter Grill Oct 24 '12 at 19:38
That doesn't really solve the problem. At normal zoom the lines appear too dark and inconsistent in thickness, and they still can disappear on mobile devices. If there is no shading the standard lines work well so the problem appears to be that the shading overlaps the lines, rather than the other way round. – Peter Colman Oct 24 '12 at 22:22

Your \hlines are not aligned as you have spurious spaces after

Type One    & \adjustbox{valign=t}{


and similar where you end the line with an open {. This can be eliminated by putting a % at the end as

Type One    & \adjustbox{valign=t}{%


As far as padding is concerned, you have to reduce the tabcolsep to zero and it is done in

\newcolumntype{D}{>{\hspace{-\tabcolsep}}p{0.43\linewidth-2\tabcolsep}} %Description


And the invisible lines in acrobat is an issue of the reader. (They are visible partially again 75% at zoom in acrobat, for example) You should get all those lines when the document is actually printed.

The code:

%\PassOptionsToPackage{table}{xcolor}
\documentclass[twoside]{article}
\usepackage{longtable}
\usepackage{multirow}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[table]{xcolor}
\usepackage[a4paper,top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry}
\usepackage{pbox}

\begin{document}

%Colours used in the table
\newcommand{\titlecolor}{\rowcolor{gray!50}} %Color of titlbar
\newcommand{\raa}{\rowcolor{gray!30}}  % alternate set a rows
\newcommand{\rab}{\rowcolor{gray!15}}
\newcommand{\rba}{\rowcolor{gray!20}}  % alternate set b rows
\newcommand{\rbb}{\rowcolor{gray!15}}
\newcommand{\altrows}{\rowcolors{2}{gray!20}{gray!30}}

%Column types for main table
\newcolumntype{I}{%
>{\textbf\bgroup}p{0.2\linewidth-2\tabcolsep}<{\egroup}} %Item
\newcolumntype{T}{@{}p{0.8\linewidth-2\tabcolsep}} %Inner table
%Column types for inner tables
\newcolumntype{D}{>{\hspace{-\tabcolsep}}p{0.43\linewidth-2\tabcolsep}} %Description
\newcolumntype{Q}{>{\hfill}p{0.07\linewidth-2\tabcolsep}} %Qty
\newcolumntype{N}{p{0.50\linewidth-\arrayrulewidth}} %Notes
%The title bar for the main tables
\begin{tabular}{@{}D p{0.07\linewidth-2\tabcolsep} N}
\textbf{Description} & \textbf{Qty} & \textbf{Notes} \\
\end{tabular} } \\
}

\altrows
\begin{longtable}[t] {I T}

\titlebar \hline
\multicolumn{2}{l}{\textit{Continued from previous page}}\\
\titlebar \hline

\rowcolor{white}\multicolumn{2}{r}{\textit{Continued on next page}} \\
\endfoot
\endlastfoot

\begin{tabular}{@{}D Q N}%
\raa Green sprockets &  1 &  \\
\rab Painted Magnolia &  & not green\newline not old \\
\raa Yellow Legumes & 3 &  old and faded blue things which don't wash well.\\
\rab Red Carnations & &  \\
\end{tabular}} \\ \hline

\begin{tabular}{@{}D Q N}
\rba Green sprockets & 1 & Army green\\
\rbb Painted Magnolia & & very messy\\
\rba Yellow Legumes & 3 &
another line\newline
yet more lines\\
\rbb Red Carnations & & odour free\\
\end{tabular}} \\ \hline

\begin{tabular}{@{}D Q N}
\raa Green sprockets &  1 &  \\
\rab Painted Magnolia &  & very messy\\
\raa Yellow Legumes & 3 &  old and faded blue things which don't wash well.\\
\rab Red Carnations & &  \\
\end{tabular}} \\ \hline

\end{longtable}

\end{document}


Another option is to use booktabs package for more elegant rules than \hline. With booktabs your table becomes:

%\PassOptionsToPackage{table}{xcolor}
\documentclass[twoside]{article}
\usepackage{longtable}
\usepackage{multirow}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[table]{xcolor}
\usepackage[a4paper,top=2.5cm, bottom=2.5cm, left=2.5cm, right=2.5cm]{geometry}
\usepackage{pbox}
\usepackage{booktabs}

\begin{document}

%Colours used in the table
\newcommand{\titlecolor}{\rowcolor{gray!50}} %Color of titlbar
\newcommand{\raa}{\rowcolor{gray!30}}  % alternate set a rows
\newcommand{\rab}{\rowcolor{gray!15}}
\newcommand{\rba}{\rowcolor{gray!20}}  % alternate set b rows
\newcommand{\rbb}{\rowcolor{gray!15}}
\newcommand{\altrows}{\rowcolors{2}{gray!20}{gray!30}}

%Column types for main table
\newcolumntype{I}{%
>{\textbf\bgroup}p{0.2\linewidth-2\tabcolsep}<{\egroup}} %Item
\newcolumntype{T}{@{}p{0.8\linewidth-2\tabcolsep}} %Inner table
%Column types for inner tables
\newcolumntype{D}{>{\hspace{-\tabcolsep}}p{0.43\linewidth-2\tabcolsep}} %Description
\newcolumntype{Q}{>{\hfill}p{0.07\linewidth-2\tabcolsep}} %Qty
\newcolumntype{N}{p{0.50\linewidth-\arrayrulewidth}} %Notes
%The title bar for the main tables
\begin{tabular}{@{}D p{0.07\linewidth-2\tabcolsep} N}
\textbf{Description} & \textbf{Qty} & \textbf{Notes} \\
\end{tabular} } \\
}

\altrows
\begin{longtable}[t] {I T}

\titlebar \toprule
\multicolumn{2}{l}{\textit{Continued from previous page}}\\
\titlebar \midrule

\rowcolor{white}\multicolumn{2}{r}{\textit{Continued on next page}} \\
\endfoot
\endlastfoot

\begin{tabular}{@{}D Q N}%
\raa Green sprockets &  1 &  \\
\rab Painted Magnolia &  & not green\newline not old \\
\raa Yellow Legumes & 3 &  old and faded blue things which don't wash well.\\
\rab Red Carnations & &  \\
\end{tabular}} \\ \midrule

\begin{tabular}{@{}D Q N}
\rba Green sprockets & 1 & Army green\\
\rbb Painted Magnolia & & very messy\\
\rba Yellow Legumes & 3 &
another line\newline
yet more lines\\
\rbb Red Carnations & & odour free\\
\end{tabular}} \\ \midrule

\begin{tabular}{@{}D Q N}
\raa Green sprockets &  1 &  \\
\rab Painted Magnolia &  & very messy\\
\raa Yellow Legumes & 3 &  old and faded blue things which don't wash well.\\
\rab Red Carnations & &  \\
\end{tabular}} \\ \bottomrule

\end{longtable}

\end{document}


-
Perfect answer for first 2 issues and a possible for the third - I like it, we'll see what my client thinks :) I was wondering why many examples often had a % after a { - I hadn't realised that white space at that point was significant, thanks for clarifying. – Peter Colman Oct 25 '12 at 8:45
One question: I notice that placing the @{} in the column description (as in "\newcolumntype{D}{@{}>{\...") has exactly the same effect as placing it before the D in each tabular definition. Other than for versatility, is there any reason why I shouldn't just place it there? – Peter Colman Oct 25 '12 at 9:04