0

I was trying to recreate a table from a question with some modifications (irrelevant to the substance in this question). The table which I could build looks like this:

Image of table

There are still several issues with this table, such as

  1. the vertical spacing between cells,
  2. the color overflowing the sides and not filling the entire cells.

But in this question, I would like to focus only on the table heading, or caption. I would like to

  • align the caption so that it starts where the table starts;
  • table should remain in the center of page;
  • I would not like to stretch the table heading or caption (so some whitespace should remain in this case to the right of the heading.

Other similar questions generally align both the caption and and to left, that would not work here.

Any ideas of what I should add or modify? Minimally invasive procedures are preferred.


\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}
4
  • I'd recommend to not use the tabu package as it currently is unmaintained and you might experience some unintended side effects. Also, please kaap in mind, that the horizonal lines from the booktabs package were designed to be used without vertical lines.
    – leandriis
    Apr 24, 2021 at 14:43
  • @leandriis Hmm. I compiled again (this time without tabu) and nothing changed, no errors popped up. This is strange; before I could not compile without it. I'll try to avoid it per your suggestion, thank you. I am not sure what you mean by 'horizonal lines from the booktabs package were designed to be used without vertical lines'. Do you mean that I should use them only in tables which do not have any vertical lines? Apr 24, 2021 at 14:55
  • 1
    The booktabs package documentation explicitly states: "More importantly the rules generated by the new commands are in no way guaranteed to connect with verticals generated by {|} characters in the preamble.This is a feature (see above). You should not use vertical rules in tables, end of story." So yes, use horizontal lines from the booktabs package only in tables that don't use vertical lines. (For most tables you don't really need vertical lines anyways.)
    – leandriis
    Apr 24, 2021 at 15:04
  • @leandriis That's interesting to learn. I guess it seemed a bit weird that the verticals didn't connect with the horisontals at first but I got used to it; seems natural now. I am not quite yet ready to give up vertical lines entirely... But your point is well taken. Apr 24, 2021 at 15:11

2 Answers 2

1

With the help of the threeparttable package and caption's singlelinecheck=false option. (Red lines in the following image indicate the margins and I did not address the various otehr issues with this table.)

enter image description here

\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}
6
  • Lovely, Thank you. Yes, it is perfectly okay that you did not address the other issues. I'd prefer to address them in separate questions, more focus this way. Thank you for also introducing me to showframe, that could be useful in the future. I will likely accept your answer in some 24 to 48 h. Apr 24, 2021 at 14:52
  • Follow-up: if the table gets big and overflows over page side margins, is there a way to center the table then with this solution? (Table heading should, in this case, be simply at the left margin for text. I checked that this is the case at the moment which is very good an should stay like this for overflow case). Apr 24, 2021 at 15:45
  • The approach presented here tex.stackexchange.com/a/39436/134144 should help you to horizontally center an overly wide table on a page, while left aligning the caption text inside of the text block. Personally, I wodul however try to reduce the overall width of the table in order to make sure it fits into the available space instead of leetting it overflow into both margins.
    – leandriis
    Apr 24, 2021 at 15:52
  • Thanks. That link is actually why I initially chose to build tables with \makebox (as in OP). With threeparttable there as well, I am not sure where to add it to make it work. The two most natural places (before \begin{tabular} (as in OP) or before \begin{threeparttable}) give various error messages upon compilation. The adjustbox option does not work because the table environment is in play here as well. Apr 24, 2021 at 16:04
  • For overly wide tables, remove the threeparttable environment and place the adjustbox environment around tabular but inside table.
    – leandriis
    Apr 24, 2021 at 16:06
1

A possible (partial) solution with the \ttabbox command from floatrow. For the vertical spacing, I replaced the rules from booktabs with thick rules commands from makecell, which do not add any vertical spacing:

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

enter image description here

5
  • Neat, thank you very much! It does give an (ignorable) error message: Command \texttwosuperior invalid in math mode line 36. By the way, do the horisontal lines from makecell take arguments to have them cover only certain cells (similar to cline[1-3] for instance)? Apr 24, 2021 at 15:36
  • Follow-up: if the table gets big and overflows over page side margins, is there a way to center the table then with this solution? (Table heading should, in this case, be simply at the left margin for text. I checked that this is the case at the moment which is very good and should stay like this for overflow case). Apr 24, 2021 at 15:45
  • 1
    There is also an \Xcline command. See p.21 in the documentation.
    – Bernard
    Apr 24, 2021 at 15:47
  • As to the follow-up, you should avoid overflowing tables as much as possible (playing with the fontsize and the value ot \tabcolsep, not with scaling the table). If it is not enough, you can use the adjustwidth environment from changepage to locally change the text width.
    – Bernard
    Apr 24, 2021 at 15:54
  • Thank you for the reference. I am also happy to see that CTAN documentation is now available from HTTPS (I have HTTPS-only) :). Your recommendation to try to keep tables in a "non-overflowing state" is understood. Thus far I have had one or two brushes with tables that better convey information in the "overflowing state", so not very often. Apr 24, 2021 at 16:14

You must log in to answer this question.

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