使用 NiceTabular 在表格中换行时遇到问题

使用 NiceTabular 在表格中换行时遇到问题

编辑:我已设法调整表格大小以适合一页,但我实在想不通为什么第一行偏离了方向并打印到了第二行。我也不明白为什么第一行的文本没有换行。 在此处输入图片描述

\documentclass[a4paper,12pt,twoside,BCOR=10mm]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage[icelandic]{babel}
% \usepackage{t1enc}
\usepackage[T1]{fontenc}
\usepackage[dvipsnames]{xcolor}
\usepackage{graphicx,booktabs}
\usepackage[intoc]{nomencl}
\usepackage{enumerate,color}
\usepackage{url}
\usepackage{appendix}
\usepackage{float}
\usepackage{eso-pic}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{longtable}
\usepackage{acronym}
\usepackage{nicematrix}
%\usepackage[sf,normalsize]{subfigure}
\usepackage[format=plain,labelformat=simple,labelsep=colon]{caption}
\usepackage{placeins}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{adjustbox}
\usepackage{subcaption}
%\usepackage{subfig}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{external}
% Packages used for title page layout
\usepackage{tikz}
\usetikzlibrary{positioning}

\usepackage[pdfborder={0 0 0}]{hyperref}

% Blue color according to HÍ corporate design
\convertcolorspec{RGB}{16,9,159}{rgb}\tmphiblue
\definecolor{hiblue}{rgb}\tmphiblue


\setlength{\parskip}{\baselineskip}
\setlength{\parindent}{0cm}
\raggedbottom

\setkomafont{captionlabel}{\itshape}
\setkomafont{caption}{\itshape}
\setkomafont{section}{\FloatBarrier\Large}
\setcapwidth{\textwidth}



\usepackage{lmodern} % Use Latin Modern (instead of the default Computer Modern that is rendered using a bitmap font).
\usepackage{fixcmex} % To fix that Latin Modern large symbol math fonts has by default only one size: https://tex.stackexchange.com/a/621536

\usepackage{mathptmx}

\graphicspath{{figs/}} % Figures in directory figs


\usepackage[backend=biber, style=authoryear]{biblatex}  % BibLaTeX used for references. 
\usepackage{csquotes} % BibLaTex wants to have context sensitive quotes
\addbibresource{references.bib} %  Name of *.bib file containing references

\begin{document}

\begin{table}
      \centering
      {\small\renewcommand{\arraystretch}{.8}
      \caption{Spá um raforkunotkun, forgangsorka og skerðanleg orka. Almenn notkun að meðtöldum dreifitöpum og núverandi og samþykktir nýir stórnotendur (*Rauntölur). \label{table:Orkuspá}}
      \resizebox{!}{.3\paperheight}{
      \begin{NiceTabular}{lcccccccccc}
      \hline
      \Block{3-1}{Ár} & \Block{3-2}{Úttekt frá dreifikerfinu forgangs- og skerðanleg orka með dreifitöpum} & \Block{3-2}{Úttekt frá flutningskerfinu} & \Block{3-2}{Flutningstöp} & \Block{3-2}{Úttekt frá vinnslufyrirtækjum} & \Block{3-2}{Samtals} \\
      & & & & & & & & & & \\
      & & & & & & & & & & \\
      & Orka GWh & Afl MW & Orka GWh & Afl MW & Orka GWh & Afl MW & Orka GWh & Afl MW & Orka GWh & Afl MW \\
      \hline
      2015* & 3.604,74 & 618,46 & 14.356,34 & 1.685,89 & 369,56 & 57,11 & 468,32 & 75,40 & 18.798,96 & 2.383,25 \\
    2016* & 3.383,99 & 590,10 & 14.334,19 & 1.695,96 & 360,42 & 57,05 & 470,86 & 71,46 & 18.549,46 & 2.378,51 \\
    2017* & 3.519,13 & 608,86 & 14.869,95 & 1.750,87 & 373,07 & 61,49 & 476,38 & 84,74 & 19.238,53 & 2.443,61 \\
      \end{NiceTabular}}}
    \end{table}
\end{document}

我试图让单元格内的长文本换行,就像下面的图片一样。但是,短标题应该位于单元格的中间,这就是为什么我把所有\Block{3-*} 在此处输入图片描述 不幸的是,我还没有找到我的问题的正确答案,但我会继续寻找。

答案1

您误解了\Block{X-Y},它获取单元格的内容并将其分散到当前X单元格下方和Y右侧的单元格区域。因此,在您的示例中,\Block{3-1}{Ár}将内容放在Ár三个单元格深、一个单元格宽的区域中间。

更明显的是

\begin{NiceTabular}{ccc}
\Block{2-2}{a} & b & c \\
d & e & f \\
g & h & i \\
\end{NiceTabular}

它可以用于跨越多列或多行,

\begin{NiceTabular}{ccc}
\Block{2-1}{a} & \Block{1-2}{b} &  \\
 & e & f \\
g & h & i \\
\end{NiceTabular}

但它不会自动创建多个单元格,因此

\begin{NiceTabular}{ccc}
\Block{1-3}{a} & b & c \\
 & e & f \\
g & h & i \\
\end{NiceTabular}

不会创建三个单元格a,然后创建两个带有 和 的附加单元格,b而是c将其设置a在三列的中间,然后将其放置b在第二列的相同位置,然后放置在第三列。相反,我们需要在第四列之后c手动添加两个空单元格。\Block{1-3}{a}b

所以你想要更多类似的东西

\begin{NiceTabular}{lcccc}
\hline
\Block{2-1}{Ár} & \Block{1-2}{Úttekt frá dreifikerfinu forgangs- og skerðanleg orka með dreifitöpum} & & \Block{1-2}{Úttekt frá flutningskerfinu} \\
& Orka GWh & Afl MW & Orka GWh & Afl MW \\
\hline
2015* & 3.604,74 & 618,46 & 14.356,34 & 1.685,89 \\
\end{NiceTabular}

不过,的多列变体\Block不会引入任何自动换行(单列\Block{1-X}可以,但在这种情况下也不会,因为您只使用l- 和c- 类型列)。这需要手动输入

\begin{NiceTabular}{lcccc}
\hline
\Block{2-1}{Ár} & \Block{1-2}{Úttekt frá dreifikerfinu\\ forgangs- og skerðanleg\\ orka með dreifitöpum} & & \Block{1-2}{Úttekt frá\\ flutningskerfinu} \\
& Orka GWh & Afl MW & Orka GWh & Afl MW \\
\hline
2015* & 3.604,74 & 618,46 & 14.356,34 & 1.685,89 \\
\end{NiceTabular}

可以通过设置块内的对齐方式\Block[<options>]{X-Y}{<content>},但默认为居中,所以我认为我们已经知道您想要做的事情。

就我个人而言,我更倾向于\multicolumn使用固定宽度居中的列类型(如何创建带有文本“raggedright/centered/raggedleft”的固定宽度表格列?

\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{tabular}{lcccc}
\hline
Ár & \multicolumn{2}{C{4cm}}{Úttekt frá dreifikerfinu forgangs- og skerðanleg orka með dreifitöpum} & \multicolumn{2}{C{4cm}}{Úttekt frá flutningskerfinu} \\
& Orka GWh & Afl MW & Orka GWh & Afl MW \\
\hline
2015* & 3.604,74 & 618,46 & 14.356,34 & 1.685,89 \\
\end{tabular}

或者Zarko 的回答使用一些额外的花哨功能,效果非常好。

答案2

我宁愿使用tabularray带有库的包amsmathbooktabssiunitx(它们加载同名的包)。除此之外,包ragged2e和选项的使用hspan=minimal可以更好地填充多列单元格中的文本:

在此处输入图片描述

\documentclass[a4paper,12pt,twoside,BCOR=10mm]{scrbook}
\usepackage[icelandic]{babel}
\usepackage[T1]{fontenc}
\usepackage{lmodern} % Use Latin Modern (instead of the default Computer Modern that is rendered using a bitmap font).
\usepackage[format=plain,
            labelformat=simple,
            labelsep=colon]{caption}

\usepackage{ragged2e}
\usepackage[dvipsnames]{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{amsmath, booktabs, siunitx}

\begin{document}
    \begin{table}
    \footnotesize
    \caption{Spá um raforkunotkun, forgangsorka og skerðanleg orka. Almenn notkun að meðtöldum dreifitöpum og núverandi og samþykktir nýir stórnotendur (*Rauntölur). 
    \label{table:Orkuspá}}
\begin{tblr}{colsep  = 4pt,
             colspec = {@{} X[l] * {10}{Q[c]}
                        @{} },
            row{1,2} = {guard,  m, mode=text, font=\scriptsize},
            cell{1}{even[2]} = {c=2}{f, cmd=\RaggedRight},
            hspan=minimal, 
            }
    \toprule           
        & Úttekt frá dreifikerfinu forgangs- og skerðanleg orka með dreifitöpum 
                &         &  Úttekt frá flutningskerfinu
                                      &          &  Flutningstöp  
                                                            &       & Úttekt frá vinnslufyrirtækjum
                                                                           &       & Samtals
                                                                                               &            \\
    \cmidrule[lr]{2-3}
    \cmidrule[lr]{4-5}
    \cmidrule[lr]{6-7}
    \cmidrule[lr]{8-9}
    \cmidrule[l]{10-11}
Ár    & {Orka\\ GWh} 
                  & {Afl\\ MW} 
                          & {Orka\\ GWh} 
                                      & {Afl\\ MW} 
                                                 & {Orka\\ GWh} 
                                                          & {Afl\\ MW}
                                                                  & {Orka\\ GWh}
                                                                           & {Afl\\ MW}
                                                                                   & {Orka\\ GWh}
                                                                                               & {Afl\\ MW} \\
      \midrule
2015* & 3.604,74 & 618,46 & 14.356,34 & 1.685,89 & 369,56 & 57,11 & 468,32 & 75,40 & 18.798,96 & 2.383,25   \\
2016* & 3.383,99 & 590,10 & 14.334,19 & 1.695,96 & 360,42 & 57,05 & 470,86 & 71,46 & 18.549,46 & 2.378,51   \\
2017* & 3.519,13 & 608,86 & 14.869,95 & 1.750,87 & 373,07 & 61,49 & 476,38 & 84,74 & 19.238,53 & 2.443,61   \\
    \bottomrule
\end{tblr}
    \end{table}
\end{document}

相关内容