I am trying to create some nice-looking captions for some photos. I do not want to adjust the aspect ratio of these photos, so I am including the keepaspectratio option in \includegraphics.
Inspired by Martin Scharrer's answer to the question Automatic width of floats, I have used a savebox to capture the width of my photos, and then I use this width in the \captionsetup command. This approach works well, except that I am noticing a rather strange phenomenon:
- If the width option in
\includegraphicsis set towidth=1\textwidth, then everything compiles as it should. - If the width option in
\includegraphicsis set towidth=a\textwidth, where a is less than 1, then the caption will be offset to the right by about 2pt, and will thus not be correctly centered under the image. - If the width option in
\includegraphicsis set towidth=b\textwidth, where b is greater than 1, then there will be a thin amount of white space between the caption and the image (although the caption will not be offset in this case).
Below is a MWE:
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{tgbonum}
\usepackage{tikz}
\usepackage{caption}
\usepackage{float}
\usepackage{tcolorbox}
\usepackage{calc}
%include own graphics path \graphicspath{{/Users/...}}
%%captions formatting%%
\definecolor{captionColor}{RGB}{103,143,150}
\DeclareCaptionFormat{customCaption}{%
\begin{tcolorbox}[
nobeforeafter,
colback=captionColor,
arc=0pt,
outer arc=0pt,
boxrule=0pt,
colupper=white,
%fontupper=\normal,
boxsep=0pt
]
#1#2#3
\end{tcolorbox}%
}
\begin{document}
The beginning of the document stretches the full text width.
\begin{minipage}[t]{0.60\textwidth}\vspace{0pt}
There will be some text on the left side of the page. The start of this text needs to be vertically aligned with the top of the other minipage.
\end{minipage}
\quad \quad \quad
\begin{minipage}[t]{0.29\textwidth}\vspace{0pt}
%FIRST PHOTO
%use savebox to get the width of photo1
\newsavebox\placeHolderOne
\sbox\placeHolderOne{\includegraphics[width=.9\linewidth,height=.9\linewidth,keepaspectratio]{examplePhoto1}}
%set the width of the caption to the width of photo1
\captionsetup{type=figure,width=\wd\placeHolderOne,format=customCaption,labelformat=empty,justification=raggedright,position=bottom,skip=0pt,textfont={it}}
%include photo1, with caption
\includegraphics[width=.9\linewidth,height=.9\linewidth,keepaspectratio]{examplePhoto1}
{\caption[]{Caption is offset to the side when width < linewidth}}
\vspace{6mm}
%SECOND PHOTO
%use savebox to get the width of photo2
\newsavebox\placeHolderTwo
\sbox\placeHolderTwo{\includegraphics[width=1.2\linewidth,height=.9\linewidth,keepaspectratio]{examplePhoto2}}
%set the width of the caption to the width of photo2
\captionsetup{type=figure,width=\wd\placeHolderTwo,format=customCaption,labelformat=empty,justification=raggedright,position=bottom,skip=0pt,textfont={it}}
%include photo2, with caption
\includegraphics[width=1.2\linewidth,height=.9\linewidth,keepaspectratio]{examplePhoto2}
{\caption[]{A white space appears between image and caption when width>linewidth}}
\end{minipage}
\end{document}




height=.9\linewidthdidn't you intend\textheightthere? – David Carlisle Sep 09 '21 at 22:30height=.9\linewidth, although I admit it might not have been the most logical choice. My thought behind this was thatlinewidthwould be my unit, and all dimensions would be in terms of this unit (having a unit defined this way seems (at least to me) helpful since the definition of the unit will change if I change the size of the minipage in which this unit lives) – Sam Sep 09 '21 at 23:40