2

I an error that I don't understand. Yet overleaf gives me the desired result.

\begin{center}
\begin{tabular}{|p{0.5cm}|p{2.5cm}|p{2.5cm}|}
\hline
\centering $k$ & \centering $\mathbb{P}(X = k)$         & $~~~~~~ \mathbb{P}(Z = k)$ 
 \hline
\centering 0                 & \centering 0,4213 &  $~~~~~~0,4231$   \hline
\centering 1                      & \centering 0,3660 & $~~~~~~0,3639$     \hline
\centering 2                   & \centering 0,1571 & $~~~~~~0,1564$     \hline
\centering 3                   & \centering 0,0444 & $~~~~~~0,0448$     \hline
\end{tabular}
\end{center}
3
  • 1
    Please, post the MWE in compilable format, i.e., from \documentclass to \end{document}
    – MadyYuvi
    Dec 16, 2022 at 14:21
  • 2
    Quick suggestion, replace \hline to \\ \hline and check
    – MadyYuvi
    Dec 16, 2022 at 14:22
  • For tags: this isn't related to overleaf nor equations. it is related to table
    – anis
    Dec 16, 2022 at 14:27

4 Answers 4

4

The first error isn't the one mentioned in the title of this thread, but:

! Misplaced \noalign.
\hline ->\noalign 
                  {\ifnum 0=`}\fi \hrule \@height \arrayrulewidth \futurelet...
l.8  \hline

Always you have to focus to the first error because following errors may be only due to the error recovery from the first error isn't done ideally.

How to understand Misplaced \noalign: TeX allows to insert between table rows a vertical material (typically lines between them). It is done by \noalign TeX primitive. This primitive can be used after the current row is terminated and new row isn't opened. The LaTeX macro \hline inserts the line between table rows using \noalign primitive. And you have used \hline although the row wasn't terminated.

The second error says:

! Extra alignment tab has been changed to \cr.
<recently read> \endtemplate 
                             
l.9 \centering 0                 &
                                   \centering 0,4213 &  $~~~~~~0,4231$   \hline

It means that the material from \centering{0} is added to the material of the last column (because row wasn't terminated) and the following character & gives problem for TeX. It denotes, that next table item have to be created in a next table column. But that would be fourth column and you declared only three columns in the table preamble. So, TeX reports error and adds the row terminator \cr.

I understand why you don't understand these error messages: they are TeX error messages and if you are thinking only in LaTeX terminology, you are unable to understand the TeX messages. TeX uses \cr but LaTeX provides \\ in tables which is internally changed to \cr. TeX uses \noalign primitive but LaTeX doesn't mention nothing about this TeX primitive. It is often useful to understand TeX directly and its terminology because LaTeX is only macro package and what you are running in fact is TeX.

3

As is already noted in the @anis answer (+1), each table row should be terminated by \\ :

\documentclass{article}
\usepackage{amssymb}

\begin{document}
    \begin{center}
\begin{tabular}{|p{0.5cm}|p{2.5cm}|p{2.5cm}|}
    \hline
\centering $k$ & \centering $\mathbb{P}(X = k)$ 
                                    & $~~~~~~ \mathbb{P}(Z = k)$    \\
    \hline
\centering 0    & \centering 0,4213 & $~~~~~~0,4231$    \\
    \hline
\centering 1    & \centering 0,3660 & $~~~~~~0,3639$    \\  
    \hline
\centering 2    & \centering 0,1571 & $~~~~~~0,1564$    \\   
    \hline
\centering 3    & \centering 0,0444 & $~~~~~~0,0448$    \\
    \hline
\end{tabular}
    \end{center}
\end{document}

enter image description here

However, by defining new column tape and put columns in math node your table code is much shorter:

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

\begin{document}
    \begin{center}
\begin{tabular}{|>{$}C{0.5cm}<{$}|>{$}C{2.5cm}<{$}|>{$}C{2.5cm}<{$}|}
    \hline
k   & \mathbb{P}(X = k)  
                & \mathbb{P}(Z = k) \\
    \hline
0   & 0,4213    & 0,4231            \\
    \hline
1   & 0,3660    & 0,3639            \\  
    \hline
2   & 0,1571    & 0,1564            \\   
    \hline
3   & 0,0444    & 0,0448            \\
    \hline
\end{tabular}
    \end{center}
\end{document}

Result is the same as before.

One more alternative is use of tabularray and siunitx package:

\documentclass{article}
\usepackage{amssymb}
\usepackage{tabularray}
\UseTblrLibrary{siunitx}


\begin{document}
    \begin{center}
    \sisetup{table-format=1.4,
             output-decimal-marker={,}}
\begin{tblr}{hlines, vlines,
             colspec={Q[c, wd=0.5cm,$] Q[c, wd=2.5cm,si, $] Q[c, wd=2.5cm,si, $]},
             row{1} ={guard}
0   & 0,4213    & 0,4231            \\
1   & 0,3660    & 0,3639            \\  
2   & 0,1571    & 0,1564            \\   
3   & 0,0444    & 0,0448            \\
\end{tblr}
    \end{center}
\end{document}

Now table has a bit more nice: added are small vertical space around cells contents, decimal commas are better spacing:

enter image description here

3
  • Your last example could benefit from the icomma package Dec 16, 2022 at 15:08
  • @samcarter_is_at_topanswers.xyz, you may be right. I never use this package. but in similar cases I rather use S columns defined in the siunitx package. See edited answer.
    – Zarko
    Dec 16, 2022 at 15:23
  • Yes, siunitx also works to get the correct spacing Dec 16, 2022 at 15:26
3

You have to terminate rows by \\. The output seems correct because TeX sees \hline not preceded by \\ and, after telling you that \cr (the internal command) is missing, adds it.

Never disregard errors.

On the other hand, there are better ways to build your table: instead of p{<dimen>} and \centering (which, as you probably noticed, doesn't work in the last column) you can use the w{c}{<dimen>} specifier provided by the array package.

But with siunitx and booktabs the output is much better.

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{array} % for the first realization

\usepackage{siunitx,booktabs} % for the second realization
\sisetup{output-decimal-marker={,}}

\begin{document}

\begin{center}
\begin{tabular}{|w{c}{0.5cm}|w{c}{2.5cm}|w{c}{2.5cm}|}
\hline
$k$ & $\mathbb{P}(X = k)$ & $\mathbb{P}(Z = k)$ \\
\hline
0 & 0{,}4213 & $0{,}4231$ \\ \hline
1 & 0{,}3660 & $0{,}3639$ \\ \hline
2 & 0{,}1571 & $0{,}1564$ \\ \hline
3 & 0{,}0444 & $0{,}0448$ \\ \hline
\end{tabular}
\end{center}

\begin{center}
\begin{tabular*}{0.5\columnwidth}{
  @{\extracolsep{\fill}}
  c
  S[table-format=1.4]
  S[table-format=1.4]
  @{}
}
\toprule
$k$ & {$\mathbb{P}(X = k)$} & {$\mathbb{P}(Z = k)$} \\
\midrule
0 & 0,4213 & 0,4231 \\
1 & 0,3660 & 0,3639 \\
2 & 0,1571 & 0,1564 \\
3 & 0,0444 & 0,0448 \\
\bottomrule
\end{tabular*}
\end{center}

\end{document}

enter image description here

2

You are missing \\ at the end of each row. This is the row delimiter. it indicates that the new row starts.

You can try to use a LaTex Table editor to better understand how they work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .