4

Consider the MWE below. If I \usepackage{floatrow} the longtable is not horizontal centered. If I comment out \usepackage{floatrow} the table is properly horizontal centered.

How can I get this snippet working (both use floatrow and center the longtable)?

Credits: Landscape construction (stripped down by me for MWE): Steven B. Segletes in this answer.

Without usepackage floatrow in below MWE (correct):

enter image description here

With usepackage floatrow in below MWE (not centered anymore):

enter image description here

MWE:

\documentclass{article}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{pdflscape}
\usepackage{afterpage}
\usepackage{setspace}
\usepackage[a4paper]{geometry}
% >> 'PROBLEM' package
\usepackage{floatrow}
% << 'PROBLEM' package
\begin{document}
Document is portrait
\afterpage{%
    \clearpage% Flush earlier floats (otherwise order might not be correct)
        \newgeometry{left=1cm, right=1cm,top=1cm,bottom=1cm}
    \begin{landscape}% Landscape page
        \pagestyle{empty}
        \centering % Center table
        \vspace{0pt}
        \begin{singlespacing}
\begin{longtable}{rr}
 \caption{Table is landscape.}
 \tabularnewline
 \textbf{Col1} & \textbf{Col2}\tabularnewline
 \midrule
 \endfirsthead
 \caption{Caption (Continued)}\tabularnewline
 \textbf{Col1} & \textbf{Col2}\tabularnewline
\midrule
 \endhead
 \bottomrule
 \multicolumn{2}{r}{Cont}
 \endfoot
 \bottomrule
 \endlastfoot
 A & B \tabularnewline
 C & D \tabularnewline
\end{longtable}
    \end{singlespacing}
    \end{landscape}
    \clearpage% Flush page
}
\restoregeometry
\end{document}
robert
  • 1,285
  • \centering never has any affect on longtable. – David Carlisle Jul 22 '16 at 19:17
  • Well, if you run MWE with usepackage{floatrow} commented out, you will see that it works. So if you say that it is not work, can you clarify why it nevertheless works? E.g. is it by default centering? – robert Jul 22 '16 at 19:18
  • longtables are centred by default unless you use [l] or [r] but \centering, \flushright don't do anything as they are always full width. – David Carlisle Jul 22 '16 at 19:20
  • So then the question is: why is this default behavior broken after importing floatrow, and how to fix it? – robert Jul 22 '16 at 19:22

2 Answers2

6

For some strange reason floatrow sets \LTleft and \LTright to 0pt breaking longtable's default positioning, you could set them back, or simpler just use

\begin{longtable}[c]{rr}

to force longtable to centre.


this is sort of mentioned in the floatrow manual

Alignment Settings for longtable. The floatrow expands some settings of table lay- out to the longtable environment, so you may set \LTleft and \LTright parameters inside \DeclareMarginSet settings. For example, centering option was defined like: \DeclareMarginSet{centering}{\setfloatmargins{\hfill}{\hfill}% \LTleft=\fill \LTright=\fill}

David Carlisle
  • 757,742
1

this may be the solution:


header-includes:
 - \usepackage{floatrow}
 - \floatsetup[longtable]{margins=centering}

UPDATE

Package "float" is used with kable and kableExtra.

When we need to place the caption of a figure at the top the only solution I have found is to use the "floatrow" package.

Since "float" and "floatrow" are incompatible, it is necessary to add some additional configurations in "floatrow" so that the appearance of the tables generated with kable and kableExtra using rstudio, rmarkdown and knitr is identical.


header-includes:
 - \usepackage{floatrow}
 - \floatsetup[figure]{capposition=top} % Caption figure top.
 - \floatsetup[table]{capposition=top} % Caption table top.
 - \floatplacement{figure}{H} % Figure position according to the code flow.
 - \floatplacement{table}{H} % Table position according to the code flow.
 - \floatsetup[longtable]{margins=centering} % Longtable tables centered.