2

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:

plot

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?

Thanks in advance!

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.

6
  • 4
    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
    – percusse
    May 3, 2018 at 13:24
  • 1
    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.
    – daleif
    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.
    – Andreas
    May 3, 2018 at 13:52
  • 1
    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.
    – daleif
    May 3, 2018 at 14:03
  • 1
    @daleif: I am sorry, you are right. They will of course be distorted.
    – Andreas
    May 3, 2018 at 14:49

1 Answer 1

3

Your MWE does the following:

  • 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.

What you should do instead:

  • 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}

enter image description here

Some more comments:

  • 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.
2
  • Thank you very much for your detailed explanation. It solves it perfectly. To be honest, I tried to recycle some older code, where I had four images on one page. I see that their width (according to my old code) was smaller than 1, hence the reason why there were centered but the two larger images were not.
    – Andreas
    May 3, 2018 at 13:51
  • i wouldn't use height option. with determining only image width the original image aspect ratio is preserved.
    – Zarko
    May 3, 2018 at 14:58

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.