tabularx 中的多列单元格溢出到边缘

tabularx 中的多列单元格溢出到边缘

我的 中有这张表MWE。在第二个表中,单元格颜色似乎溢出到了边距。我尝试使用 和tabularx中的各种参数multicol来减少这种情况,但效果并不理想。是否有理由在同一页面上linewidth提供textwidth不同的宽度。换句话说,两个表都是相同的,除了多列,我认为这就是问题所在。

\documentclass[letterpaper]{memoir}
\usepackage{color, colortbl, longtable}
\usepackage{booktabs}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{multicol}
\usepackage{tabularx}
\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{center}
{\rowcolors{2}{black!10}{white}
\begin{tabularx}{\linewidth}{lX<{\raggedright}X<{\raggedright}}
\toprule
\textbf{Property}   &       \textbf{Addition}       &       \textbf{Multiplication} \\
\midrule
Associative     &   $\mathbf{A + (B + C) = (A + B) + C}$
                    $2 + (3 + 4) = (2 + 3) + 4$     
                                                    &       $\mathbf{A \times (B \times C) = 
                                                            (A \times B) \times C}$         
                                                            $2 \times (3 \times 4) = (2 \times 
                                                            3) \times 4$ \\
                &   Basically, if you only see addition 
                    signs around, it's okay, group the 
                    numbers however you want        &       Same deal here. If there's nothing 
                                                            but multiplication, it doesn't matter 
                                                            where the parentheses are. \\
\midrule
Commutative     &   $\mathbf{A + B = B + A}$ \newline
                    $5 + 7 = 7 + 5$                 &       $\mathbf{A \times B = B \times A}$ \newline
                                                            $5 \times 7 = 7 \times 5$ \\
                &   No big deal. Addition works in 
                    any order.                      &       Same! Multiplication doesn't care about 
                                                            order. \\
\midrule
Identity            &   $\mathbf{A + 0 = A}$ \newline
                    $3 + 0 = 3$                     &       $\mathbf{A \times 1 = A}$ \newline
                                                            $3 \times 1 = 3$ \\
                &   It's good to remember this one.
                    Anything plus zero is just 
                    itself.                         &       For multiplication, anything times one 
                                                            is itself. \\
\bottomrule
\end{tabularx} }

\bigskip
{\rowcolors{2}{black!10}{white}
\begin{tabularx}{\textwidth}{XX}
\toprule
        \multicolumn{2}{c}{\textbf{Distributive:}} \\       
\midrule
        $\mathbf{A(B + C) = A 
        \times B + A \times C}$         &   
                                            $7(3 + 1) = 7 \times 3 + 7 \times 1$    \\
        \multicolumn{2}{p{\textwidth}}{
        This will help you a lot. If there's
        more than one number inside 
        parentheses, then you can do 
        the operation (in this case, 
        multiplying by $7$) to each 
        number in the parentheses
        to simplify things.} \\
\bottomrule
\end{tabularx}}
\end{center}

\end{document}

答案1

你有两个\tabcolsep太多了。

\multicolumn{2}{p{\textwidth-2\tabcolsep}}

您获得了所需的宽度。加载calc包。请注意,这multicol不是必需的,它用于不同的目的(两列或更多列中的文本)。我留下来是draft为了表明您收到的框过满消息不是由于背景颜色,而是由于第一个表中的某些内容。

\documentclass[letterpaper,draft]{memoir}
\usepackage{color, colortbl, longtable,calc}
\usepackage{booktabs}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{tabularx}
\usepackage{lipsum}

\begin{document}

\lipsum[1]

\begin{center}
{\rowcolors{2}{black!10}{white}
\begin{tabularx}{\linewidth}{lX<{\raggedright}X<{\raggedright}}
\toprule
\textbf{Property}   &       \textbf{Addition}       &       \textbf{Multiplication} \\
\midrule
Associative     &   $\mathbf{A + (B + C) = (A + B) + C}$
                    $2 + (3 + 4) = (2 + 3) + 4$     
                                                    &       $\mathbf{A \times (B \times C) = 
                                                            (A \times B) \times C}$         
                                                            $2 \times (3 \times 4) = (2 \times 
                                                            3) \times 4$ \\
                &   Basically, if you only see addition 
                    signs around, it's okay, group the 
                    numbers however you want        &       Same deal here. If there's nothing 
                                                            but multiplication, it doesn't matter 
                                                            where the parentheses are. \\
\midrule
Commutative     &   $\mathbf{A + B = B + A}$ \newline
                    $5 + 7 = 7 + 5$                 &       $\mathbf{A \times B = B \times A}$ \newline
                                                            $5 \times 7 = 7 \times 5$ \\
                &   No big deal. Addition works in 
                    any order.                      &       Same! Multiplication doesn't care about 
                                                            order. \\
\midrule
Identity            &   $\mathbf{A + 0 = A}$ \newline
                    $3 + 0 = 3$                     &       $\mathbf{A \times 1 = A}$ \newline
                                                            $3 \times 1 = 3$ \\
                &   It's good to remember this one.
                    Anything plus zero is just 
                    itself.                         &       For multiplication, anything times one 
                                                            is itself. \\
\bottomrule
\end{tabularx} }

\bigskip
{\rowcolors{2}{black!10}{white}
\begin{tabularx}{\textwidth}{XX}
\toprule
        \multicolumn{2}{c}{\textbf{Distributive:}} \\       
\midrule
        $\mathbf{A(B + C) = A 
        \times B + A \times C}$         &   
                                            $7(3 + 1) = 7 \times 3 + 7 \times 1$    \\
        \multicolumn{2}{p{\textwidth-2\tabcolsep}}{
        This will help you a lot. If there's
        more than one number inside 
        parentheses, then you can do 
        the operation (in this case, 
        multiplying by $7$) to each 
        number in the parentheses
        to simplify things.} \\
\bottomrule
\end{tabularx}}
\end{center}

\end{document}

在此处输入图片描述

相关内容