感谢您的帮助

感谢您的帮助

我搜索了代码的解决方案。但尝试失败。我使用“tablegenerator”为我的项目制作了一个表格。

错误:

    ! Extra alignment tab has been changed to \cr. <template> \endtemplate 

    l.182 ...centage was written in the parenthesis.}}

    \\
    You have given more \span or & marks than there were in the preamble to the \halign or \valign now in progress. So I'll assume that you meant to type \cr instead.

套餐:

\usepackage{tabularx}
\usepackage{multirow}
\usepackage[table,xcdraw]{xcolor}
\usepackage{longtable, lscape}

代码:

\begin{table}[H]
\centering
\begin{threeparttable}
\caption{Frequency}
\label{tab:frevio}
\begin{tabular}{lllll}
\cline{1-3}
\multicolumn{1}{|c|}{\cellcolor[HTML]{C0C0C0}}                                              & \multicolumn{2}{l|}{\cellcolor[HTML]{EFEFEF}\textbf{var3}}                                                        &  &  \\ \cline{2-3}
\multicolumn{1}{|c|}{\multirow{-2}{*}{\cellcolor[HTML]{C0C0C0}\textbf{var1}}} & \multicolumn{1}{l|}{\cellcolor[HTML]{EFEFEF}Yes-var3} & \multicolumn{1}{l|}{\cellcolor[HTML]{EFEFEF}No-var3} &  &  \\ \cline{1-3}
\multicolumn{1}{|l|}{\cellcolor[HTML]{C0C0C0}Yes-var1}                                   & \multicolumn{1}{l|}{990(11\%)}                                    & \multicolumn{1}{l|}{339(4\%)}                                   &  &  \\ \cline{1-3}
\multicolumn{1}{|l|}{\cellcolor[HTML]{C0C0C0}No-var1}                                    & \multicolumn{1}{l|}{7113(79\%)}                                    & \multicolumn{1}{l|}{564(6\%)}                                   &  &  \\ \cline{1-3}
                                                                                            &                                                            &                                                           &  & 
\multicolumn{2}{l}{\textsuperscript{*}\footnotesize{The percentage was written in the parenthesis.}}\\
\end{tabular}
\end{threeparttable}
\end{table}

感谢您的帮助

答案1

您的倒数第二行是空白的,但没有结束换行符/新行\\。因此,最后一行中的注释被视为前一行的一部分,因为前一行的列数不足以满足tabular列规范中的定义(您只有 5 个)。

不需要添加没有任何条目的空白行,只需\\按原样使用:

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{multirow}

\begin{document}

\begin{table}
  \centering
  \caption{Frequency}
  \begin{tabular}{ *{3}{l} }
    \cline{1-3}
    \multicolumn{1}{|c|}{\cellcolor[HTML]{C0C0C0}} & \multicolumn{2}{l|}{\cellcolor[HTML]{EFEFEF}\textbf{var3}} \\
    \cline{2-3}
    \multicolumn{1}{|c|}{\multirow{-2}{*}{\cellcolor[HTML]{C0C0C0}\textbf{var1}}} & \multicolumn{1}{l|}{\cellcolor[HTML]{EFEFEF}Yes-var3} & \multicolumn{1}{l|}{\cellcolor[HTML]{EFEFEF}No-var3} \\
    \cline{1-3}
    \multicolumn{1}{|l|}{\cellcolor[HTML]{C0C0C0}Yes-var1} & \multicolumn{1}{l|}{990(11\%)} & \multicolumn{1}{l|}{339(4\%)} \\
    \cline{1-3}
    \multicolumn{1}{|l|}{\cellcolor[HTML]{C0C0C0}No-var1} & \multicolumn{1}{l|}{7113(79\%)} & \multicolumn{1}{l|}{564(6\%)} \\
    \cline{1-3}
    \\
    \multicolumn{2}{l}{\textsuperscript{*}\footnotesize{The percentage was written in the parenthesis.}}
  \end{tabular}
\end{table}

\end{document}

从上面的例子可以看出,只需要 3 列。


就我个人而言,我会这样布置桌子(使用booktabs):

在此处输入图片描述

\documentclass{article}

\usepackage{booktabs}

\begin{document}

\begin{table}
  \centering
  \caption{Frequency}
  \begin{tabular}{ l r r }
    \toprule
                  & \multicolumn{2}{c}{\textbf{var3}} \\
    \cmidrule{2-3}
    \multicolumn{1}{c}{\textbf{var1}} & \multicolumn{1}{c}{Yes} & \multicolumn{1}{c}{No} \\
    \midrule
    Yes &   990 (11\%) & 339 (4\%) \\
    No  & 7,113 (79\%) & 564 (6\%) \\
    \bottomrule
  \end{tabular}
\end{table}

\end{document}

答案2

另一种可能性是,使用threepartablehhline(需要在彩色表中有可见的水平线):

\documentclass{book}
\usepackage[utf8]{inputenc}

\usepackage{multirow, float, threeparttable, hhline}
\usepackage[table]{xcolor}

\begin{document}

\begin{table}%[H]
\centering\setlength{\extrarowheight}{2pt}
\begin{threeparttable}
\caption{Frequency}
\label{tab:frevio}
\begin{tabular}{|>{\columncolor[HTML]{C0C0C0}}lrr|}
\hhline{---}
 & \multicolumn{2}{c|}{\cellcolor[HTML]{EFEFEF}\textbf{var3}} \\[-0.8ex]
\hhline{>{\arrayrulecolor[HTML]{C0C0C0}}->{\arrayrulecolor[HTML]{EFEFEF}}-->{\arrayrulecolor{black}}}
\multirow{-1.67}{*}{\textbf{var1}}&\multicolumn{1}{c}{\cellcolor[HTML]{EFEFEF}Yes-var3} & \multicolumn{1}{c|}{\cellcolor[HTML]{EFEFEF}No-var3}
\\ \hhline{---}
Yes-var1 &990 (11\%) &339 (4\%) \\ \hhline{---}
No-var1 & 7113 (79\%) & 564 (6\%) \\ \hhline{---}
\end{tabular}
\begin{tablenotes}[flushleft]\footnotesize\smallskip
 \item[*]The percentage was written in the parenthesis.
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document} 

在此处输入图片描述

相关内容