# Fitting caption width to table width using nicematrix package

I'm very much a LaTeX beginner and searching for possibilities to include footnotes below tables, I came across the nicematrix package. This package combines basically everthing I need to generate tables in my document. The only thing I'm missing is the possibility to fit the caption width to the table width. I tried using the package floatrow that is mentioned in the caption package manual, but that messes up the table footnotes generated by NiceMatrix (they simply do no longer appear in the document). Since the table footnotes are already fitted to the table width, I was hoping there might be a similar way to achieve this. For most tables, this isn't an issue as the caption is smaller in width than the table anyway, but in the example shown below, it doesn't look to good for my taste.

The entire document is compiled using LuaLaTeX, but I don't think it is mandatory here.

\usepackage{caption} [2022/02/20]
\captionsetup{%
format=hang,%
labelsep=period%
}
\setkomafont{captionlabel}{\sffamily\bfseries}

\usepackage{booktabs}

\usepackage{nicematrix}
\NiceMatrixOptions{%
cell-space-limits = 2pt,%
notes/code-before = \footnotesize%
}

\usepackage{enumitem}

\usepackage{siunitx}
\newcolumntype{A}[1]{%
S[%
table-number-alignment = center,%
table-format = #1%
]%
}

\begin{document}

\begin{table}[ht]
\caption{This is a long long long caption that needs to be fitted to the
table width}
\label{tab:Activity}
\centering
\toprule
\Block{2-1}{Nuclide}    & \Block{1-2}{Maximal activity in \unit{Bq}}  \\
\cmidrule(lr){2-3}
& {Material~1}                 & {Material~2} \\
\midrule
$^{99}\mathrm{Tc}$\tabularnote{This is a long long long tabular note
that is already fitted to the table width.}
& 10.1e9                       & 1.1e5        \\
\bottomrule
\end{NiceTabular}
\end{table}

\end{document}

Can anybody help realizing what I'm trying to achieve?

Furthermore, since I'm a LaTeX beginner, feel free to mention and correct stupid things I've done in the code above.

Thank you very much, Sebastian

New contributor
koXx is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
• Welcome to TeX.SE! Jun 23 at 12:22

This solution uses the package floatrow to fit the caption and add the footnote.

\usepackage{caption} [2022/02/20]
\captionsetup{%
format=hang,%
labelsep=period%
}

format=hang,
hangindent=2ex,
}

\setkomafont{captionlabel}{\sffamily\bfseries}
\usepackage{booktabs}

\usepackage{floatrow} % added <<<<<<<<<<<<<<<

\usepackage{nicematrix}
\NiceMatrixOptions{%
cell-space-limits = 2pt,%
notes/code-before = \footnotesize%
}

\usepackage{enumitem}
\usepackage{siunitx}
\newcolumntype{A}[1]{%
S[%
table-number-alignment = center,%
table-format = #1%
]%
}

\begin{document}

\begin{table}[ht]
\centering
\caption{This is a long long long caption that needs to be fitted to the table width}\label{tab:Activity}}%
\toprule
\Block{2-1}{Nuclide}    & \Block{1-2}{Maximal activity in \unit{Bq}}  \\
\cmidrule(lr){2-3}
& {Material~1}          & {Material~2} \\
\midrule
$^{99}\mathrm{Tc}$\mpfootnotemark   & 10.1e9 & 1.1e5        \\ % changed <<<<<<<<<<<<<<<
\bottomrule
\end{NiceTabular}%
\floatfoot{\textsuperscript{\thempfootnote}\ This is a long long long tabular note  that is already fitted to the table width.} % added <<<<<<<<<<<<<<<
}
\end{table}

\end{document}
• This generally works - thank you very much! However, I really like the alignment of the footnote labels and the hanging indent of the footnote text provided by the NiceMatrix table footnotes. Can we achieve something similar with your proposed solution? Also I really don't like the generated links for the table footnotes when hyperref is used (I am redirected to the end of the document when clicking on the footnote marker). Is it possible to disable that for the table footnotes?
– koXx
Jun 23 at 15:41
• After some research in the KOMA script and hyperref manual, I was able to work it out by myself. I added the option hyperfootnotes=false to the hyperref package and \deffootnote[1em]{1em}{1em}{\textsuperscript{\thefootnotemark}} in the preamble. Hopefully, the hyperref option has no side effect, but so far everything looks fine.
– koXx
Jun 24 at 8:27
• @koXx Thank you for your feedback. I updated the answer adding the hanging indent. Jun 24 at 15:16
• Do you know why the caption slightly extends beyond the width of the table? All the examples shown in the floatrow manual suggest that this should not be the case. It has nothing to do with the nicematrix package because the same behavior occurs without using it.
– koXx
21 hours ago