Table and longtable centering and caption alignment

I want to format table and longtable in a special way:

1) table/longtable should be positioned to the center of the page

2) table/longtable caption should be positioned between left and right table/longtable width; also it should be aligned to the left edge of object.

Using \centering command in \table environment satisfies first requirement (longtable is centered by default)

I found a way to satisfy second requirement for table - using \usepackage{floatrow} and reformat all tables with \ttabbox{}{}. But there are 2 downsides:

• I don't know how to use it with longtable

• \usepackage{floatrow} makes longtables to be positioned at left side of page

Minimal example provided.

\documentclass[12pt, a4paper, final, oneside, titlepage]{article}
\usepackage{caption}
\usepackage{longtable}
%\usepackage{float}
%\usepackage{floatrow}

\begin{document}
Table should be positioned at the center of the page, and caption should be positioned between left and right edge of table (aligned to left edge)

\begin{table}[h!]
\centering
\caption{Very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long caption}
\begin{tabular}{|c|c|p{8cm}|}
\hline
a & b & c \\ \hline
1 & 2 & 3 \\ \hline
\end{tabular}
\end{table}

Longtable  should positioned at the center of the page, and caption should be positioned between left and right edge of table (aligned to left edge)

\begin{longtable}[h!]{|p{5cm}|p{3cm}|}

\caption{Very,  very, very, very, very, very, very, very  very, very, very, very, very, very, very long caption} \\

\hline
a & b \\ \hline

\multicolumn{2}{l}{Short caption} \\
\hline
a & b \\ \hline

Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
Some string & c \\ \hline
\end{longtable}
\end{document}

• If your real case is as above, where the widths are known in advance |p{5cm}|p{3cm}| then it is easy you can set \LTcapwidth to 6cm+3\arrayrulewidth+2\tabcolsep (or 4\tabcolsep depending on your definition of width of the table) if your table has dynamically sized l c r columns, you need a two pass solution picking up the width from the last run (for which I think there is an answer on site, I'll look...) – David Carlisle Oct 20 '14 at 10:33
• looking at the source floatrow only changes the longtable alignment of left or right alignments of floats generally are requested: \DeclareMarginSet{raggedleft}{\setfloatmargins{\hfil}{}% \LTleft=\fill \LTright=\z@} – David Carlisle Oct 20 '14 at 10:45
• For the width of a longtable, I don't seem to have an answer here but see Heiko's code here (I didn't test it but I have faith in his code:-) compgroups.net/comp.text.tex/longtable-tablewidth/1922986 – David Carlisle Oct 20 '14 at 10:48
• Heiko's code gives behaviour for longtables exactly as i wanted! Is there similar solution for tables? Cause reformatting all tables with \ttabbox{}{} is a huge pain. Also \usepackage{floatrow} still makes longtables to be positioned at left side of page – kostr22 Oct 20 '14 at 11:11
• Use threeparttable for tables and Heiko's code (link given in David's comment above) for longtable. \begin{table}[h!] \centering \begin{threeparttable} \caption{Very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long caption} \begin{tabular}{|c|c|p{8cm}|} \hline a & b & c \\ \hline 1 & 2 & 3 \\ \hline \end{tabular} \end{threeparttable} \end{table}. – user11232 Oct 20 '14 at 11:17