5

I am trying to save horizontal space in a nicematrix NiceTabularX table as the report my table is going in has a page limit (the table is half a page long). I desire using nicematrix as I use tikz, \tabularnote{} and a coloured header which doesn't bleed in my main table so would prefer to use this. By way of an image, this is how my MWE table looked initially (colour added manually):

enter image description here

I have controlled the red distance so it is flush to the left margin by using \begin{NiceTabularX}{\textwidth}{@{}c!{}X} (pg. 7 of the nicematrix documentation) but I do not know how to control the distance of the blue line. I want there to be some space but also want to control as I have pruned my table but reducing the space could perhaps save me a line or two which is desirable.

Using {\textwidth}{@{}c!{}@{}l!{}} produced this so I feel that I need the X column:

enter image description here

Does anyone know how I can control the distance between the first and second columns in a NiceTabularX table whilst allowing line breaks please? I cannot claim too much experience with tables so apologies if this is basic or asked before, I checked the NiceTabularX questions on here.

How it currently looks:

enter image description here

MWE:

\documentclass{book}

\usepackage{nicematrix}
\usepackage{booktabs}
\usepackage{lipsum}

\begin{document}

\begin{table}
\footnotesize

\begin{NiceTabularX}{\textwidth}{@{}c!{}X}
\toprule
\textbf{Year} & \textbf{Important milestones or key achievements in de novo enzyme design}\\ \midrule
1983-84 & \lipsum[1][1-3] \\ \addlinespace[0.1em]
1994    & \lipsum[1][1-3] \\ \addlinespace[0.1em]
1995    & \lipsum[1][1-3] \\ \addlinespace[0.1em]
2000-04 & \lipsum[1][1-3] \\ \addlinespace[0.1em]
2010-14 & \lipsum[1][1-3] \\ \addlinespace[0.1em]
\bottomrule
\end{NiceTabularX}
\end{table}

\end{document}
3
  • 3
    The horizontal amount of white space that is added to either side of the text in a cell is controlled via \tabcolsep. By default it amounts to 6pt. You can change this by using \setlength{\tabcolsep}{3pt} or any other value of your choice. Adding this to the preamble will affect all tables in your document, using it inside of the table environment will keep the effect locally.
    – leandriis
    Apr 8, 2023 at 9:33
  • Cannot believe it was that basic, so sorry!! Now I know, would you like to post as an answer please?
    – JamesT
    Apr 8, 2023 at 9:37
  • 3
    I would use \begin{NiceTabularX}{\textwidth}{@{} c X @{}} or rather \begin{NiceTabularX}{\textwidth}{@{} l X @{}} and then see if the \tabcolsep is needed to be reduced too.
    – Zarko
    Apr 8, 2023 at 9:45

2 Answers 2

5

You don't seem to be making effective use of the various easily available spacing options. For instance, you could insert @{} at both ends of the tabular-like environment in order to eliminate any unwanted whitespace padding at the edges of the tabular-like environment.

The amount of intercolumn whitespace is given by 2\tabcolsep. To generate less of this whitespace, you can either set a smaller value for \tabcolsep (its default value is 6pt in most document classes), or you can state directly how much whitespace you're willing to allow between columns 1 and 2 -- by writing, say, @{\hspace{4pt}} if 4pt is the desired amount of whitespace.

I'm not sure if your real document makes further use of the machinery provided by the nicematrix package. As the following screenshot demonstrates, there's no difference between using a NiceTabularX environment or just a plain old tabularx environment, for the code you've provided.

Oh, please do use -- rather than - to generate typographic en-dashes between year groups.

enter image description here

\documentclass{book}
\usepackage{tabularx,nicematrix}
\usepackage{booktabs,lipsum}

\newcommand\blob{%
  \toprule
  \textbf{Year} & 
  \textbf{Important milestones or key achievements in de novo enzyme design}\\ 
  \midrule
  1983--84 & \lipsum[1][1-3] \\ \addlinespace[0.1em]
  1994     & \lipsum[1][1-3] \\ \addlinespace[0.1em]
  1995     & \lipsum[1][1-3] \\ \addlinespace[0.1em]
  2000--04 & \lipsum[1][1-3] \\ \addlinespace[0.1em]
  2010--14 & \lipsum[1][1-3] \\ \addlinespace[0.1em]
  \bottomrule}

\begin{document}

\begin{table}
\footnotesize

\begin{NiceTabularX}{\textwidth}{@{} l @{\hspace{4pt}} X @{}}
\blob
\end{NiceTabularX}

\bigskip

\begin{tabularx}{\textwidth}{@{} l @{\hspace{4pt}} X @{}}
\blob
\end{tabularx}

\end{table}

\end{document}
1
  • 1
    Thank you Mico for the explanation, I need to get better with tables as they are my weak link. I have bookmarked your answer and will return to it in the future to get better, I accepted pretty quick as it explained and solved my issue and I am happy with it
    – JamesT
    Apr 8, 2023 at 10:01
3

Well, you stres that solution should consider nicematrix package, but anyway use tabularray is a good alternative (which need to be compiled only ones):

\documentclass{book}

\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\usepackage{lipsum}

\begin{document}
    \begin{table}
    \footnotesize
\begin{tblr}{colsep  = 4pt, % or desired amount
             colspec = {@{} l X[j] @{}},
             row{1}  = {font=\bfseries}
             }
    \toprule
Year    & Important milestones or key achievements in de novo enzyme design \\
    \midrule
1983-84 & \lipsum[1][1-3] \\
1994    & \lipsum[1][1-3] \\
1995    & \lipsum[1][1-3] \\
2000-04 & \lipsum[1][1-3] \\
2010-14 & \lipsum[1][1-3] \\
    \bottomrule
\end{tblr}
    \end{table}
\end{document}

enter image description here

6
  • +1. You may want to point out somewhere -- other than by referring the reader to the package's user guide... -- that setting colsep = 4pt results in 2*4pt=8pt of intercolumn separation. Put differently, if the desired amount of intercolumn whitespace is 4pt, one has to set colsep = 2pt.
    – Mico
    Apr 8, 2023 at 10:10
  • +1 thank you for the answer, it could help future readers looking for an alternative, the more alternatives the better! :)
    – JamesT
    Apr 8, 2023 at 10:10
  • @Mico, of course.colsep define padding between column content and its left/right borders (as \tabcolsep do) For have distance between contents of neighbour cells only 4 pt, then you should set colsep=2pt. Or do I miss something?
    – Zarko
    Apr 8, 2023 at 10:19
  • 1
    @Mico, you are right! Now I see, that you explain this in your answer. Do I add some words about this in my answer too?
    – Zarko
    Apr 8, 2023 at 11:04
  • 1
    @Mico, No luck with my answer :-(. Firs I upload it to wrong question -- so you beat me for four minutes ;-) -- now, when I try to upvote than your answer, crash my PC. It seems that both are too old ... 😉
    – Zarko
    Apr 8, 2023 at 11:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .