Latex 中的多列和多行表

Latex 中的多列和多行表

我正在尝试生成下表:

enter image description here

我的代码是:

    \documentclass[12pt, a4paper]{article}
\usepackage{multirow}
\usepackage[labelfont=bf,labelsep=period]{caption}
\captionsetup[table]{skip = 1 pt}

    \begin{document}

    \begin{table}[!ht]\scriptsize
\caption{Comércio entre UE e Mercosul.}
\centering
\begin{tabular}{c c c c c c c c c}
    \toprule
    \multirow{3}{*}{\bf Itens} & \multicolumn{4}{c}{\bf Importação} & \multicolumn{4}{c}{Exportação} \\
    \cmidrule(r){2-5}\cmidrule(r){6-9}
    & {\bf 2013} & {\bf 2016} & {\bf 2013} & {\bf 2016} \\
    & \multicolumn{2}{c}{} & \multicolumn{2}{c}{} & \multicolumn{2}{c}{} & \multicolumn{2}{c}{} \\
   \cmidrule(r){2-3}\cmidrule(r){4-5}\cmidrule(r){6-7}\cmidrule(r){8-9}
    & Euro & \% & Euro & \% & Euro & \% & Euro & \% \\
    \hline    
        Total & 47.352 & 100,00 & 41.895 & 100,00 & 56.823 & 100,00 & 43.111 & 100,00 \\
        Produtos primários & 34.379 & 72,60 & 29.809 & 71,17 & 6.647 & 11,70 & 4.739 & 10,99 \\
        Manufaturados & 9.762 & 20,62 & 10.066 & 24,03 & 47.971 & 84,42 & 36.852 & 85,48 \\
        Outros produtos & 876 & 1,85 & 1.332 & 3,18 & 825 & 1,45 & 626 & 1,45 \\
        Outros & 2.336 & 4,93 & 687 & 1,64 & 1.379 & 2,43 & 894 & 2,07 \\
    \bottomrule
        \multicolumn{5}{l}{\tiny {\it Nota}: Mercosul 5 e União Europeia 28.} \\
        \multicolumn{5}{l}{\tiny Fonte: Eurostat comext (2017).} \\
\end{tabular}
\label{tab2}
    \end{table}

    \end{document}

有谁能够帮助我?

谢谢你!

答案1

像这样?

enter image description here

(红线表示文字边框)

由于您没有提供有关文档的任何信息,因此我设计了自己的文档,这可能与您的文档有很大不同:

\documentclass{article}
% my preamble may be different from yours
\usepackage{geometry}
\usepackage[portuges]{babel}
\usepackage[utf8]{inputenc}
\usepackage{siunitx}
\usepackage[skip=1ex]{caption}
\usepackage{booktabs,
            multirow,
            tabularx}

\begin{document}
    \begin{table}%[!ht]
\caption{Comércio entre UE e Mercosul.}
\label{tab2}
    \sisetup{%table-text-alignment=center, % used in table columns
             table-number-alignment=center,
             input-decimal-markers={,},
             output-decimal-marker={,},
             group-minimum-digits=4,
             group-separator={.}
             }
    \cmidrulekern=2pt
\begin{tabularx}{\linewidth}{X
                        *{4}{S[table-figures-integer=5, 
                               table-figures-decimal=0]
                             S[table-figures-integer=3,
                               table-figures-decimal=2]}
                            }
    \toprule
    & \multicolumn{4}{c}{\textbf{Importação}}
                      & \multicolumn{4}{c}{\textbf{Exportação}}     \\
    \cmidrule(lr){2-5}\cmidrule(lr){6-9}
\textbf{Itens}
    & {\textbf{2013}} & {\textbf{2016}} & {\textbf{2013}} & {\textbf{2016}}
    & {\textbf{2013}} & {\textbf{2016}} & {\textbf{2013}} & {\textbf{2016}} \\
   \cmidrule(lr){2-3}\cmidrule(lr){4-5}
   \cmidrule(lr){6-7}\cmidrule(lr){8-9}
    & {Euro}    & {\%}      & {Euro}    & {\%}
    & {Euro}    & {\%}      & {Euro}    & {\%}          \\
    \midrule
