[Tex/LaTex] Centering figure on page

graphicssubfloats

I am new to Latex and trying to center two figures using the following script:

\begin{figure}

\begin{center}
\subfloat{
\includegraphics[scale = 0.07,trim={40 5 25 10},clip]{Constantloadwithrigidboundary_Uy_X0Y0}}
\subfloat{
\includegraphics[scale = 0.07,trim={40 5 25 10},clip]{Constantloadwithrigidboundary_Vy_X0Y0}}
\end{center}
\end{figure}

As explained near the end of this video. Unfortunately in my case the figures do not center but rather are skewed to the right of the page as shown below:enter image description here

I am also including the following:

\documentclass{article}
\usepackage[margin=1.2in]{geometry}
\usepackage{graphicx}
\usepackage{subfig}

At the beginning of my script. Why does this happen? How can I get my subfloats to center properly on the page?

Best Answer

The problem is one of overflowing the allocated text width, as shown in the first figure set in the MWE below (where the showframe option of the geometry package shows explicitly the text area allocated by the current document settings). TeX honors the left margin, but if the figure-set is too large, it spills out the right.

There are three fixes:

  1. You can reduce the width of the figure-set to fit inside the margins allocated; this is the route taken in the 2nd figure-set on the 1st page.

  2. You can trick LaTeX into momentarily ignoring the margins, as I do on page 2 of the MWE. With \makebox[0pt]{\begin{minipage}{1.2\textwidth}...\end{minipage}}, I create a minipage large enough to handle the oversized figure-set, but then place it in a zero-width \makebox subject to centering. The \makebox thus makes TeX unaware that the minipage exceeds the margin bounds. Note that I also use an \hfill to employ the full width of the minipage, lest the minipage be centered, but not its content. Or, finally,

  3. You can set a \newgeometry expanding the margins, possibly for a single page only (shown on pages 3,4 in MWE below).

The MWE.

\documentclass{article}
\usepackage[pass,showframe]{geometry}
\usepackage{subfig,graphicx}
\begin{document}
\begin{figure}[ht]
\begin{center}
\subfloat{
\includegraphics[scale = 0.45]{example-image-A}}
\subfloat{
\includegraphics[scale = 0.45]{example-image-B}}
\end{center}
\end{figure}
\begin{figure}[ht]
\begin{center}
\subfloat{
\includegraphics[scale = 0.35]{example-image-A}}
\subfloat{
\includegraphics[scale = 0.35]{example-image-B}}
\end{center}
\end{figure}
\begin{figure}[ht]
\begin{center}
\makebox[0pt]{\begin{minipage}{1.2\textwidth}%
\subfloat{%
\includegraphics[scale = 0.5]{example-image-A}}%
\hfill%
\subfloat{%
\includegraphics[scale = 0.5]{example-image-B}}%
\end{minipage}}
\end{center}
\end{figure}
\savegeometry{origigeom}
\clearpage
\newgeometry{margin=2cm}
\begin{figure}[ht]
\begin{center}
\subfloat{%
\includegraphics[scale = 0.5]{example-image-A}}%
\subfloat{%
\includegraphics[scale = 0.5]{example-image-B}}%
\end{center}
\end{figure}
\clearpage
\restoregeometry
Hi, Mom!
\end{document}

enter image description here

enter image description here