The Centering Declaration: An Easier Way To Center Latex Floats

Centering Floats in LaTeX

Aligning figures, tables, and other floating elements is a common challenge when typesetting documents with LaTeX. While LaTeX handles text flow and positioning automatically, floats require special care to place them precisely where intended. Centering floats consistently can be surprisingly tricky without the right techniques.

The Struggle to Align Figures and Tables

LaTeX gives authors tools to indicate a float's desired location, like [ht] for "here or top" and [b] for "bottom of page." However, the latex compiler ultimately chooses float positions based on optimizing page composition. This can lead to off-center or uneven float alignment.

Manual spacing adjustments using vspace can nudge floats into position, but this approach is tedious and fragile. Overfull or underfull pages can easily throw carefully tuned spacing awry, requiring manual tweaking during each compile cycle.

A better solution is to tell LaTeX to center floats within their detected positions. The \centering declaration coupled with LaTeX's powerful float placement specifiers provides an easier way to align floats consistently.

Using the center Environment

LaTeX's center environment is one approach to centering float content. This method surrounds the float body with centering tags:

\begin{center}
    \includegraphics{chart}
\end{center}

However, the center tags alone center the float contents but not the entire float box and caption. This causes the float to remain left-aligned on the page, with uneven spacing around the slightly off-center contents. Additionally, center does not work for tabular floats like tables.

Introducing the \centering Declaration

A better centering method is the \centering command. This LaTeX declaration is placed before the float contents, indicating that the entire float should be centered within its final resting position on the page.

What is the \centering declaration?

The \centering command is a LaTeX instruction that tells the typesetting engine to center the following element. On its own, \centering centers the very next block-level item.

When used before a float object like \includegraphics or \begin{tabular}, \centering centers the entire float contents as well as the caption.

How to use \centering for figures and tables

Using \centering is as simple as placing it on the line before the float:

\begin{figure}[ht]
    \centering
    \includegraphics[width=\linewidth]{chart}
    \caption{Centered Figure} 
    \label{fig:chart}
\end{figure}

This centers both the image and caption for a consistently aligned figure. The same method works for tables:

\begin{table}[b]
    \centering
    \begin{tabular}{ |c|c| } 
        \hline
        Cell 1 & Cell 2\\ 
        \hline
    \end{tabular}
    \caption{Centered Table}
    \label{tab:table1}
\end{table}

Adding \centering centers the entire table contents along with caption for perfect alignment.

Example codes demonstrating \centering

Here are complete code examples for centered figures and tables:

Figure:

\begin{figure}[ht]
    \centering
    \includegraphics[width=.8\linewidth]{fig1}
    \caption{Rate of enzymatic reaction under varied pH}
    \label{fig:enzyme-pH}
\end{figure}

Table:

\begin{table}[hb]
    \centering
    \begin{tabular}{|c c c c|}
        \hline
        Temperature (°C) & Pressure (Pa) & Volume (L) & Mass (g) \\ [0.5ex] 
        \hline
        20  & 101325 & 1.00 & 100.5 \\ 
        80 & 182340 & 1.47 & 89.2\\ 
        \hline
    \end{tabular}
    \caption {Effects of temperature on gas properties}
    \label{tab:gas-data}
\end{table}

Positioning Floats with \centering

LaTeX's float placement specifiers give authors powerful control to align figures and tables. \centering combines with these tools for centered floats placed precisely where needed.

Using \centering alongside position specifiers

Float placement indicators like [ht] for "here or top" work together with \centering. The compiler first considers the requested position, then \centering centers the float in that location.

This allows both vertical position and centered horizontal alignment to be defined for figures, tables, and custom float types.

Example code for centering a top-aligned figure

For a figure placed at the top of the page, [t] does vertical placement while \centering centers it horizontally:

\begin{figure}[t]
    \centering
    \includegraphics[width=.8\linewidth]{fig2}
    \caption{Effect of humidity on fracture strength}
    \label{fig:humidity-fracture}
\end{figure}

Example code for centering a bottom-aligned table

Similarly, a bottom-placed table combines [b] and \centering:

\begin{table}[b]
    \centering
    \begin{tabular}{|l l l|}
        \hline
        Sample & Result 1 & Result 2\\
        \hline
        1 & 0.53 & 0.61 \\
        2 & 0.48 & 0.58 \\
        3 & 0.50 & 0.63 \\
        \hline
    \end{tabular}
    \caption{Table caption}
    \label{tab:my-label}
\end{table}

This reliably positions the table bottom while keeping the contents neatly centered.

Achieving Consistent Centered Floats

Instead of manually adding \centering to each float, LaTeX also allows setting document-wide defaults to center figures and tables consistently.

Setting default centering behavior

The \floatplacement command globally sets float positioning and alignment rules for the whole document or within a scoped group:

\floatplacement{figure}{H}
\floatplacement{table}{b}

The first argument sets the float type, while the second defines the location. Capital "H" is a predefined LaTeX marker indicating "Here" for float placement along with centered alignment.

Globally applying centering to all floats

A single \floatplacement call at the start of a LaTeX document is an easy way to centralize centering behavior:

\floatplacement{figure}{H} 
\floatplacement{table}{H}

Now figures and tables default to \centering without having to manually declare it repeatedly. Authors can still override on individual floats by adding [t], [b] etc.

Example global configuration for centered floats

\documentclass{article}

\floatplacement{figure}{H}
\floatplacement{table}{H}

\begin{document}

\begin{figure}
   \includegraphics[width=\linewidth]{fig1}
   \caption{Default centered figure}   
\end{figure}

\begin{table}
    \begin{tabular}{cc}
         Cell 1 & Cell 2 \\
    \end{tabular}
    \caption{Default centered table} 
\end{table}

\end{document}

Both floats are neatly centered without needing \centering declarations.

Conclusion and Best Practices

\centering and \floatplacement enable consistent, precisely aligned centered floats in LaTeX. By combining position specifiers with the \centering command, figures, tables, and custom floats can be placed exactly where needed with clean horizontal alignment.

Summary of key points on centering floats

  • LaTeX's \centering declaration centers entire floats plus captions
  • Works for figures, tables, and custom float types
  • Combine with positional specifiers like [ht] for precision placement
  • \floatplacement globally sets default centered alignment

Recommendations for effective use of \centering

  • Add \centering before each float for one-off centering
  • Or set document-level defaults with \floatplacement
  • Use alongside [t], [b] etc. to simultaneously define position and alignment
  • Check compiled PDF carefully to catch any anomalous drift from true center

Using \centering appropriately helps authors save time, reduce formatting inconsistencies, and create professionally typeset, centered float layouts. Aligning figures and tables precisely has never been easier.

Leave a Reply

Your email address will not be published. Required fields are marked *