Total
    & 47 352    & 100,00    & 41 895    & 100,00
    & 56 823    & 100,00    & 43 111    & 100,00        \\
    \midrule
Produtos primários
    & 34 379    & 72,60     & 29 809    & 71,17
    &  6 647    & 11,70     &  4 739    & 10,99         \\
Manufaturados
    &  9 762    & 20,62     & 10 066    & 24,03
    & 47 971    & 84,42     & 36 852    & 85,48         \\
Outros produtos
    &   876     &  1,85     & 1 332     &  3,18
    &   825     &  1,45     &   626     &  1,45         \\
Outros
    & 2 336     &  4,93     &   687     &  1,64
    & 1 379     &  2,43     &   894     &  2,07         \\
    \bottomrule
\multicolumn{9}{p{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth\relax}}{\footnotesize
    \textit{Nota}: Mercosul 5 e União Europeia 28.\newline
    \textit{Fonte}: Eurostat comext (2017).
                }
\end{tabularx}
    \end{table}
\end{document}

答案2

除了 Zarko 的精彩回答之外,还有一些建议。

  1. 的设置output-decimal-marker应在文档级别。
  2. 永远不要使用句号来分隔数字组,因为它会产生歧义。最好将工作留给siunitx那些会使用小空格的人。
  3. 表中我们有三到五位数字的混合,因此我们将其添加group-four-digits以保持一致性。
  4. \footnotesize只要你稍微挤压一下,就能把桌子放进去。

计算很简单,尽管需要一点手工操作。当表格排版时没有调整到\tabcolsep,报告的溢出量为 30.65004pt。我们有 16 个列间距可以处理,因为左右填充已被 移除@{}。将数量除以 16 得出的结果略小于 2pt,可以安全地将其减去\tabcolsep

在第一次运行中,将参数\addtolenght设置为0pt;然后我进行了计算并修复了长度。

该设置仅限于特定table环境,因此不会传播到其他表。

