使用内表和水平线对齐表格

使用内表和水平线对齐表格

这张表有 3 个问题。经过多次试验,我可以解决其中 1 或 2 个,但永远无法同时解决所有 3 个。我有一个非常挑剔的客户(他们不是都这样吗),我必须把这件事做好。

以下是代码:

\PassOptionsToPackage{table}{xcolor}
\documentclass[twoside]{article}
\usepackage{longtable}
\usepackage{multirow}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[table]{xcolor}
\usepackage{calc,adjustbox}
\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
\newcommand{\titlebar}{\titlecolor \textbf{Item} & \adjustbox{valign=t}{
    \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
\endfirsthead
\multicolumn{2}{l}{\textit{Continued from previous page}}\\
\titlebar \hline
\endhead

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


Type One    & \adjustbox{valign=t}{
    \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

Type Two    & \adjustbox{valign=t}{
    \begin{tabular}{D Q N}
        \rba Green sprockets & 1 & Army green\\ 
        \rbb Painted Magnolia & & very messy\\ 
        \rba Yellow Legumes & 3 & 
                old and faded\newline
                another line\newline
                yet more lines\\
        \rbb Red Carnations & & odour free\\ 
    \end{tabular}} \\ \hline

Type Three  & \adjustbox{valign=t}{
    \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}

以下是它的结果: 表3

问题 1:我无法让 \hline 与桌子的边缘完全对齐。

问题 2:我需要文本(绿色链轮、彩绘木兰等)准确地从替代阴影的开始点开始,并且没有任何填充。

问题 3:在 Acrobat 上以 100% 的比例查看时,或者在 iPad 或 Android 设备上查看时,主要行之间的 \hline 不可见。

这是其屏幕截图。 禁止使用

当然,作为 LaTeX 的初学者,我可能完全做错了,也许有更好的方法来实现我的目标。

答案1

您的\hlines 没有对齐,因为后面有虚假空格

Type One    & \adjustbox{valign=t}{

和类似的,你用一个开放的 来结束一行。这可以通过在末尾{添加一个来消除,例如%

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

至于填充,你必须将其减少tabcolsep到零,这是在

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

Acrobat 中不可见的线条是阅读器的问题。(例如,在 Acrobat 中,放大 75% 后,它们仍部分可见)实际打印文档时,您应该能看到所有这些线条。

代码:

%\PassOptionsToPackage{table}{xcolor}
\documentclass[twoside]{article}
\usepackage{longtable}
\usepackage{multirow}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[table]{xcolor}
\usepackage{calc,adjustbox}
\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
\newcommand{\titlebar}{\titlecolor \textbf{Item} & \adjustbox{valign=t}{%
    \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
\endfirsthead
\multicolumn{2}{l}{\textit{Continued from previous page}}\\
\titlebar \hline
\endhead

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


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

    \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

Type Two    & \adjustbox{valign=t}{%
    \begin{tabular}{@{}D Q N}
        \rba Green sprockets & 1 & Army green\\
        \rbb Painted Magnolia & & very messy\\
        \rba Yellow Legumes & 3 &
                old and faded\newline
                another line\newline
                yet more lines\\
        \rbb Red Carnations & & odour free\\
    \end{tabular}} \\ \hline

Type Three  & \adjustbox{valign=t}{%
    \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}

在此处输入图片描述

另一个选择是使用 booktabs包来获得比更优雅的规则\hlinebooktabs你的表格变成:

%\PassOptionsToPackage{table}{xcolor}
\documentclass[twoside]{article}
\usepackage{longtable}
\usepackage{multirow}
\renewcommand{\familydefault}{\sfdefault}
\usepackage[table]{xcolor}
\usepackage{calc,adjustbox}
\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
\newcommand{\titlebar}{\titlecolor \textbf{Item} & \adjustbox{valign=t}{%
    \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
\endfirsthead
\multicolumn{2}{l}{\textit{Continued from previous page}}\\
\titlebar \midrule
\endhead

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


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

    \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

Type Two    & \adjustbox{valign=t}{%
    \begin{tabular}{@{}D Q N}
        \rba Green sprockets & 1 & Army green\\
        \rbb Painted Magnolia & & very messy\\
        \rba Yellow Legumes & 3 &
                old and faded\newline
                another line\newline
                yet more lines\\
        \rbb Red Carnations & & odour free\\
    \end{tabular}} \\ \midrule

Type Three  & \adjustbox{valign=t}{%
    \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}

在此处输入图片描述

相关内容