TransWikia.com

multirow table, error

TeX - LaTeX Asked on July 29, 2021

I’m trying to create a table which some of the cells are multirow and this is my code:

begin{table}[!htb]
centering
smalladdtolength{tabcolsep}{4pt}
caption{""}
label{table3}
begin{tabular}{*{7}{|c}|}
hline
 multirow{2}{*}{H}& multicolumn{3}{c|}{K@10} & multicolumn{3}{c|}{K@20}cline{2-7}
 & P & R & TFFR &  P & R & TFFR  hline 
Htextsubscript{01}  & multirow{2}{*} & multirow{2}{*} & multirow{2}{*} & multirow{2}{*} & multirow{2}{*}  & multirow{2}{*}  hline
Htextsubscript{02} & & & & & &  
Htextsubscript{03}  & multirow{2}{*}  & multirow{2}{*} & multirow{2}{*} & multirow{2}{*} & multirow{2}{*}  & multirow{2}{*}   hline
Htextsubscript{04} & & & & & &  
Htextsubscript{05}  & multirow{2}{*}  & multirow{2}{*}  & multirow{2}{*} & multirow{2}{*} & multirow{2}{*}  & multirow{2}{*}  hline
Htextsubscript{06} & & & & & &  hline
 end{tabular}
end{table}

I don’t know what’s wrong with this code. This is the error which I got:

! LaTeX Error: Something's wrong--perhaps a missing item.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.487 ... & multirow{2}{*}  & multirow{2}{*}  

2 Answers

Let us list the errors first.

  1. For quotation marks you shouldn't use "..." but ``...''
  2. multirow{2}{*}{H} takes three arguments - multirow{<rows>}{<width>}{<content>}. You have missed the third argument. Even if you don't want to have text, you should have it but without content like multirow{2}{*}{}.
  3. textsubscript isn't defined by LaTeX. You have to use usepackage{fixltx2e} in the preamble to make it work. BTW are you trying to write a chemical formula? If so, there are packages meant for that job. Or are those Htextsubscript{02}s mathematical variables? Please elaborate.
  4. To make colored cells, load colortbl package or usepackage[table]{xcolor} (Both are equivalent). and the use cellcolor{<color> inside the cell:

    & cellcolor{green!50!blue} <content> &
    

    or

     & cellcolor{green!50!blue}multirow{2}{*}{}  &
    
  5. Do you really want vertical lines? What about reducing the number of some horizontal lines? You may read the documentation of booktabs package to learn how to produce a good table.

Now your code should be:

documentclass{article}
usepackage{multirow,fixltx2e}
usepackage[table]{xcolor}    %% this in turn loads colortbl
%usepackage{colortbl}
begin{document}
  begin{table}[!htb]
centering
smalladdtolength{tabcolsep}{4pt}
caption{``''}
label{table3}
begin{tabular}{*{7}{|c}|}
hline
 multirow{2}{*}{H}& multicolumn{3}{c|}{K@10} & multicolumn{3}{c|}{K@20}cline{2-7}
 & P & R & TFFR &  P & R & TFFR  hline
Htextsubscript{01}  & multirow{2}{*}{} & multirow{2}{*}{}& multirow{2}{*}{} & multirow{2}{*}{} & multirow{2}{*} {} & multirow{2}{*}{}  hline
Htextsubscript{02} & cellcolor{green!50!blue} & & & & &  
Htextsubscript{03}  & cellcolor{green!50!blue}multirow{2}{*}{}  & multirow{2}{*}{} & multirow{2}{*}{} & multirow{2}{*}{} & multirow{2}{*}{}  & multirow{2}{*}{}   hline
Htextsubscript{04} & & cellcolor{green!50!blue} & & & &  
Htextsubscript{05}  & multirow{2}{*}{}  & cellcolor{green!50!blue}multirow{2}{*}{}  & multirow{2}{*}{} & multirow{2}{*}{} & multirow{2}{*}{}  & multirow{2}{*}{}  hline
Htextsubscript{06} & & & & & &  hline
 end{tabular}
end{table}
end{document}

enter image description here

Correct answer by user11232 on July 29, 2021

With {NiceTabular} of nicematrix, you will have a perfect output in the PDF viewers at all zoom levels.

documentclass{article}
usepackage{xcolor}
usepackage{nicematrix}

begin{document}
begin{table}[!htb]
centering
small
addtolength{tabcolsep}{4pt}
caption{``My caption''}
label{table3}
begin{NiceTabular}{ccccccc}[vlines,colortbl-like]
Hline
Block{2-1}{H}      & Block{1-3}{K@10} &&& Block{1-3}{K@20}  Hline
                    & P & R & TFFR &  P & R & TFFR  Hline
Htextsubscript{01}  Hline
Htextsubscript{02} & cellcolor{green!50!blue}   
Htextsubscript{03} & cellcolor{green!50!blue}    Hline
Htextsubscript{04} & & cellcolor{green!50!blue} 
Htextsubscript{05} & & cellcolor{green!50!blue} Hline
Htextsubscript{06}  Hline
end{NiceTabular}
end{table}
end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of both codes

If you prefer, it's possible to specify the colors of the cells in the CodeBefore.

documentclass{article}
usepackage{xcolor}
usepackage{nicematrix}

begin{document}
begin{table}[!htb]
centering
small
addtolength{tabcolsep}{4pt}
caption{``My caption''}
label{table3}
begin{NiceTabular}{ccccccc}[vlines]
CodeBefore
  cellcolor{green!50!blue}{4-2,5-2,6-3,7-3}
Body
  Hline
  Block{2-1}{H}      & Block{1-3}{K@10} &&& Block{1-3}{K@20}  Hline
                      & P & R & TFFR &  P & R & TFFR  Hline
  Htextsubscript{01}  Hline
  Htextsubscript{02}  
  Htextsubscript{03}  Hline
  Htextsubscript{04}  
  Htextsubscript{05}  Hline
  Htextsubscript{06}  Hline
end{NiceTabular}
end{table}
end{document}

The output is the same.

Answered by F. Pantigny on July 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP