1

Edit: I have managed to resize the table to fit into one page, but for the life of me, I can't figure out why the first line is way off and gets printed into line two. I also don't understand why the text in first line doesn't get wrapped. enter image description here

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

I am trying to have the long text within the cells to wrap similar to picture here below. However, the short headings should be located in the middle of the cell, which is why I have all \Block{3-*} enter image description here Unfortunately I haven't stumbled onto the correct answer for my problem, but I will keep looking.

Ingmar
  • 6,690
  • 5
  • 26
  • 47
  • Compiling I get something too mangled to play with, but \Block{3-1} is creating a cell spanning 3 rows and 1 column, so "Ár" would appear on the second row of the first column as expected, ditto the next entry "Úttekt frá [...]" is three rows tall so appears on the second row. It probably still leaves some overlap to be resolved with line breaking within the cell, but do you mean to have every Block{3-*} as Block{1-*}? – Dai Bowen Apr 12 '23 at 13:32
  • I am not sure if I understand "have every Block{3-*} as Block{1-*}. I have this table in Excel, and the headings are bit of a mess. I am fine with "Ár" showing up on line 2. I was hoping that "Úttekt frá ..." would span the whole three lines, and be abowe the two columns below. Except "Ár", all the other headings have two columns below it. Maybe I just need to configure this more manually? – Ólafur Víðir Guðbjargarson Apr 12 '23 at 13:41
  • 1
    Right, yes, \Block will span rows that already exist, so if you want the first row to span three rows, you need two rows of empty cells beneath it (otherwise it'll overlap the content with those non-empty cells). Using \Block{1-2} will get you the multi-column aspect, but you'll need something else to have the cell spanning multiple lines. I'd do this with \multicolumn and either manually line break within the cell or use a fixed-width column. – Dai Bowen Apr 12 '23 at 13:50
  • OK, I added two lines below, but for some reason the text within \Block{3-2} does not wrap at all, and it looks like a hot mess. – Ólafur Víðir Guðbjargarson Apr 12 '23 at 13:54
  • @DaiBowen I did try to use \multicolumn and \multiline with breaklines and fixed width of column, but the text would not wrap. Did not think of \cell, will try that. – Ólafur Víðir Guðbjargarson Apr 12 '23 at 14:10

2 Answers2

2

You're misunderstanding \Block{X-Y}, it takes the contents of a cell and spreads it over an area X cells down and Y cells right of the current cell. So in your example \Block{3-1}{Ár} is putting the contents Ár in the middle of an area three cells deep and one cell wide.

It's more apparent in

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

It can be used to both span multiple columns or multiple rows,

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

but it doesn't automatically create multiple cells, so

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

doesn't create three cells spanned by a, then two additional cells with b and c, it sets a in the middle of the three columns, then places b in the second column in the same place, and c in the third column. Instead we'd need to manually add two empty cells after \Block{1-3}{a} to have b in a fourth column.

So you want something more like

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

The multi-column variants of \Block doesn't introduce any automatic linebreaking though (the mono-column \Block{1-X} can but in this case wouldn't either as you are using only l- and c- type columns). That needs to come in manually

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

The alignment within the block can be set by \Block[<options>]{X-Y}{<content>} but defaults to centering so I think we have what you were trying to do.

Personally I'd be more inclined to go with something like \multicolumn using a fixed width centred column type (How to create fixed width table columns with text 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}

or Zarko's answer uses a few more bells and whistles to very nice effect.

Dai Bowen
  • 6,117
1

I would rather use tabularray package with libraries amsmath, booktabs and siunitx (which load packages of the same names). Beside them, the package ragged2e and use of the option hspan=minimal enables better filling of text in the multi column cells:

enter image description here

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

Zarko
  • 296,517