\documentclass[12pt, a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[brazil]{babel}

\usepackage{siunitx,booktabs}
\usepackage{eurosym}

\usepackage[labelfont=bf,labelsep=period]{caption}
\captionsetup[table]{skip = 1pt}

\sisetup{output-decimal-marker={,}}

\begin{document}

\begin{table}[!htp]
\caption{Comércio entre UE e Mercosul.}\label{tab2}

\centering
\footnotesize

\addtolength{\tabcolsep}{-1.91563pt}
% Overfull was 30.65004; divided by 16 gives 1.9156275

\begin{tabular}{
  @{}
  l
  S[table-format=5.0,group-four-digits]
  S[table-format=3.2]
  S[table-format=5.0,group-four-digits]
  S[table-format=3.2]
  S[table-format=5.0,group-four-digits]
  S[table-format=3.2]
  S[table-format=5.0,group-four-digits]
  S[table-format=3.2]
  @{}
}
\toprule
\textbf{Itens} &
\multicolumn{4}{c}{\bfseries Importação} &
\multicolumn{4}{c}{\bfseries Exportação} \\
\cmidrule(lr){2-5} \cmidrule(l){6-9}
& \multicolumn{2}{c}{\bfseries 2013} & \multicolumn{2}{c}{\bfseries 2016}
& \multicolumn{2}{c}{\bfseries 2013} & \multicolumn{2}{c}{\bfseries 2016} \\
\cmidrule(lr){2-3}\cmidrule(lr){4-5}\cmidrule(lr){6-7}\cmidrule(l){8-9}
& {\euro/1000} & \% 
& {\euro/1000} & \% 
& {\euro/1000} & \% 
& {\euro/1000} & \% \\
\midrule
Total              & 47352 & 100,00 & 41895 & 100,00 & 56823 & 100,00 & 43111 & 100,00 \\
Produtos primários & 34379 &  72,60 & 29809 &  71,17 &  6647 &  11,70 &  4739 &  10,99 \\
Manufaturados      &  9762 &  20,62 & 10066 &  24,03 & 47971 &  84,42 & 36852 &  85,48 \\
Outros produtos    &   876 &   1,85 &  1332 &   3,18 &   825 &   1,45 &   626 &   1,45 \\
Outros             &  2336 &   4,93 &   687 &   1,64 &  1379 &   2,43 &   894 &   2,07 \\
\midrule[\heavyrulewidth]
\multicolumn{9}{@{}l@{}}{\scriptsize \textit{Nota}: Mercosul 5 e União Europeia 28.} \\
\multicolumn{9}{@{}l@{}}{\scriptsize \textit{Fonte}: Eurostat comext (2017).} \\
\end{tabular}

\end{table}

\end{document}

另一种方法是使用tabular*

\documentclass[12pt, a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[brazil]{babel}

\usepackage{siunitx,booktabs}
\usepackage{eurosym}

\usepackage[labelfont=bf,labelsep=period]{caption}
\captionsetup[table]{skip = 1pt}

\sisetup{output-decimal-marker={,}}

\begin{document}

\begin{table}[!htp]
\caption{Comércio entre UE e Mercosul.}\label{tab2}

\centering
\footnotesize

\setlength{\tabcolsep}{0pt}

\begin{tabular*}{\textwidth}{
  @{\extracolsep{\fill}}
  l
  S[table-format=5.0,group-four-digits]
  S[table-format=3.2]
  S[table-format=5.0,group-four-digits]
  S[table-format=3.2]
  S[table-format=5.0,group-four-digits]
  S[table-format=3.2]
  S[table-format=5.0,group-four-digits]
  S[table-format=3.2]
  @{}
}
\toprule
\textbf{Itens} &
\multicolumn{4}{c}{\bfseries Importação} &
\multicolumn{4}{c}{\bfseries Exportação} \\
\cmidrule(r{1pt}){2-5} \cmidrule(l{1pt}){6-9}
& \multicolumn{2}{c}{\bfseries 2013} & \multicolumn{2}{c}{\bfseries 2016}
& \multicolumn{2}{c}{\bfseries 2013} & \multicolumn{2}{c}{\bfseries 2016} \\
\cmidrule(r{1pt}){2-3}\cmidrule(l{1pt}r{1pt}){4-5}
\cmidrule(l{1pt}r{1pt}){6-7}\cmidrule(l{1pt}){8-9}
& {\euro/1000} & \% 
& {\euro/1000} & \% 
& {\euro/1000} & \% 
& {\euro/1000} & \% \\
\midrule
Total              & 47352 & 100,00 & 41895 & 100,00 & 56823 & 100,00 & 43111 & 100,00 \\
Produtos primários & 34379 &  72,60 & 29809 &  71,17 &  6647 &  11,70 &  4739 &  10,99 \\
Manufaturados      &  9762 &  20,62 & 10066 &  24,03 & 47971 &  84,42 & 36852 &  85,48 \\
Outros produtos    &   876 &   1,85 &  1332 &   3,18 &   825 &   1,45 &   626 &   1,45 \\
Outros             &  2336 &   4,93 &   687 &   1,64 &  1379 &   2,43 &   894 &   2,07 \\
\midrule[\heavyrulewidth]
\multicolumn{9}{@{}l@{}}{\scriptsize \textit{Nota}: Mercosul 5 e União Europeia 28.} \\
\multicolumn{9}{@{}l@{}}{\scriptsize \textit{Fonte}: Eurostat comext (2017).} \\
\end{tabular*}

\end{table}

\end{document}

两种方法的输出基本相同。

enter image description here

相关内容