表格超出右边距

表格超出右边距

我在使用表格时遇到问题,表格超出了右边距。

下面是代码:

\documentclass[12 pt, a4paper]{book}
\usepackage{multicol}
\usepackage{geometry}
\geometry{
a4paper,
total={170 mm,257 mm},
left=20 mm,
top=20 mm,
}
\usepackage{amsmath,bm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{soul}
\usepackage{indentfirst}
\usepackage[utf8]{inputenc}
\usepackage{palatino}
\usepackage[dvipsnames]{xcolor}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{array}
\usepackage{enumitem}
\usepackage{mathtools}
\usepackage{cancel}
\usepackage{arydshln}
\usepackage{calc}
\usepackage{float}
\usepackage{ulem}
\usepackage{multirow}

\begin{table}[h]
            \setlength\extrarowheight{2pt}
            \begin{tabular}{| c | c | c | c |}
                \hline
                \multirow{2}{*}{\begin{tabular}{c}\textbf{Operations}\end{tabular}} & \multicolumn{3}{c |}{\textbf{Subtraction Magnitudes}} \\
                \cline{2-4}
                            & A $>$ B & A $<$ B & A $=$ B \\
                \hline
                (+A) + (-B) & +(A - B) & -(B - A) & +(A - B) \\
                \hline
                (-A) + (+B) & -(A - B) & +(B - A) & +(A - B) \\
                \hline
                (+A) - (+B) & +(A - B) & -(B - A) & +(A - B) \\
                \hline
                (-A) - (-B) & -(A - B) & +(B - A) & +(A - B) \\
                \hline 
            \end{tabular}
            \caption{Subtraction using Signed Magnitude Representation}
\end{table}

有人可以帮我把这个表格对齐到页面的中心吗?

答案1

tabular代码中的环境很好。缺少的是指令\centering,以便表格及其标题都居中。

以下是一些进一步的建议(无特定顺序):

  • 由于表格中的几乎所有内容都是“数学”的,所以我会使用array环境而不是tabular环境。

  • 这个palatino包和这个包一样已经过时了times。我建议你加载这个newpxtext包而不是这个palatino包。我会进一步加载这个newpxmath包,以提供一个 Palatino 克隆数学字体包。(Palatino 文本和计算机现代数学不太协调。)

  • 我看不出有什么理由大胆的表格标题行的内容。

  • 序言代码包含几个轻微的低效之处。我已在下面的代码中对其中一些进行了评论。请检查是否可以删除文档序言中存在的一些冗余内容。

在此处输入图片描述

\documentclass[12pt, a4paper]{book}
\usepackage{multicol}    % are you sure this is needed?
\usepackage{geometry}
\geometry{%a4paper,      % this is already set globally
          total={170mm, 257mm},
          left=20mm, top=20mm}
\usepackage{%amsmath,    % is loaded automatically by 'mathtools'
    bm}
%%\usepackage{amsfonts}  % is loaded automatically by 'amssymb'
\usepackage{amssymb}
\usepackage{soul}
\usepackage{indentfirst}

\usepackage[T1]{fontenc} % <-- new
%%\usepackage[utf8]{inputenc} % that's the default nowadays
%%\usepackage{palatino}  % 'palatino' package is deprecated 
\usepackage{newpxtext}   % <-- more modern than 'palatino' package
\usepackage{newpxmath}   % <-- Palatino-clone math font

\usepackage[dvipsnames]{xcolor}
%%\usepackage{xcolor}    % don't load packages twice
\usepackage{graphicx}
\usepackage{fancyhdr}    % are you sure this is needed?
\usepackage{array}       % for '\extrarowheight' macro
\usepackage{enumitem}
\usepackage{mathtools}
\usepackage{cancel}
\usepackage{arydshln}    % are you sure this is needed?
\usepackage{calc}
\usepackage{float}       % are you sure this is needed?
\usepackage[normalem]{ulem} % I've added 'normalem' option
\usepackage{multirow} 

\begin{document}

\begin{table}[htbp]
\setlength\extrarowheight{2pt} % nice touch!
\centering % <-- new
$\begin{array}{| c | c | c | c |} % suggest using 'array' instead of 'tabular'
    \hline
    \multirow{2.1}{*}{\textnormal{Operations}} & % first arg. of '\multirow' needn't be integer-valued
      \multicolumn{3}{c|}{\textnormal{Subtraction magnitudes}}\\
    \cline{2-4}
                & A > B    & A < B    & A = B    \\
    \hline
    (+A) + (-B) & +(A - B) & -(B - A) & +(A - B) \\
    \hline
    (-A) + (+B) & -(A - B) & +(B - A) & +(A - B) \\
    \hline
    (+A) - (+B) & +(A - B) & -(B - A) & +(A - B) \\
    \hline
    (-A) - (-B) & -(A - B) & +(B - A) & +(A - B) \\
    \hline 
\end{array}$

\caption{Subtraction using signed magnitude representation}
\end{table}

\end{document}

答案2

与 @Mico 的回答略有不同 (+1)。主要区别如下:

  • 用于表格的tabularray包(有了它,表格代码就短多了)
  • 浮动的定位是全局定义的
  • 包中geometry仅使用margin选项
\documentclass[12pt, a4paper]{book}
\usepackage[margin=20mm]{geometry}
\usepackage{indentfirst}

\usepackage[T1]{fontenc} 
\usepackage{newpxtext}    
\usepackage{amssymb, bm, mathtools} % should be loaded before newpxmath
\usepackage{newpxmath}    

\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{amsmath}
% global float positioning
\makeatletter
    \def\fps@figure{hbtp}
    \def\fps@table{hbtp}
\makeatother

\begin{document}
    \begin{table}
    \centering  
\begin{tblr}{hlines, vlines,
             colspec = { *{4}{Q[c, mode=math]} },
             row{1}  = {mode=text},
             rowsep=4pt
             }
\SetCell[r=2]{} Operations 
    &   \SetCell[c=3]{} Subtraction magnitudes  
                &           &               \\
    & A > B     & A < B     & A = B         \\
(+A) + (-B) 
    & +(A - B)  & -(B - A)  & +(A - B)      \\
(-A) + (+B) 
    & -(A - B)  & +(B - A)  & +(A - B)      \\
(+A) - (+B) 
    & +(A - B)  & -(B - A)  & +(A - B)      \\
(-A) - (-B) 
    & -(A - B)  & +(B - A)  & +(A - B)      \\
\end{tblr}
\caption{Subtraction using signed magnitude representation}
    \end{table}
\end{document}

在此处输入图片描述

附录:
或者你可能更喜欢以下设计:

在此处输入图片描述

\documentclass[12pt, a4paper]{book}
\usepackage{geometry}
\geometry{margin=20mm}
\usepackage{indentfirst}

\usepackage[T1]{fontenc} 
\usepackage{newpxtext}    
\usepackage{amssymb, bm, mathtools} % should be loaded before newpxmath
\usepackage{newpxmath}    

\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{amsmath,
                booktabs}   % new
% global float positioning
\makeatletter
    \def\fps@figure{hbtp}
    \def\fps@table{hbtp}
\makeatother

\begin{document}
    \begin{table}
    \centering  
\begin{tblr}{colspec = { *{4}{Q[c, mode=math]} },
             column{1} = {rightsep=2em},    % new
             column{2-Y} = {colsep+=2pt},
             row{1}  = {mode=text},
             }
    \toprule
\SetCell[r=2]{} Operations 
    &   \SetCell[c=3]{} Subtraction magnitudes  
                &           &               \\
    \midrule
    & A > B     & A < B     & A = B         \\
    \midrule
(+A) + (-B) 
    & +(A - B)  & -(B - A)  & +(A - B)      \\
(-A) + (+B) 
    & -(A - B)  & +(B - A)  & +(A - B)      \\
(+A) - (+B) 
    & +(A - B)  & -(B - A)  & +(A - B)      \\
(-A) - (-B) 
    & -(A - B)  & +(B - A)  & +(A - B)      \\
    \bottomrule
\end{tblr}
\caption{Subtraction using signed magnitude representation}
    \end{table}
\end{document}

相关内容