0

The following code

\documentclass{article} 
\usepackage[dvipsnames]{xcolor}
\usepackage{colortbl}

\definecolor{myGray}{HTML}{F2F2F2} \newcolumntype{G}{>{\columncolor{myGray}\centering$}m{6mm}<{$}} \newcolumntype{M}{>{\centering$}m{6mm}<{$}} \newcolumntype{C}{>{$}c<{$}}

\begin{document} \begin{tabular}{|G|M|M|M|M|} \hline \rowcolor{myGray} \times & 1 & i & j & k \\hline 1 & 1 & i & j & k \\hline i & i & -1 & k & -j \\hline j & j & -k & -1 & i \\hline k & k & j & -i & -1 \\hline \end{tabular} \end{document}

creates this error message:

! LaTeX Error: There's no line here to end.

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

l.13 ...rowcolor{myGray} \times & 1 & i & j & k \ \hline

When I modified the alignment of the table from {|G|M|M|M|M|} to {|G|M|M|M|m|}, it does work. But I think that the definition of M isn't wrong.

Mico
  • 506,678
  • Welcome to Tex.SE. – Mico Jul 17 '22 at 05:40
  • 1
    You need to (a) load the array package (to enable the macro \newcolumntype) and (b) replace both instances of \centering with \centering\arraybackslash. Separately, do also replace \usepackage[dvipsnames]{xcolor} \usepackage{colortbl} with \usepackage[dvipsnames,table]{xcolor} in order to assure optimal interoperability of the xcolor and colortbl packages. – Mico Jul 17 '22 at 05:41
  • 1
    @Mico Thanks very much! It works very well now! – Ventrikle Jul 17 '22 at 05:47

2 Answers2

2

The problem is that the LaTeX macro \centerig redefines \\:

\DeclareRobustCommand\centering{%
  \let\\\@centercr  %% <<< this is re-definition of \\
  \rightskip\@flushglue\leftskip\@flushglue
  \finalhyphendemerits=\z@
  \parindent\z@\parfillskip\z@skip}
\protected\def\@centercr{\ifhmode \unskip\else \@nolnerr\fi
       \par\@ifstar{\nobreak\@xcentercr}\@xcentercr}
\gdef\@nolnerr{%
  \@latex@error{There's no line here to end}\@eha}

The test \ifhmode fails because you are in math mode when \\ occurs.

One solution is not to use \centering macro but TeX primitive \hfil because you want to center only single-line paragraphs:

\newcolumntype{M}{>{\hfil $}m{6mm}<{$}}
wipet
  • 74,238
0

To your problem (defining column types) you can avoided on more ways:

  • consider @Mico comment,
  • consider @wipet answer,
  • instead of \\ use \tabularnewline
  • and also by use of tabularray package:
\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\definecolor{myGray}{HTML}{F2F2F2}
\usepackage{tabularray}

\begin{document} \begin{tblr}{hlines, vlines, colspec = {Q[c,bg=myGray, mode=math] *{4}{Q[c, mode=math]}}, row{1} = {bg=myGray} } \times & 1 & i & j & k \ 1 & 1 & i & j & k \ i & i & -1 & k & -j \ j & j & -k & -1 & i \ k & k & j & -i & -1 \ \end{tblr} \end{document}

enter image description here

Zarko
  • 296,517