2

How can I force caption to align to the left corner of the image above it and also not be wider than the image in this code example?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{caption}        % for source
\usepackage{float}          
\usepackage[export]{adjustbox} % valign
\newcommand{\imagesource}[1]{{\scriptsize Source: #1}}
\usepackage[export]{adjustbox}
    
\begin{document}

\begin{figure}[H]
\centering
    \begin{minipage}[t]{.475\textwidth} % [t][][b]
        \begin{tabular}[t]{ @{} r @{} }
        \includegraphics[width = .9\linewidth,valign=t]{example-image} \\
        \imagesource{(EU 2020)}
       \end{tabular}
    \end{minipage}\hfill
    \begin{minipage}[t]{.475\textwidth}
        \begin{tabular}[t]{ @{} r @{} }
        \includegraphics[width = .7\linewidth,valign=t]{example-image} \\
        \imagesource{(IEA 2019)}
        \end{tabular}
    \end{minipage}
     \begin{minipage}[t]{.475\textwidth}
       \caption{This is a caption for the first image.}\label{fig:image1}
     \end{minipage}\hfill
     \begin{minipage}[t]{.475\textwidth}
        \caption{This is a caption for the second image.}\label{fig:image2}
     \end{minipage}
\end{figure}
\end{document}

xxx

1
  • 1
    If I compile your document, I get a different output than you. In my output, the captions each take up two lines, resulting in a different alignment. See also my recent comment under the answer to your last question.
    – leandriis
    Commented Oct 19, 2020 at 19:31

2 Answers 2

4

Like this?

enter image description here

With use of tabular instead of the minipages:

\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}           % for source
\usepackage[export]{adjustbox} % valign
\newcommand{\imagesource}[1]{{\smallskip\hfill\scriptsize Source: #1}}

\begin{document}

\begin{figure}[ht]
\centering
\setkeys{Gin}{width=\linewidth}
    \begin{tabular}{p{0.45\linewidth} p{0.34\linewidth}}
\includegraphics[valign=t]{example-image}

\imagesource{(EU 2020)}
    & 
\includegraphics[valign=t]{example-image}

\imagesource{(IEA 2019)}    \\
\caption{This is a caption for the first image.}
\label{fig:image1}
    &   \caption{This is a caption for the second image.}
        \label{fig:image2}
    \end{tabular}
\end{figure}

\end{document}

Addendum: You may liked alternative solution with use of the copyrightbox:

\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}        % for source
\newcommand{\imagesource}[1]{\hfill\scriptsize Source: #1}
\usepackage{copyrightbox}
\makeatletter
\renewcommand{\CRB@setcopyrightparagraphstyle}{\raggedleft} % new
\makeatother

\begin{document}

\begin{figure}[ht]
\centering
\setkeys{Gin}{width=\linewidth}
    \begin{tabular}{p{0.45\linewidth}p{0.34\linewidth}}
\copyrightbox[b]{\includegraphics{example-image-duck}}{(EU 2020)}
    & 
\copyrightbox[b]{\includegraphics{example-image-duck}}{(IEA 2019)}    
    \\[-3ex]
\caption{This is a caption for the first image.}
\label{fig:image1}
    &   \caption{This is a caption for the second image.}
        \label{fig:image2}
    \end{tabular}
\end{figure}

\end{document}

enter image description here

4
  • Looks interesting, but I need to have the sources aligned to the bottom right corner of each image. Otherwise it is just what I am looking for.
    – Clone
    Commented Oct 19, 2020 at 19:51
  • 1
    @Clone, see edited answer.
    – Zarko
    Commented Oct 19, 2020 at 20:04
  • very nice solution but how do I make these figuers to align with the text outside of the figure? i.imgur.com/kcwQiNB.png
    – Clone
    Commented Oct 19, 2020 at 21:49
  • 1
    @Clone, I do not understand you. How to be aligned? In the first example is provided solution for which you ask in question, the second example only show, how I would present this images. You not need to use the second example, if you not liked it.
    – Zarko
    Commented Oct 19, 2020 at 21:55
3

The following MWE allows to reproduce the output shown in the original question.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{caption}        % for source
\usepackage{float}          
\usepackage[export]{adjustbox} % valign
\newcommand{\imagesource}[1]{{\scriptsize Source: #1}}
\usepackage[export]{adjustbox}

\usepackage{fullpage} % full wide page, small margins
\begin{document}

\begin{figure}[H]
\centering
    \begin{minipage}[t]{.475\textwidth} % [t][][b]
        \begin{tabular}[t]{ @{} r @{} }
        \includegraphics[width = .9\linewidth,valign=t]{example-image} \\
        \imagesource{(EU 2020)}
       \end{tabular}
    \end{minipage}\hfill
    \begin{minipage}[t]{.475\textwidth}
        \begin{tabular}[t]{ @{} r @{} }
        \includegraphics[width = .7\linewidth,valign=t]{example-image} \\
        \imagesource{(IEA 2019)}
        \end{tabular}
    \end{minipage}
     \begin{minipage}[t]{.475\textwidth}
       \caption{This is a caption for the first image.}\label{fig:image1}
     \end{minipage}\hfill
     \begin{minipage}[t]{.475\textwidth}
        \caption{This is a caption for the second image.}\label{fig:image2}
     \end{minipage}
\end{figure}
\end{document}

As already speculated in the comments to the previous question, this behaviour is due to the caption package that automatically horizontally centers captions that only span one line. You can turn off this behaviour globally (in the preamble) or locally (in the corresponding figure environment) by using \captionsetup{singlelinecheck=false}. This will result in the expected output:

enter image description here

1
  • You can change the width of the caption using \captionsetup (inside the minipage). Commented Oct 20, 2020 at 1:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .