# Large images not centering

I have this code:

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[margin=2.5cm, showframe]{geometry}
\usepackage{pgfplots}
\usepackage{graphicx}
\usepackage[skip=0.33\baselineskip]{caption}
\captionsetup[figure]{
font=bf,
size=normalsize,
justification=centerlast,
}
\usepackage[labelformat=simple]{subcaption}
\captionsetup[subfigure]{
font=normal,
size=normalsize,
justification=centerlast,
}
\renewcommand*{\thesubfigure}{Panel \thefigure\Alph{subfigure}:}

\newcommand{\plotheight}{0.425}
\newcommand{\plotwidth}{1.45}

\begin{document}
\begin{figure}[ht]
\centering
\begin{subfigure}{0.5\textwidth}
\centering
\includegraphics[width=\plotwidth\linewidth, height=\plotheight\textheight]{monkey.jpg}
\caption{Monkey 1}
\end{subfigure}\\
\centering
\begin{subfigure}{0.5\textwidth}
\centering
\includegraphics[width=\plotwidth\linewidth, height=\plotheight\textheight]{monkey.jpg}
\caption{Monkey 2}
\end{subfigure}
\vspace{2ex}
\caption{Monkey Plot}
\parbox{\linewidth}{\small{This figure contains an image of the same monkey twice.}\vspace{2ex}}
\end{figure}
\end{document}


which produces this output:

When the width of the images are less than or equal to 1, both images are centred. However, when I insert a width value larger than 1, then the images are skewed to the right, as shown in the picture.

I do get a warning from LaTeX that the images are too wide. Although, the images seems to fit perfectly fine if centred.

Does anyone have an idea what the problem is?

Edit: the real images I am using in my report are vector based, hence they will not be distorted. But thanks to those pointing that out.

• They are centered but they are larger than the text width hence they leak out to the right margin creating the illusion The problem is that your container subfigure is wasting most of the whitespace with 0.5\linewidth and images don't fit into this container May 3, 2018 at 13:24
• Why are you using \plotwidth\linewidth in the first place, where \plotwidth is larger than 1? Also the way you are setting height and width may distort the image, you might want to only set one of either height or width, or also add keepaspectratio. May 3, 2018 at 13:37
• @percusse: Thank you for your answer. I haven't been using the subfigure environment for too long, so I was unaware of where the problem was. May 3, 2018 at 13:52
• It will be distorted! Consider an image of a circle, scaled to a width of 5cm and a height of 3cm. That image is no longer a circle. May 3, 2018 at 14:03
• @daleif: I am sorry, you are right. They will of course be distorted. May 3, 2018 at 14:49

• The subfigures are set to the fixed width of 0.5\textwidth.
• Inside the subfigures, the included images are set to \plotwidth\linewidth.

Inside the subfigures, \linewidth equals 0.5\textwidth. If you now set \plotwidth to a factor larger than one, the images will get a width that is larger than that of the subfigure. That's why you get the warning, and also centering doesn't work for overflowing stuff.

• Set the subfigures to a width \plotwidth\textwidth where for \plotwidth you can choose (and easily adjust) any number between 0 and 1.
• Inside the subfigures, set the width of the included images to the fixed \linewidth.

This way, you have the flexibility you wanted to have with your construction, but there will never be an overflow of the subfigure.

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[margin=2.5cm, showframe]{geometry}
\usepackage{pgfplots}
\usepackage{graphicx}
\usepackage[skip=0.33\baselineskip]{caption}
\captionsetup[figure]{
font=bf,
size=normalsize,
justification=centerlast,
}
\usepackage[labelformat=simple]{subcaption}
\captionsetup[subfigure]{
font=normal,
size=normalsize,
justification=centerlast,
}
\renewcommand*{\thesubfigure}{Panel \thefigure\Alph{subfigure}:}

\newcommand{\plotheight}{0.425}
\newcommand{\plotwidth}{0.7}

\begin{document}
\begin{figure}[ht]
\centering
\begin{subfigure}{\plotwidth\textwidth}
\centering
\includegraphics[width=\linewidth, height=\plotheight\textheight]{example-image-a}
\caption{Monkey 1}
\end{subfigure}\\
\begin{subfigure}{\plotwidth\textwidth}
\centering
\includegraphics[width=\linewidth, height=\plotheight\textheight]{example-image-b}
\caption{Monkey 2}
\end{subfigure}
\vspace{2ex}
\caption{Monkey Plot}
\end{figure}
\end{document}


• You might want to change the use of \plotheight in a similar way, to have a consistent scheme for both dimensions.
• You might want to reconsider setting both width and height separately, or at least to add the keepaspectratio option.
• i wouldn't use height option. with determining only image width the original image aspect ratio is preserved. May 3, 2018 at 14:58