# Precise alignment of a caption to the graphic, not wrapfigure

I'm a bit anal about caption positioning. In the following screenshots there are several issues:

Figure (0.1):

• ✅ the image is correctly positioned at the very right (it touches the magenta line)
• ✅ left margin with the text is enough
• ❌ caption is not aligned with the image

Figure (0.2):

• ❌ image is wrongly positioned (it doesn't touch the magenta line on the right)
• ❌ left margin is too small
• ✅ caption is perfectly aligned with the image

My goal is to score ✅✅✅, i.e. align the image to the far right, have a large enough left margin and a raggedright caption aligned with the image left.

In the following MWE you see that I'm using wrapfig and adjustbox that allows an extra parameter to includegraphics (the position relative to the wrapfigure). Both wrapfigures are aligned to the right, but...

In the Figure (0.1) I'm aligning the image to the right relative to the wrapfigure (good), but the caption starts at the default margin on the left (wrong). By trial and error I could shorten the wrapfigure itself, but I would end with a too short left margin (also wrong)

In the Figure (0.2) I'm aligning the image to the left (relative to the wrapfigure), which helps in aligning it with the caption (good), but the image doesn't reach the far right (wrong) and the left margin is too short (also wrong). Using a wider image doesn't really help because even if the image would end up touching the right, the left margin keeps being too small.

Also, why is that the first image starts exactly at the top of the page, while the second is vertically off (~ one line) relative to where it should be?

If this is of any help, I'm using a document of class scrbook.

## MWE

\documentclass[11pt,oneside,captions=centeredbeside]{scrbook}

\usepackage{blindtext}
\usepackage{wrapfig}

\usepackage{xcolor}
\definecolor{caption-label-color}{HTML}{333333}
\definecolor{caption-text-color}{HTML}{777777}

\usepackage{caption}
\DeclareCaptionLabelSeparator{enskip}{\enskip}
\DeclareCaptionFont{blah}{\small\sffamily\selectfont}
\captionsetup[figure]{
position=bottom,
skip=3mm,
labelfont={bf,sf,color=caption-label-color},
textfont={blah,color=caption-text-color},
singlelinecheck=false,
justification=raggedright,
labelformat=parens,
labelsep=enskip
}
\setcapindent{0em}

\begin{document}

\begin{wrapfigure}{r}{0.50\textwidth}
\includegraphics[width=0.47\textwidth,right]{example-image-a}
\caption{A long caption that spans more than one row}
\end{wrapfigure}

\blindtext[1]

\begin{wrapfigure}{r}{0.50\textwidth}
\includegraphics[width=0.47\textwidth]{example-image-a}
\caption{A long caption that spans more than one row}
\end{wrapfigure}

\blindtext[1]
\end{document}


Thanks!

• Have you tried putting both the image and the caption in one minipage and flush it right? I have no computer now, but like (into the wrapfig) \hfill\begin{minipage}{0.95\linewdth}\includegraphic[width=\linewidth]{•••}\caption{•••}\end{minipage}? – Rmano Apr 7 at 21:05

I propose this solution with the insbox plain TeX macro package and the captionof{figure} command:

\documentclass[11pt,oneside, captions=centeredbeside]{scrbook}%
\usepackage{showframe}
\renewcommand{\ShowFrameLinethickness}{0.3pt}
\usepackage{blindtext}
\usepackage{wrapfig}
\input{insbox}
\makeatletter
\@InsertBoxMargin=3mm
\makeatother
\usepackage{floatrow, threeparttable}
\usepackage{xcolor}
\definecolor{caption-label-color}{HTML}{333333}
\definecolor{caption-text-color}{HTML}{777777}

\usepackage{caption}
\DeclareCaptionLabelSeparator{enskip}{\enskip}
\DeclareCaptionFont{blah}{\small\sffamily\selectfont}
\captionsetup[figure]{
position=bottom,
skip=3mm,
labelfont={bf,sf,color=caption-label-color},
textfont={blah,color=caption-text-color},
singlelinecheck=false,
justification=raggedright,
labelformat=parens,
labelsep=enskip
}
\setcapindent{0em}

\begin{document}

\InsertBoxR{0}{\parbox{36mm}{\captionsetup{format=plain}%
{\raggedleft\includegraphics[width=36mm]{example-image-a}%
\captionof{figure}{A long caption that spans more than one row.}}}}[4]
\blindtext[1]

\end{document}


You can put both your figure and the caption in a minipage a bit smaller than the wrapped figure, and align the minipage to the right. Notice here:

1. I am using \linewidth, which is relative to the enclosing environment, so that if I later change the wrap figure width I can change it only in one place;

2. I changed blindtext for kantlipsum because the first one creates a nasty underfull hbox that I hate (everyone has their pet peeves).

3. I added geometry to show the text frames.

\documentclass[11pt,oneside,captions=centeredbeside]{scrbook}

\usepackage{kantlipsum}
\usepackage[showframe]{geometry}
\usepackage{wrapfig}

\usepackage{xcolor}
\definecolor{caption-label-color}{HTML}{333333}
\definecolor{caption-text-color}{HTML}{777777}

\usepackage{caption}
\DeclareCaptionLabelSeparator{enskip}{\enskip}
\DeclareCaptionFont{blah}{\small\sffamily\selectfont}
\captionsetup[figure]{
position=bottom,
skip=3mm,
labelfont={bf,sf,color=caption-label-color},
textfont={blah,color=caption-text-color},
singlelinecheck=false,
justification=raggedright,
labelformat=parens,
labelsep=enskip
}
\setcapindent{0em}

\begin{document}

\kant[1]

\begin{wrapfigure}{r}{0.50\textwidth}
\hfill\begin{minipage}{0.95\linewidth}
\includegraphics[width=\linewidth]{example-image-a}
\caption{A long caption that spans more than one row}
\end{minipage}
\end{wrapfigure}

\kant[2]

\end{document}