3

I have the following table:

enter image description here

From the code:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}

\usepackage[T1]{fontenc}
\usepackage[brazil]{babel}
\usepackage[a4paper, margin=2cm]{geometry}

\usepackage{newtxtext,newtxmath}
\usepackage{array,ragged2e,tabularx,multirow}

\begin{document}
\section{Custos}

    \begin{tabular}
    {|
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{3.9cm}|} % Item
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{1.9cm}|} % Quantidade
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{3.0cm}|} % Valor, Valor
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{2.6cm}|} % Valor, Valor
    }
        \hline Item             &   Quantidade  &   Valor Unitário (R\$)    &   Valor Total (R\$) \\ \hline
        CD                      &   1           &   5,00                    &   5,00,00           \\ \hline
        Impressão               &   800         &   0,15                    &   120,00            \\ \hline
        Reserva Gerencial       &   1           &   20,00                   &   20,00             \\ \hline
        Reserva de Contingência &   1           &   20,00                   &   20,00             \\ \hline
        Total                   & \multicolumn{2}{c c|}{}                   &   165,00            \\ \hline

    \end{tabular}

\end{document}

But is throwing the error:

 test.tex:29: Package array Error: Only one column-spec. allowed.. [...                  & \multicolumn{2}{c c}{}]

The table is rendering almost as I like to, except for the shifted | before 165,00.

How to remove the error from appearing and align correctly the shifted | pipe?


Update

Nice list for references:

  1. Multi-column and multi-row cells in LaTeX tables
  2. https://en.wikibooks.org/wiki/LaTeX/Tables
  3. What is the difference between tabular, tabular* and tabularx environments?
  4. Using multicolumn in latex
  5. Latex Table multiple row and multiple column
2

\multicolumn can have only one column specifier. You have two! However, why do you have such complicated head definition. This does the same:

\begin{tabular}
    {|
        >{\RaggedRight}p{3.9cm}| 
        % Item
        >{\RaggedRight}p{1.9cm}|
        % Quantidade
        >{\RaggedRight}p{3.0cm}| 
        % Valor, Valor
        >{\RaggedRight}p{2.6cm}| }\hline 
    Item             &   Quantidade  &   Valor Unitário (R\$)    &   Valor 
    Total (R\$) \\ \hline
    CD                      &   1           &   5,00                    &   
    5,00,00           \\ \hline
    Impressão               &   800         &   0,15                    &   
    120,00            \\ \hline
    Reserva Gerencial       &   1           &   20,00                   &   
    20,00             \\ \hline
    Reserva de Contingência &   1           &   20,00                   &   
    20,00             \\ \hline
    Total                   & \multicolumn{2}{c|}{}                   &   
    165,00            \\ \hline 
\end{tabular}
2

The error is in the argument to \multicolumn, but I think you're taking the wrong approach to tables: there is no need to specify the width, as TeX will compute it by itself.

You can see that the first and second example are essentially the same.

I also added a third example for showing how easy it is to get alignment at the decimal comma with siunitx. This requires some attention in the input of the table headers, but the reward is big.

In the final example I used booktabs features, but they're not mandatory.

\documentclass[12pt]{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[brazil]{babel}

\usepackage[a4paper, margin=2cm]{geometry}

\usepackage{newtxtext,newtxmath}
\usepackage{array,ragged2e,tabularx,multirow}

\usepackage{booktabs,siunitx} % for the last example

\sisetup{output-decimal-marker={,}} % for siunitx

\begin{document}

\begin{table}[htp]
\centering
\caption{Original, with the fix}

    \begin{tabular}
    {|
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{3.9cm}|} % Item
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{1.9cm}|} % Quantidade
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{3.0cm}|} % Valor, Valor
        *1{@{\hspace{3.0pt}}>{ \RaggedRight\arraybackslash\hsize=1.1\hsize }p{2.6cm}|} % Valor, Valor
    }
        \hline Item             &   Quantidade  &   Valor Unitário (R\$)    &   Valor Total (R\$) \\ \hline
        CD                      &   1           &   5,00                    &   5,00              \\ \hline
        Impressão               &   800         &   0,15                    &   120,00            \\ \hline
        Reserva Gerencial       &   1           &   20,00                   &   20,00             \\ \hline
        Reserva de Contingência &   1           &   20,00                   &   20,00             \\ \hline
        Total                   & \multicolumn{2}{c|}{}                   &   165,00            \\ \hline

    \end{tabular}

\end{table}

\begin{table}[htp]
\centering
\caption{Simpler}

    \begin{tabular}{|l|l|l|l|}
        \hline Item             &   Quantidade  &   Valor Unitário (R\$)    &   Valor Total (R\$) \\ \hline
        CD                      &   1           &   5,00                    &   5,00              \\ \hline
        Impressão               &   800         &   0,15                    &   120,00            \\ \hline
        Reserva Gerencial       &   1           &   20,00                   &   20,00             \\ \hline
        Reserva de Contingência &   1           &   20,00                   &   20,00             \\ \hline
        Total                   & \multicolumn{2}{c|}{}                   &   165,00            \\ \hline

    \end{tabular}

\end{table}


\begin{table}[htp]
\centering

\caption{Better?}

\begin{tabular}{ l S[table-format=3.0] S[table-format=2.2] S[table-format=3.2] }
\toprule
{Item}                  & {Quantidade} & {Valor Unitário (R\$)} & {Valor Total (R\$)} \\
\midrule
CD                      &   1          &    5,00                &     5,00            \\
Impressão               &   800        &    0,15                &   120,00            \\
Reserva Gerencial       &   1          &   20,00                &    20,00            \\
Reserva de Contingência &   1          &   20,00                &    20,00            \\
\midrule
Total                   & \multicolumn{2}{c}{}                  &   165,00            \\
\bottomrule
\end{tabular}

\end{table}


\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.