1

I have a tabular which gets aligned to the right while I use \centering. I'm using the article class with only the packages babel and array. The code I have:

\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}

The table may be a bit complicated on the inside, but I don't see why it should get aligned to the right, like here. The grey sides are the ends of the document. You can clearly see the caption is centered, but the tabular is not.

enter image description here

Why is that and how can I fix it?

  • 1
    It seems that your table is too long so it is not fitting in text area. – Sigur Jan 25 '15 at 22:13
  • 1
    @Sigur you're right, thanks. Setting smaller margins helps. Could you turn that into an answer, or is this a duplicate? I couldn't find anything though. – Keelan Jan 25 '15 at 22:23
4

You need to make the table narrower than the text width, also as latex warns:

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

The document class option syntax was wrong.

enter image description here

\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}
  • Oh right, thanks. I had paper=a4 because I used the scrartcl class first. Thanks very much. – Keelan Jan 25 '15 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.