TransWikia.com

Justify customised captions in a figure

TeX - LaTeX Asked by yufiP on April 1, 2021

I am using a customised caption with subfigures. For example, one of them has two subfigures vertically stacked one on top of the other. What I want to do is to display my caption below figure, starting from the left bottom corner of the figure. But the general solutions such as adjusting caption settings justification=justified does not work and creates
enter image description here

My current lines of code creating the image above are

documentclass[a4paper,11pt]{report}
usepackage{import}
usepackage{graphicx}
usepackage{subfig}
usepackage{tabularx}
usepackage{booktabs}
usepackage{caption}

newcommandmycaption[2]{caption{textbf{#1}newlinefootnotesize#2}}
captionsetup{font=small, labelfont=bf, singlelinecheck=false, tableposition=bottom, justification=justified}
begin{document}

begin{figure}[t]
    centering
    subfloat[First figure]
    {
        label{fig.test1}
        includegraphics[clip,width=0.6columnwidth]{figureTest.png} } 
    vfill 
    subfloat[Second figure]
    label{fig.test2}
    {
        includegraphics[clip,width=0.6columnwidth]{figureTest.png} } 
    mycaption{I want to display this caption right below the left corner of the figure.}{Explanation blah blah..}
    label{fig.test1}
end{figure}

end{document}

I see many similar questions, but haven’t found the desired solution that works for my customised captions. Would anyone be able to point me to the right direction?

2 Answers

I'd use subcaption instead of subfig (which seems to add some small horizontal space).

documentclass[a4paper,11pt]{report}
usepackage{graphicx}
usepackage{subcaption} % also loads caption

captionsetup{
  font=small,
  labelfont=bf,
  font=bf,
  singlelinecheck=false,
  figureposition=bottom,
  tableposition=bottom,
  justification=justified
}
captionsetup[subfigure]{font=normalfont}
newcommandcaptionexplain[1]{parmedskip{footnotesize#1par}}

begin{document}

begin{figure}[t]
centering

begin{subfigure}{0.6columnwidth}
  includegraphics[clip,width=textwidth]{example-image}%
  caption{First figurelabel{fig.test1}}
end{subfigure}

medskip

begin{subfigure}{0.6columnwidth}
  includegraphics[clip,width=textwidth]{example-image}%
  caption{Second figurelabel{fig.test2}}
end{subfigure}

begin{minipage}{0.6columnwidth}
caption{I want to display this caption right below the left corner 
  of the figure.label{fig.test}}

captionexplain{Explanation blah blah..}
end{minipage}
end{figure}

end{document}

I'd not use mycaption, but a proper auxiliary command, as shown.

enter image description here

Correct answer by egreg on April 1, 2021

First of all there are extra (unwanted) spaces in front of the image, therefore the caption is not adjusted correctly with the left margin of the image. Removing the extra spaces with % will remove them.

See also: Unwanted spaces

documentclass[a4paper,11pt]{report}
usepackage{import}
usepackage{graphicx}
usepackage{subfig}
usepackage{tabularx}
usepackage{booktabs}
usepackage{caption}

newcommandmycaption[2]{caption{textbf{#1}newlinefootnotesize#2}}
captionsetup{font=small, labelfont=bf, singlelinecheck=false, tableposition=bottom, justification=justified}
begin{document}

begin{figure}[t]
    centering
    subfloat[First figure]
    {%
        label{fig.test1}%
        includegraphics[clip,width=0.6columnwidth]{example-image-a.png}}
    vfill
    subfloat[Second figure]
    {%
        label{fig.test2}%
        includegraphics[clip,width=0.6columnwidth]{example-image-a.png}}
    mycaption{I want to display this caption right below the left corner of the figure.}{Explanation blah blah..}
    label{fig.test}
end{figure}

end{document}

If you do not want to align the caption with the left margin of the image, but with the y-axis of the image instead, one needs to add a little bit tinkering:

documentclass[a4paper,11pt]{report}
usepackage{import}
usepackage{graphicx}
usepackage{subfig}
usepackage{tabularx}
usepackage{booktabs}
usepackage{caption}

newcommandmycaption[2]{caption{textbf{#1}newlinefootnotesize#2}}
captionsetup{font=small, labelfont=bf, singlelinecheck=false, tableposition=bottom, justification=justified}
begin{document}

begin{figure}[t]
    captionsetup[subfigure]{margin=0.5cm} % Add extra margin to the sub-captions
    centering
    subfloat[First figure]
    {%
        label{fig.test1}%
        includegraphics[clip,width=0.6columnwidth]{example-image-a.png}}
    vfill
    subfloat[Second figure]
    {%
        label{fig.test2}%
        includegraphics[clip,width=0.6columnwidth]{example-image-a.png}}
    mycaption{I want to display this caption right below the left corner of the figure.}{Explanation blah blah..}
    label{fig.test}
end{figure}

end{document}

I have added extra 0.5cm margin here, you need to adjust this value until the caption fits.

Answered by P.M. on April 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP