更新

更新

我有下表:

在此輸入影像描述

從程式碼來看:

\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}

但拋出錯誤:

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

表格幾乎按照我喜歡的方式呈現,除了|之前的移動165,00

如何消除出現的錯誤並正確對齊移位的|管道?


更新

不錯的參考清單:

  1. LaTeX 表格中的多列和多行儲存格
  2. https://en.wikibooks.org/wiki/LaTeX/Tables
  3. tabular、tabular* 和 tabularx 環境有什麼區別?
  4. 在乳膠中使用多列
  5. Latex 表多行多列

答案1

\multicolumn只能有列說明符。你有兩個!但是,為什麼頭部定義這麼複雜呢?這也有同樣的作用:

\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

錯誤在於 的參數中\multicolumn,但我認為您對錶格採取了錯誤的方法:不需要指定寬度,因為 TeX 會自行計算它。

您可以看到第一個範例和第二個範例本質上是相同的。

我還添加了第三個範例,用於展示使用 . 在小數點逗號處對齊是多麼容易siunitx。這需要在表頭的輸入中註意一些,但回報是巨大的。

在最後一個範例中,我使用了booktabs功能,但它們不是強制性的。

\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}

在此輸入影像描述

相關內容