# Figure centering problem

I have following codes:

\begin{figure}[h]
\begin{subfigure}{0.3\linewidth}
\centering
\includegraphics[width=\textwidth, height=4 cm]{boat_gray_256.png}
\centering
\caption*{Source: Public-Domain Test Images for Homeworks and Projects \cite{data_bb}}
\caption{Boat Image}
\label{fig:4_2_1}
\centering
\end{subfigure}
\begin{subfigure}{0.3\linewidth}
\centering
\includegraphics[width=\textwidth, height=4cm]{cameraman_gray_256.png}
\centering
\caption*{Source: Image Databases, Standard   \\ Test Images \cite{data_lwpcmphlwj}}
\caption{Cameraman Image}
\label{fig:4_2_2}
\centering
\end{subfigure}
\newline
\begin{subfigure}{0.3\linewidth}
\centering
\includegraphics[width=\textwidth, height=4 cm]{lena_gray_256.png}
\centering
\caption*{Source: Image Databases, Standard Test Images \cite{data_lwpcmphlwj}}
\caption{Lena Image}
\label{fig:4_2_3}
\centering

\end{subfigure}
\newline
%\centering
\begin{subfigure}{0.3\linewidth}
\centering
\includegraphics[width=\textwidth, height=4cm]{jetplane_gray_256.png}
\centering
\caption*{Source: Image Databases, Standard   \\ Test Images \cite{data_lwpcmphlwj}}
\caption{Jet Plane Image}
\label{fig:4_2_4}
\centering
\end{subfigure}
\begin{subfigure}{0.3\linewidth}
\centering
\includegraphics[width=\textwidth, height=4cm]{mri_gray_256.jpg}
\centering
\caption*{Source: Northern Arizona University, Gray Scale Images \cite{data_mri}}
\caption{MRI Image}
\label{fig:4_2_5}
\centering
\end{subfigure}
\caption{Used Images}
\label{fig:datasetimages}
\end{figure}


The problem is that 3. image, Lena, does not stay on the center it's vertically in a line with boat and jet plane. How can center the lena?

• What exactly is the layout you want to achieve? From your question, I conclude that you want images a and b in the first line, c in the second line (horizontally centered) and imaged d and e in a third line. From your example code, one could conclude that you want three images in a row (0.3\linewidth). – leandriis Apr 29 '18 at 13:53
• Exatcly, I am expecting your first guess. – user8151324 Apr 29 '18 at 17:01

Maybe you are satisfied with the following example:

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\begin{document}

\begin{figure}[h]
\centering
\begin{subfigure}[t]{0.3\linewidth}
\includegraphics[width=\textwidth, height=4 cm]{example-image}
\caption*{Source: Public-Domain Test Images for Homeworks and Projects \cite{data_bb}}
\caption{Boat Image}
\label{fig:4_2_1}
\begin{subfigure}[t]{0.3\linewidth}
\includegraphics[width=\textwidth, height=4cm]{example-image}
\caption*{Source: Image Databases, Standard  Test Images \cite{data_lwpcmphlwj}}
\caption{Cameraman Image}
\label{fig:4_2_2}
\end{subfigure}

\begin{subfigure}[t]{0.3\linewidth}
\includegraphics[width=\textwidth, height=4 cm]{example-image}
\caption*{Source: Image Databases, Standard Test Images \cite{data_lwpcmphlwj}}
\caption{Lena Image}
\label{fig:4_2_3}
\end{subfigure}

\begin{subfigure}[t]{0.3\linewidth}
\includegraphics[width=\textwidth, height=4cm]{example-image}
\caption*{Source: Image Databases, Standard   Test Images \cite{data_lwpcmphlwj}}
\caption{Jet Plane Image}
\label{fig:4_2_4}
\begin{subfigure}[t]{0.3\linewidth}
\includegraphics[width=\textwidth, height=4cm]{example-image}
\caption*{Source: Northern Arizona University, Gray Scale Images \cite{data_mri}}
\caption{MRI Image}
\label{fig:4_2_5}
\end{subfigure}
\caption{Used Images}
\label{fig:datasetimages}
\end{figure}

\end{document}


In comparison to your original code, I have removed all the superfluous \centering commands. One of them in the beginning of the figure environment is enough to achive the above shown layout. To get image c in a separate line, I have used empty lines in the code just before and after the corresponding subfigure environment. In order to get some horizontal space between images a/b and d/e respectivly, I have added \quad between the corresponding subfigure environments.

• What an amazing team! I am really thankfull! All of them work great! – user8151324 Apr 29 '18 at 21:09

see if you will like the following solution:

edit: as suggested Mico in his comment, i add vertical space between lines with images:

- added \centering after begin{figure} - removed all \centering inside of subfigure, they are superfluous - use of \caption*{...} is replaced with copyrightbox with option r, which put copyright material (source of image) on the right side of image

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{subcaption}

\begin{document}
\begin{figure}[h]
\centering
\begin{subfigure}{0.3\linewidth}
{\includegraphics[width=\textwidth,height=4 cm]{boat_gray_256.png}}
{Source: Public-Domain Test Images for Homeworks and Projects \cite{data_bb}}%
\caption{Boat Image}
\label{fig:4_2_1}
\end{subfigure}\hfil
\begin{subfigure}{0.3\linewidth}
{\includegraphics[width=\linewidth,height=4cm]{cameraman_gray_256.png}}
{Source: Image Databases, Standard Test Images \cite{data_lwpcmphlwj}}%
\caption{Cameraman Image}
\label{fig:4_2_2}
\end{subfigure}\\[2ex]
%
\begin{subfigure}{0.3\linewidth}
{\includegraphics[width=\linewidth,height=4 cm]{lena_gray_256.png}}
{Source: Image Databases, Standard Test Images \cite{data_lwpcmphlwj}}%
\caption{Lena Image}
\label{fig:4_2_3}
\end{subfigure}\\[2ex]
%
\begin{subfigure}{0.3\linewidth}
{\includegraphics[width=\linewidth,height=4cm]{jetplane_gray_256.png}}
{Source: Image Databases, Standard Test Images \cite{data_lwpcmphlwj}}%
\caption{Jet Plane Image}
\label{fig:4_2_4}
\end{subfigure}
\begin{subfigure}{0.3\linewidth}
{\includegraphics[width=\linewidth,height=4cm]{mri_gray_256.jpg}}
{Source: Northern Arizona University, Gray Scale Images \cite{data_mri}}%
\caption{MRI Image}
\label{fig:4_2_5}
\end{subfigure}
\caption{Used Images}
\label{fig:datasetimages}
\end{figure}
\end{document}

• Adding a bit of extra vertical space between the rows of subfigure environments might be nice. – Mico Apr 29 '18 at 19:19
• @Mico, I was looked in manual for the package caption, but there is no option for set this by \captionesetup, but something like this not exist. i will stick with \\[<some value>] and correct answer asap. – Zarko Apr 29 '18 at 19:26
• What I tend to in cases such as this one is to insert either \medskip or \bigskip between rows. – Mico Apr 29 '18 at 19:36

Something like the following solution? Note that the middle subfigure is set in its own minipage, whose overall width is the sum of two "regular" subfigures plus the space between them. I've also gotten rid of all \caption* wrappers as they don't seem to do much of anything (useful). The code also takes care to center the entire figure horizontally on the page.

\documentclass{article}
\usepackage[demo]{graphicx} %remove 'demo' option in real document
\usepackage{subcaption} % for 'subfigure' environment
\usepackage{ragged2e}  % for '\Centering' macro
\usepackage[]{geometry}   % set page parameters suitably
\begin{document}
\begin{figure}
\Centering
\begin{subfigure}[t]{0.3\textwidth}
\Centering
\includegraphics[width=\textwidth, height=4cm]%
{boat_gray_256.png}
Source: Public-Domain Test Images for Homeworks
and Projects~\cite{data_bb}

\caption{Boat Image}
\label{fig:4_2_1}
\end{subfigure}\hspace{0.05\textwidth}%
\begin{subfigure}[t]{0.3\textwidth}
\Centering
\includegraphics[width=\textwidth, height=4cm]%
{cameraman_gray_256.png}
Source: Image Databases, Standard\\
Test Images~\cite{data_lwpcmphlwj}

\caption{Cameraman Image}
\label{fig:4_2_2}
\end{subfigure}

\bigskip
\begin{minipage}{0.65\textwidth} % 0.3+0.3+0.05=0.65
\Centering
\begin{subfigure}[t]{0.462\textwidth} % 0.3/0.65=0.462
\Centering
\includegraphics[width=\textwidth, height=4cm]%
{lena_gray_256.png}
Source: Image Databases, Standard Test Images~\cite{data_lwpcmphlwj}

\caption{Lena Image}
\label{fig:4_2_3}
\end{subfigure}
\end{minipage}

\bigskip
\begin{subfigure}[t]{0.3\textwidth}
\Centering
\includegraphics[width=\textwidth, height=4cm]%
{jetplane_gray_256.png}
Source: Image Databases, Standard \\
Test Images \cite{data_lwpcmphlwj}

\caption{Jet Plane Image}
\label{fig:4_2_4}
\end{subfigure}\hspace{0.05\textwidth}%
\begin{subfigure}[t]{0.3\textwidth}
\Centering
\includegraphics[width=\textwidth, height=4cm]%
{mri_gray_256.jpg}
Source: Northern Arizona University, Gray
Scale Images~\cite{data_mri}

\caption{MRI Image}
\label{fig:4_2_5}
\end{subfigure}

\bigskip
\caption{Used Images}
\label{fig:datasetimages}
\end{figure}
\end{document}


I guess that height=4cm is just for the example, otherwise it would probably distort the image. Be careful that PNG images cannot in general be arbitrarily scaled.

This said, you seem to want a star-shaped layout.

Get rid of all your \centering commands, which do nothing useful. In order to typeset sources in the narrow space they have, it's better to use \raggedright; a single outer \centering does the rest.

I also changed \caption* to a home-made \source command, for printing the sources in smaller type.

\documentclass{article}
\usepackage{subcaption}
\usepackage[demo]{graphicx}

\newcommand{\source}[1]{{\footnotesize Source: #1\par}}

\begin{document}

\begin{figure}[htp]
\centering

\begin{subfigure}{0.3\linewidth}
\raggedright
\includegraphics[width=\textwidth, height=4 cm]{boat_gray_256.png}
\source{Public-Domain Test Images for Homeworks and Projects \cite{data_bb}}
\caption{Boat Image}\label{fig:4_2_1}
\end{subfigure}\hfill
\begin{subfigure}{0.3\linewidth}
\raggedright
\includegraphics[width=\textwidth, height=4cm]{cameraman_gray_256.png}
\source{Image Databases, Standard   \\ Test Images \cite{data_lwpcmphlwj}}
\caption{Cameraman Image}\label{fig:4_2_2}
\end{subfigure}

\begin{subfigure}{0.3\linewidth}
\raggedright
\includegraphics[width=\textwidth, height=4 cm]{lena_gray_256.png}
\source{Image Databases, Standard Test Images \cite{data_lwpcmphlwj}}
\caption{Lena Image}\label{fig:4_2_3}
\end{subfigure}

\begin{subfigure}{0.3\linewidth}
\raggedright
\includegraphics[width=\textwidth, height=4cm]{jetplane_gray_256.png}
\source{Image Databases, Standard   \\ Test Images \cite{data_lwpcmphlwj}}
\caption{Jet Plane Image}\label{fig:4_2_4}
\end{subfigure}\hfill
\begin{subfigure}{0.3\linewidth}
\raggedright
\includegraphics[width=\textwidth, height=4cm]{mri_gray_256.jpg}
\source{Northern Arizona University, Gray Scale Images \cite{data_mri}}
\caption{MRI Image}\label{fig:4_2_5}
\end{subfigure}

\caption{Used Images}\label{fig:datasetimages}

\end{figure}

\end{document}


A different layout:

\documentclass{article}
\usepackage{subcaption}
\usepackage[demo]{graphicx}

\newcommand{\source}[1]{{\footnotesize Source: #1\par}}

\begin{document}

\begin{figure}[htp]
\centering

\begin{subfigure}[t]{0.3\linewidth}
\raggedright
\includegraphics[width=\textwidth, height=4 cm]{boat_gray_256.png}
\source{Public-Domain Test Images for Homeworks and Projects \cite{data_bb}}
\caption{Boat Image}\label{fig:4_2_1}
\end{subfigure}\hfill
\begin{subfigure}[t]{0.3\linewidth}
\raggedright
\includegraphics[width=\textwidth, height=4cm]{cameraman_gray_256.png}
\source{Image Databases, Standard   \\ Test Images \cite{data_lwpcmphlwj}}
\caption{Cameraman Image}\label{fig:4_2_2}
\end{subfigure}\hfill
\begin{subfigure}[t]{0.3\linewidth}
\raggedright
\includegraphics[width=\textwidth, height=4 cm]{lena_gray_256.png}
\source{Image Databases, Standard Test Images \cite{data_lwpcmphlwj}}
\caption{Lena Image}\label{fig:4_2_3}
\end{subfigure}

\bigskip

\begin{subfigure}[t]{0.3\linewidth}
\raggedright
\includegraphics[width=\textwidth, height=4cm]{jetplane_gray_256.png}
\source{Image Databases, Standard   \\ Test Images \cite{data_lwpcmphlwj}}
\caption{Jet Plane Image}\label{fig:4_2_4}
\end{subfigure}\hspace{0.05\textwidth}%
\begin{subfigure}[t]{0.3\linewidth}
\raggedright
\includegraphics[width=\textwidth, height=4cm]{mri_gray_256.jpg}
\source{Northern Arizona University, Gray Scale Images \cite{data_mri}}
\caption{MRI Image}\label{fig:4_2_5}
\end{subfigure}

\caption{Used Images}\label{fig:datasetimages}

\end{figure}

\end{document}


I use 0.05\textwidth because this is the space between the figures on the top row (simple arithmetic). Some work may be needed for aligning the captions in the top row.