[Tex/LaTex] How to center all tables in a document

floatshorizontal alignmentlyxtables

Background

Would like to center all tables within floats, without having to add \centering to each table.

Ideas

Tried the following:

\let\originaltabular\tabular
\renewcommand\tabular{\centering\originaltabular}

It compiles, but the tables remain left-justified.

Question

How would you edit the Preamble of a LyX document to make all the floating tables be centred?

Best Answer

If you would like to center all tabular environments, I would recommend to create your own environment with a different name, which calls tabular. You could use that instead of redefining tabular itself.

But no problem, this can be done. Just use \renewenvironment, for example:

\let\originaltabular\tabular
\let\endoriginaltabular\endtabular
\renewenvironment{tabular}[1]{%
  \begingroup%
  \centering%
  \originaltabular{#1}}%
  {\endoriginaltabular\endgroup}

This as addition to your first try redefining tabular.

You could redefine the floating table environment very similar:

\let\originaltable\table
\let\endoriginaltable\endtable
\renewenvironment{table}[1][ht]{%
  \originaltable[#1]
  \centering}%
  {\endoriginaltable}

You could also use etoolbox to patch the table environment or append \centering to the definition this way:

\makeatletter
\g@addto@macro{\table}{\centering}
\makeatother

or

\expandafter\def\expandafter\table\expandafter{\table\centering}