LaTeX 表格排版中遇到 Misplaced \noalign

遇到该问题的根本原因是编译器认为表格没有正确换行(末尾没有添加”\“)。但是,LaTeX 认为没有正确换行不代表真的没有正确换行。当表格使用了复杂的插入方式时,例如循环,LaTeX 无法正确判断。

报错示范

\begin{table}
  \begin{tabular}{lrrrrr}
    p & cluster distance & SE & gene & gene/SE
    \DTLforeach*{at_counts}
    {
    \pvalue=p value,
    \cd=cluster distance,
    \seCount=SE count,
    \geneCount=gene count,
    \ratio=ratio,
    \tored=tored
    }
    {
    \tabularnewline
    \ifthenelse{\tored=1}
    {
    \rowcolor{green} \pvalue & \cd & \seCount & \geneCount & \ratio
    }
    {
    \pvalue & \cd & \seCount & \geneCount & \ratio
    }
    }
  \end{tabular}
\end{table}

在上面的例子当中,使用了 datatool 宏包来导入 csv 表格数据,使用 colortbl 宏包来修改表格底色。如果忽略错误,结果符合预期。但是由于\tabularnewline没有直接和\rowcolor相邻,所以会报错。进行以下修改后不会报错,同时结果符合预期。

\begin{table}
  \begin{tabular}{lrrrrr}
    p & cluster distance & SE & gene & gene/SE
    \DTLforeach*{at_counts}
    {
    \pvalue=p value,
    \cd=cluster distance,
    \seCount=SE count,
    \geneCount=gene count,
    \ratio=ratio,
    \tored=tored
    }
    {
    \ifthenelse{\tored=1}
    {
    \tabularnewline
    \rowcolor{green} \pvalue & \cd & \seCount & \geneCount & \ratio
    }
    {
    \tabularnewline
    \pvalue & \cd & \seCount & \geneCount & \ratio
    }
    }
  \end{tabular}
\end{table}
posted @ 2021-01-17 17:08  飞舞的冰龙  阅读(4805)  评论(0)    收藏  举报