20

In a document I'm writing, I'm using the familiar [hbt] option for my floating tables, and I center them. I'd like to avoid repeating that every time I'm creating a table. I assume it's easy to add something in my preamble that makes sure that these two options are used every time I begin a new table environment?

\documentclass{article}
\begin{document}
\begin{table}[hbt]
\centering
\begin{tabular}{*3{l}}
1 & 2 & 3\\
a & b & c\\
\end{tabular}
\end{table}
\end{document}
22

the default figure placement can be set with

\makeatletter
\renewcommand\fps@figure{htbp}
\makeatletter

(It's almost always a bad idea not to include p in the default)

For centering you can add it to \@floatboxreset

\makeatletter
\def \@floatboxreset {%
        \reset@font
        \normalsize
        \@setminipage
\centering%<<<<<<<<<<<<<<<<<<<
}
\makeatletter
  • This was a lot more complicated than I had anticipated. Is there no simpler way of saying "When you initiate the environment {table}, automatically add [hbt] \centering"? EDIT: I think that's exactly what Ethan Bolker did in his answer. – Sverre Mar 26 '14 at 18:48
  • It couldn't be a lot less complicated could it? There is a single command that sets up formatting in floats and this adds \centering to it. It's just a direct implementation of the question? – David Carlisle Mar 26 '14 at 18:51
  • @Sverre well the other answer (while perfectly usable) just wraps the table in a custom environment (beware of white space after it though:-) this one does more exact;y what you asked, which is to set the default placement and formatting for floats (which is normally set by the class) – David Carlisle Mar 26 '14 at 18:53
  • By "complicated" I meant that the code is really hard for someone like me to understand and penetrate, since I have never seen commands such as \fps and \@floatboxreset before, and I'm sure I wouldn't be able to find out what they mean either. – Sverre Mar 26 '14 at 18:53
  • @Sverre or Werner's answer which actually sets exactly the same commands as here, but using custom commands from float package, rather than lower level command definitions. (Probably you should accept that one:-) – David Carlisle Mar 26 '14 at 18:55
19

The float package provides an interface to specify the float placement for a specific float using

\floatplacement{<type>}{<spec>}

The \floatplacement command resets the default placement specifier of a class of floats. So, one could use

\floatplacement{table}{hbt}

to achieve what you're after.

  • Can I use the same package to center all floats? – Sverre Mar 26 '14 at 18:57
11

Try

\documentclass{article}

\newenvironment{mytable}
{\begin{table}[hbt]
  \centering}
{\end{table}}

\begin{document}

\begin{mytable}
\begin{tabular}{*3{l}}
1 & 2 & 3\\
a & b & c\\
\end{tabular}
\end{mytable}
\end{document}

\end{document}
6

Combining Werner's answer here with lockstep's answer to How can I automatically center an image?, we can do it like this:

\documentclass{article}
\usepackage{floatrow} % this automatically centers all floats
    \floatplacement{table}{hbtp} % all tables are given the [hbtp] option
\begin{document}
\begin{table}
\begin{tabular}{*3{l}}
1 & 2 & 3\\
a & b & c\\
\end{tabular}
\end{table}
\end{document}
  • 1
    According to the documentation for floatrow it's an extension of float borrowing code from it, and you shouldn't load float as well. – pst Mar 26 '14 at 19:44
  • 1
    @HeikoOberdiek. floatrow doesn't load float but includes a modification of it, and its documentation says that you must not load floatwith it. It is a good idea to follow that. – pst Mar 27 '14 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.