如何将表格标题(标题)与表格左侧对齐,同时保持表格本身位于页面的中心?

如何将表格标题(标题)与表格左侧对齐,同时保持表格本身位于页面的中心?

我试图根据一个问题重新创建一个表格,并进行了一些修改(与该问题的内容无关)。我可以创建的表格如下所示:

表格图片

该表仍存在一些问题,例如

  1. 单元格之间的垂直间距,
  2. 颜色溢出侧面,并未填满整个单元格。

在这个问题中,我想只关注表格标题或标题。我想

  • 对齐标题,使其从表格开始的位置开始;
  • 表格应保持在页面的中心;
  • 我会不是喜欢拉伸表格标题或标题(因此在这种情况下,标题右侧应该保留一些空白。

其他类似问题一般都与标题一致而向左,这在这里行不通。

有什么想法我应该添加或修改吗?最好采用微创手术。


\documentclass[12pt]{article}
\usepackage[labelsep=period, tableposition=top, labelfont=bf]{caption}
\usepackage[table]{xcolor} %for cell colour
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{tabu} %for \cellcolor to work inside \rowstyle
\usepackage{siunitx} %for the case when the table had lots of numbers to align (not used here)

\makeatletter\usepackage{microtype}\g@addto@macro\@verbatim{\microtypesetup{activate=false}}\makeatother%

\renewcommand\cellalign{cr} %align inside \makecell

\newcolumntype{N}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{O}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  #1\ignorespaces
} %to make entire row boldface/color with one command instead of one at a time

\begin{document}

\begin{table}[!htbp]
    \caption{Title for your table goes here}
    \vspace{-0.5em}
    \centering
    \makebox[\textwidth][c]{\begin{tabular}{@{} Nc|Oc|Oc|Oc|Oc @{}}
        \toprule
        \rowstyle{\bfseries\cellcolor{cyan!70}}
        {Parabola} & {Curve} & {Focus} & {Directrix} & {Vertex}\\
        \midrule
        $x^2 = 4py$ & \makecell{up if $p > 0$ \\ down if $p < 0$} & $F(0, p)$ & $y = -p$ & $V(0, 0)$\\
        \cline{1-5}%horisontal line
        $y^2 = 4px$ & \makecell{right if $p > 0$\\ left if $p < 0$} & $F(p, 0)$ & $x = -p$ & $V(0, 0)$\\
        \bottomrule
        \end{tabular}}
    \label{table: label for table}
\end{table}
        
\end{document}

答案1

threeparttable在软件包和选项caption的帮助下singlelinecheck=false。(下图中的红线表示边距,我没有解决此表的其他各种问题。)

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage[labelsep=period, tableposition=top, labelfont=bf, singlelinecheck=false]{caption}
\usepackage[table]{xcolor} %for cell colour
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{tabu} %for \cellcolor to work inside \rowstyle
\usepackage{siunitx} %for the case when the table had lots of numbers to align (not used here)

\makeatletter\usepackage{microtype}\g@addto@macro\@verbatim{\microtypesetup{activate=false}}\makeatother%

\renewcommand\cellalign{cr} %align inside \makecell

\newcolumntype{N}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{O}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  #1\ignorespaces
} %to make entire row boldface/color with one command instead of one at a time


\usepackage{threeparttable}
\usepackage{showframe}\renewcommand*\ShowFrameColor{\color{red}}

\begin{document}

\begin{table}[!htbp]
\centering
\begin{threeparttable}
    \caption{Title for your table goes here}
    \begin{tabular}{@{} Nc|Oc|Oc|Oc|Oc @{}}
        \toprule
        \rowstyle{\bfseries\cellcolor{cyan!70}}
        {Parabola} & {Curve} & {Focus} & {Directrix} & {Vertex}\\
        \midrule
        $x^2 = 4py$ & \makecell{up if $p > 0$ \\ down if $p < 0$} & $F(0, p)$ & $y = -p$ & $V(0, 0)$\\
        \cline{1-5}%horisontal line
        $y^2 = 4px$ & \makecell{right if $p > 0$\\ left if $p < 0$} & $F(p, 0)$ & $x = -p$ & $V(0, 0)$\\
        \bottomrule
        \end{tabular}
    \label{table: label for table}
\end{threeparttable}
\end{table}
        
\end{document}

答案2

\ttabbox使用来自的命令可能(部分)解决此问题floatrow。对于垂直间距,我将来自的规则替换booktabs为来自的粗规则命令makecell,这些命令不会添加任何垂直间距:

\documentclass[12pt]{article}
\usepackage[labelsep=period, tableposition=top, labelfont=bf]{caption}
\usepackage[table]{xcolor} %for cell colour
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{tabu} %for \cellcolor to work inside \rowstyle
\usepackage{siunitx} %for the case when the table had lots of numbers to align (not used here)

\makeatletter\usepackage{microtype}\g@addto@macro\@verbatim{\microtypesetup{activate=false}}\makeatother%
\usepackage{floatrow, caption}
\renewcommand\cellalign{cr} %align inside \makecell

\newcolumntype{N}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{O}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
  #1\ignorespaces
} %to make entire row boldface/color with one command instead of one at a time
\usepackage{lipsum}

\begin{document}

\lipsum[11]
\begin{table}[!htbp]
\captionsetup{singlelinecheck=false}\setlength{\extrarowheight}{2ex}
    \centering
    \ttabbox{\begin{tabular}{@{} Nc|Oc|Oc|Oc|Oc @{}}
        \Xhline{0.08em}
        \rowstyle{\bfseries\cellcolor{cyan!70}}
        {Parabola} & {Curve} & {Focus} & {Directrix} & {Vertex}\\[1.5ex]
        \Xhline{0.05em}
        $x² = 4py$ & \makecell{up if $p > 0$ \\ down if $p < 0$} & $F(0, p)$ & $y = -p$ & $V(0, 0)$\\[1.5ex]
        \cline{1-5}%horizontal line
        $y² = 4px$ & \makecell{right if $p > 0$\\ left if $p < 0$} & $F(p, 0)$ & $x = -p$ & $V(0, 0)$\\[1.5ex]
        \Xhline{0.08em}
        \end{tabular}}{%
    \caption{A very very long title for your table goes here and nowhere else. }
    \label{table: label for table}}
\end{table}

\end{document} 

在此处输入图片描述

相关内容