使用`floatrow`,如何在表格的第一行添加标题?

使用`floatrow`,如何在表格的第一行添加标题?

我有一个大项目,floatrow使用了该包。我发现当使用这个包时,放在我表格内的标题现在显示在外面。我找不到任何方法把它放回里面。

这是我的 MWE:

\documentclass{article} % Document class
\usepackage{hyperref} % For hyperlinks
\usepackage{siunitx} % For typesetting numbers and units
\usepackage{nicematrix} % For creating tables
\usepackage{caption} % For customizing captions
\usepackage{xcolor} % For custom colors
\usepackage{floatrow} % For customizing float environments

%%% Color definition
\definecolor{gray-100}{HTML}{f3f4f6}
\definecolor{gray-200}{HTML}{e5e7eb}
\definecolor{gray-600}{HTML}{4b5563}
\definecolor{gray-800}{HTML}{1f2937}
\definecolor{slate-50}{HTML}{f8fafc}

\begin{document}

\begin{table}
   \centering % Center the table
   \renewcommand{\arraystretch}{1.5} % Adjust row height
\captionsetup[table]{font={bf,sf,color=gray-600,small}, skip=0pt} % Customize table caption
\begin{NiceTabular}{lcS}[hvlines, rounded-corners=6pt, rules/color=gray-200] % Create NiceTabular with specified properties
\CodeBefore % LaTeX code before the table
\rowcolor{gray-100}{1-2} % Colorize the first row
   \rowcolors{3}{}{slate-50} % Alternate row colors starting from the third row
\Body % Body of the table
   \Block{1-3}{\begin{minipage}{50mm}\captionof{table}{Table Title 1}\label{tab:title1}\end{minipage}} \\ % Multi-column cell for caption
   \RowStyle[bold]{\color{gray-600}} % Style for the first data row
    Town & Correspondant & {Value} \\ % Table headers
    \RowStyle[nb-rows=*,color=gray-800]{} % Style for the remaining data rows
    Paris & Peter & 1.23 \\ % Data rows
    London & Helena & 23.23\\
    Madrid & Georges &12.5 \\
    Moscou & Henry & 4.77 \\
    Tokyo & Selena & 85.3 \\
    Lima & Victoria & 13.2 \\
    Cap Town & Alexandra & 6 \\
    Chicago & Vladimier & 1.34
\end{NiceTabular}
\end{table}

\end{document}

我使用 NiceTabular,但使用时也遇到同样的问题tcolorbox

预期结果:

预期结果,标题位于第一行

实际的:

使用 floatrow 包的实际结果:标题超出了表格范围

答案1

包装\captionofinfloatrow命令\RawCaption可以达到这样的效果:

\RawCaption{\captionof{table}{Table Title 1}\label{tab:title1}}

摘自floatrow的软件包手册 (v0.3b, 2009-08-02),第 2.4.1 节“原始标题——以不寻常的方式打印”

\RawCaption

此命令允许从floatrow包为创建必要的布局而创建的特殊框寄存器中“释放”标题内容。标题作为以下参数放置\RawCaption

\RawCaption{\caption\marg{contents}\label{...}}` .
\documentclass{article} % Document class
\usepackage{siunitx} % For typesetting numbers and units
\usepackage{nicematrix} % For creating tables
\usepackage{caption} % For customizing captions
\usepackage{xcolor} % For custom colors
\usepackage{floatrow} % For customizing float environments

\usepackage{hyperref} % For hyperlinks; load hyperref last

%%% Color definition
\definecolor{gray-100}{HTML}{f3f4f6}
\definecolor{gray-200}{HTML}{e5e7eb}
\definecolor{gray-600}{HTML}{4b5563}
\definecolor{gray-800}{HTML}{1f2937}
\definecolor{slate-50}{HTML}{f8fafc}

\begin{document}

\begin{table}
   \centering % Center the table
   \renewcommand{\arraystretch}{1.5} % Adjust row height
\captionsetup[table]{font={bf,sf,color=gray-600,small}, skip=0pt} % Customize table caption
\begin{NiceTabular}{lcS}[hvlines, rounded-corners=6pt, rules/color=gray-200] % Create NiceTabular with specified properties
\CodeBefore % LaTeX code before the table
\rowcolor{gray-100}{1-2} % Colorize the first row
   \rowcolors{3}{}{slate-50} % Alternate row colors starting from the third row
\Body % Body of the table
   \Block{1-3}{%
    \begin{minipage}{50mm}
      \RawCaption{\captionof{table}{Table Title 1}\label{tab:title1}}
    \end{minipage}} \\ % Multi-column cell for caption
   \RowStyle[bold]{\color{gray-600}} % Style for the first data row
    Town & Correspondant & {Value} \\ % Table headers
    \RowStyle[nb-rows=*,color=gray-800]{} % Style for the remaining data rows
    Paris & Peter & 1.23 \\ % Data rows
    London & Helena & 23.23\\
    Madrid & Georges &12.5 \\
    Moscou & Henry & 4.77 \\
    Tokyo & Selena & 85.3 \\
    Lima & Victoria & 13.2 \\
    Cap Town & Alexandra & 6 \\
    Chicago & Vladimier & 1.34
\end{NiceTabular}
\end{table}

\end{document}

在此处输入图片描述

答案2

基本原理是可行的,尽管在这种情况下你没有把标题放在小页面中。另一方面,\RawCaption被添加到 floatrow 中是为了专门处理这个问题。

\documentclass{article} % Document class
\makeatletter
  \let\old@makecaption=\@makecaption
\makeatother
\usepackage{siunitx} % For typesetting numbers and units
\usepackage{nicematrix} % For creating tables
\usepackage{caption} % For customizing captions
\usepackage{xcolor} % For custom colors
\usepackage{floatrow} % For customizing float environments
\usepackage{hyperref} % For hyperlinks

%%% Color definition
\definecolor{gray-100}{HTML}{f3f4f6}
\definecolor{gray-200}{HTML}{e5e7eb}
\definecolor{gray-600}{HTML}{4b5563}
\definecolor{gray-800}{HTML}{1f2937}
\definecolor{slate-50}{HTML}{f8fafc}

\begin{document}

\begin{table}
\makeatletter
  \let\@makecaption=\old@makecaption
\makeatother
\centering % Center the table
\renewcommand{\arraystretch}{1.5} % Adjust row height
\captionsetup[table]{font={bf,sf,color=gray-600,small}, skip=0pt} % Customize table caption
\begin{NiceTabular}{lcS}[hvlines, rounded-corners=6pt, rules/color=gray-200] % Create NiceTabular with specified properties
\CodeBefore % LaTeX code before the table
\rowcolor{gray-100}{1-2} % Colorize the first row
   \rowcolors{3}{}{slate-50} % Alternate row colors starting from the third row
\Body % Body of the table
   \Block{1-3}{\begin{minipage}{50mm}\captionof{table}{Table Title 1}\label{tab:title1}\end{minipage}} \\ % Multi-column cell for caption
   \RowStyle[bold]{\color{gray-600}} % Style for the first data row
    Town & Correspondant & {Value} \\ % Table headers
    \RowStyle[nb-rows=*,color=gray-800]{} % Style for the remaining data rows
    Paris & Peter & 1.23 \\ % Data rows
    London & Helena & 23.23\\
    Madrid & Georges &12.5 \\
    Moscou & Henry & 4.77 \\
    Tokyo & Selena & 85.3 \\
    Lima & Victoria & 13.2 \\
    Cap Town & Alexandra & 6 \\
    Chicago & Vladimier & 1.34
\end{NiceTabular}
\end{table}

\end{document}

相关内容