3

I'm following How to use \multirow to try and create a table. I have five columns and two rows, but the first column of the two rows need to be merged into one cell. The code I have is:

\documentclass{article}
\usepackage{multirow}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}
\begin{table}[htbp]
    \centering
    \begin{tabular}{|P{6 cm}|P{2.7 cm}|c|P{2 cm}|P{2 cm}|}
    \hline
    A & B & C & D & E  \\ \hline
    \multirow{ 2}{*}{1} & 0 & 6 & 230 & 35 & \\
    & 1 & 5 & 195 & 25 \\ \hline
    \end{tabular}
    \caption{A test caption}
    \label{table2}
    \end{table}

\end{document}

But the table I see looks like this enter image description here The vertical lines are broken, and I also need a horizontal line to separate the two rows in columns B,C,D, and E.

to allow column headers to be centered while having fixed widths.

What am I doing wrong?

  • 2
    Welcome to TeX.SE! Try to provide a compilable example in your question, so we can just copy, paste, and run it. It will make it a lot easier to help solve your problem. – dgoodmaniii Nov 6 '19 at 21:03
  • 2
    Remove the & after 35. – leandriis Nov 6 '19 at 21:07
  • 2
    You might want to keep in mind that the table is currently too wide for the textwidth. Depending on the actual contents of the table, you might want to use regular l type columns instead of p type ones. Probably, again also depending on the actual contents, tabularx could also come in handy to ensure the table fits into the textwidth. – leandriis Nov 6 '19 at 21:08
  • 1
    @wrahool: The table in the linked answer has a trailing & since there are 11 columns used, but the 35 entry is in the tenth. You will therefore need a & to get a cotinuous vertical line at the right side of the table. In your example, you define 5 columns and the 35 already is in the fifth. With the & you would jump to the non existant sixth column, hence the "Extra alignment tab..." error that you get from your example. – leandriis Nov 6 '19 at 21:10
  • 1
    General comment regarding the MWE producing an error massage and you showing a screenshot of the "output": Please never ever ignore error messages! Even if you get something that on first glance resembles a pdf file, there can still be issues with it. After an error, TeX only tries to recover enough to syntax check more of the file, it does not try to make sensible output after an error. – leandriis Nov 6 '19 at 21:15
4

Corrected table code as below:

\documentclass{article}
\usepackage{multirow}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}
\begin{table}[htbp]
    \centering
    \begin{tabular}{|P{6 cm}|P{2.7 cm}|c|P{2 cm}|P{2 cm}|}
    \hline
    A & B & C & D & E  \\ \hline
    \multirow{ 2}{*}{1} & 0 & 6 & 230 & 35  \\\cline{2-5}
    & 1 & 5 & 195 & 25 \\ \hline
    \end{tabular}
    \caption{A test caption}
    \label{table2}
    \end{table}

\end{document}

enter image description here

If the table is too wide consider the following revision in code:

\documentclass{article}
\usepackage{multirow}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}
\begin{table}[htbp]
    \centering
    \begin{tabular}{lllll}
    \hline
    A & B & C & D & E  \\ \hline
    \multirow{ 2}{*}{1} & 0 & 6 & 230 & 35  \\\cline{2-5}
    & 1 & 5 & 195 & 25 \\ \hline
    \end{tabular}
    \caption{A test caption}
    \label{table2}
    \end{table}

\end{document}

enter image description here

| improve this answer | |
  • as @leandriis mention in his comments, the OP table is to wide and consequently spill-out of page ... – Zarko Nov 7 '19 at 17:35
  • corrected code for table spill – js bibra Nov 8 '19 at 0:42

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.