表格无缘无故向右对齐

表格无缘无故向右对齐

我有一个表格,当我使用时它会向右对齐\centering。我使用仅包含 babel 和 array 包的 article 类。我有以下代码:

\documentclass[paper=a4]{article} % A4 paper and 11pt font size
\usepackage[dutch]{babel} % Dutch language/hyphenation

\usepackage{array}
\newcolumntype{+}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
#1\ignorespaces
}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\begin{table}[h]
\newcommand\colwidth{10pt}
\let\oldmulticolumn\multicolumn
\renewcommand\multicolumn[3]{\oldmulticolumn{#1}{#2}{\textrm{#3}}}
\centering
\texttt{
\begin{tabular}{+r || ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} || ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} || ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} || ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} ||}
    \rowstyle{\normalfont}
                & 15 & 14 & 13 & 12 & 11 & 10 & 9 & 8 & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 \\\hline
    LOADHI      & \multicolumn{3}{c|}{dest} & 0 & \multicolumn{4}{c||}{const} & \multicolumn{4}{c||}{const} & \multicolumn{4}{c||}{const} \\\cline{6-9}
    LOADLO      & \multicolumn{3}{c|}{dest} & 1 & \multicolumn{3}{c|}{addr. B} & 1 & \multicolumn{4}{c||}{const} & \multicolumn{4}{c||}{const} \\\hline
    ALUf        & \multicolumn{3}{c|}{dest} & 1 & \multicolumn{3}{c|}{addr. B} & 0 & 1 & \multicolumn{3}{c||}{addr. A} & 1 & \multicolumn{3}{c||}{opcode} \\
    ALU         & \multicolumn{3}{c|}{dest} & 1 & \multicolumn{3}{c|}{addr. B} & 0 & 0 & \multicolumn{3}{c||}{addr. A} & 1 & \multicolumn{3}{c||}{opcode} \\\cline{10-13}
    ALU         & \multicolumn{3}{c|}{dest} & 1 & \multicolumn{3}{c|}{addr. B} & 0 & \multicolumn{4}{c||}{const. A} & 0 & \multicolumn{3}{c||}{opcode} \\\hline
    READ        & \multicolumn{3}{c|}{dest} & 1 & \multicolumn{3}{c|}{addr. B} & 0 & 1 & \multicolumn{3}{c||}{addr. A} & 1 & 1 & 1 & 1 \\\cline{10-13}
    READ        & \multicolumn{3}{c|}{dest} & 1 & \multicolumn{3}{c|}{addr. B} & 0 & \multicolumn{4}{c||}{const. A} & 0 & 1 & 1 & 1 \\\cline{2-5}\cline{10-13}
    WRITE       & 0 & 0 & 0 & 0 & \multicolumn{3}{c|}{addr. B} & 0 &  & \multicolumn{3}{c||}{addr. A} & 1 &  &  & \\\hline
    COND\_CHK   & 0 & 0 & 0 & 0 &  &  &  & 1 &  &  &  &  & \multicolumn{4}{c||}{condition}\\\hline
\end{tabular}
}
\caption{Instruction set}
\end{table}

\end{document}

表格内部可能有点复杂,但我不明白为什么它应该像这里一样向右对齐。灰色边是文档的两端。您可以清楚地看到标题居中,但表格不是。

在此处输入图片描述

这是为什么?我该如何解决?

答案1

您需要使表格比文本宽度窄,正如乳胶警告的那样:

LaTeX Warning: Unused global option(s):
    [paper=a4].

文档类选项语法错误。

在此处输入图片描述

\documentclass[a4paper]{article} % A4 paper and 11pt font size
\usepackage[dutch]{babel} % Dutch language/hyphenation

\usepackage{array}
\newcolumntype{+}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
#1\ignorespaces
}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\begin{table}[htp]

\noindent X\dotfill X
\newcommand\colwidth{9pt}

\setlength\tabcolsep{3pt}
\let\oldmulticolumn\multicolumn
\renewcommand\multicolumn[3]{\oldmulticolumn{#1}{#2}{\textrm{#3}}}
\centering\small
\texttt{%
\begin{tabular}{+r || ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} || ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} || ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} || ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} | ^C{\colwidth} ||}
    \rowstyle{\normalfont}
                & 15 & 14 & 13 & 12 & 11 & 10 & 9 & 8 & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 \\\hline
    LOADHI      & \multicolumn{3}{c|}{dest} & 0 & \multicolumn{4}{c||}{const} & \multicolumn{4}{c||}{const} & \multicolumn{4}{c||}{const} \\\cline{6-9}
    LOADLO      & \multicolumn{3}{c|}{dest} & 1 & \multicolumn{3}{c|}{addr. B} & 1 & \multicolumn{4}{c||}{const} & \multicolumn{4}{c||}{const} \\\hline
    ALUf        & \multicolumn{3}{c|}{dest} & 1 & \multicolumn{3}{c|}{addr. B} & 0 & 1 & \multicolumn{3}{c||}{addr. A} & 1 & \multicolumn{3}{c||}{opcode} \\
    ALU         & \multicolumn{3}{c|}{dest} & 1 & \multicolumn{3}{c|}{addr. B} & 0 & 0 & \multicolumn{3}{c||}{addr. A} & 1 & \multicolumn{3}{c||}{opcode} \\\cline{10-13}
    ALU         & \multicolumn{3}{c|}{dest} & 1 & \multicolumn{3}{c|}{addr. B} & 0 & \multicolumn{4}{c||}{const. A} & 0 & \multicolumn{3}{c||}{opcode} \\\hline
    READ        & \multicolumn{3}{c|}{dest} & 1 & \multicolumn{3}{c|}{addr. B} & 0 & 1 & \multicolumn{3}{c||}{addr. A} & 1 & 1 & 1 & 1 \\\cline{10-13}
    READ        & \multicolumn{3}{c|}{dest} & 1 & \multicolumn{3}{c|}{addr. B} & 0 & \multicolumn{4}{c||}{const. A} & 0 & 1 & 1 & 1 \\\cline{2-5}\cline{10-13}
    WRITE       & 0 & 0 & 0 & 0 & \multicolumn{3}{c|}{addr. B} & 0 &  & \multicolumn{3}{c||}{addr. A} & 1 &  &  & \\\hline
    COND\_CHK   & 0 & 0 & 0 & 0 &  &  &  & 1 &  &  &  &  & \multicolumn{4}{c||}{condition}\\\hline
\end{tabular}%
}
\caption{Instruction set}
\end{table}

\end{document}

相关内容