Hyphenate caption in subfigure

subfloats

I have a centered subfigure with 2 images with 2 caption like so

figures

they are made with the following code

\documentclass[12pt,a4paper]{article}
\usepackage[a4paper,margin=1in]{geometry}

 \begin{figure}[H]
 \begin{center}
      \begin{subfigure}[t]{0.4\textwidth}
          \includegraphics[width=\textwidth]{04 Introduction/figurer/Quiescent_galaxies_allz_UVJ_classic.png}
          \caption{Quiescent galaxies in UVJ colors from the Classic catalog plotted against redshift in bins of approximately 2 billion years.}
          \label{fig:cq_uvj}
      \end{subfigure}
      \hfill
      \begin{subfigure}[t]{0.4\textwidth}
          \includegraphics[width=\textwidth]{04 Introduction/figurer/Quiescent_galaxies_allz_UVJ_farmer.png}
          \centering\caption{\hspace{0pt} Quiescent galaxies in UVJ colors from the Farmer catalog plotted against redshift in bins of approximately 2 billion years.}
          \label{fig:fq_uvj}
      \end{subfigure}
         \caption{}
         \label{fig:cfq_uvj}
 \end{center}
 \end{figure}

However, the caption for each image is not hyphenated. How can I split up the words so they align nicely?

Best Answer

If you load the ragged2e package, the option justfication=RaggedRight (note the two uppercase-R letters) becomes available to \captionsetup. The difference between justfication=RaggedRight and justfication=raggedright is that the former allows hyphenation of the caption text.

enter image description here

\documentclass[demo]{article} % omit 'demo' option in real document

\usepackage{graphicx,subcaption,ragged2e}
% abandon full justification of caption text, but allow hyphenation:
\captionsetup[subfigure]{justification=RaggedRight} 

\begin{document}

\begin{figure}[ht]
\begin{subfigure}[t]{0.45\textwidth}
  \includegraphics[width=\linewidth]{04 Introduction/figurer/Quiescent_galaxies_allz_UVJ_classic.png}
  \caption{Quiescent galaxies in UVJ colors from the Classic catalogue, plotted against redshift in bins of approximately 2 billion years.}
  \label{fig:cq_uvj}
\end{subfigure}%
\hfill
\begin{subfigure}[t]{0.45\textwidth}
  \includegraphics[width=\linewidth]{04 Introduction/figurer/Quiescent_galaxies_allz_UVJ_farmer.png}
  \caption{Quiescent galaxies in UVJ colors from the Farmer catalogue, plotted against redshift in bins of approximately 2 billion years.}
  \label{fig:fq_uvj}
\end{subfigure}
\caption{}
\label{fig:cfq_uvj}
\end{figure}

\end{document}