[Tex/LaTex] Caption inside figure float

captionsfloats

I would like to expect the below output in my LaTeX.

enter image description here

The figure caption should be place in the bottom of the figure. The text should be plurred with opacity. How do achieve this?

My MWE is:

\documentclass{book}
\usepackage{graphics}
\usepackage{caption}
\DeclareCaptionFormat{overlay}{\gdef\capoverlay{#1#2#3\par}}
\DeclareCaptionStyle{overlay}{format=overlay}
\begin{document}

\begin{figure*}
\includegraphics{example-image-a}
\caption{Many of the simple rexpressions in algebra can be thought of     interms of the areas of rectangles.}
\end{figure*}
\end{document}

Best Answer

I suggest the following:

enter image description here

This is realized using the tcolorbox package:

\documentclass{book}
\usepackage{graphics}
\usepackage{caption}
\DeclareCaptionFormat{overlay}{\gdef\capoverlay{#1#2#3\par}}
\DeclareCaptionStyle{overlay}{format=overlay}

\usepackage[skins]{tcolorbox}

\tcbset{
  caption color/.store in=\captioncolor,
  caption color=white}

\newcommand{\mygraphics}[3][]{%
  \tcbincludegraphics[float*,every float=\centering,blanker,finish={%
    \captionsetup{skip=0pt}%
    \tcbsetmacrotowidthofnode\mywidth{interior}%
    \node[above,fill=\captioncolor,fill opacity=0.5,text opacity=1,
      outer sep=0mm,inner sep=2mm,text width=\mywidth-4mm]
      at (interior.south) {\captionof{figure}{#3}};},
    #1]{#2}%
}

\begin{document}

\mygraphics[caption color=blue!50]{Wing.png}
  {Many of the simple expressions in algebra can be thought of
   in terms of the areas of rectangles.}

\mygraphics[width=7cm]{example-image-a}
  {Many of the simple expressions in algebra can be thought of
   in terms of the areas of rectangles.}

\end{document}

I made a macro \mygraphics which takes two mandatory parameters: the name of the image file and the caption text. Further, a key list can be given as optional parameter. The keys can be any tcolorbox keys. The image is made a float* as was given by the OP.

In this application, the following options are especially interesting:

  • width to set the width of the image (shown in the example code). The default is the complete text width.

  • caption color to set the background color of the caption (show in the example code). The default is white.

  • floatplacement to set the float options like htb etc.

Update:

To scale the figure according to the natural size of included image, the hbox option can be added. But, many pictures may become larger than the textwidth. However, to give options to the underlying \includegraphics macro, you can use graphics options if needed.

The following code has the hbox option set as default. Also, the second example shows how to put a scale=0.5 option to a picture:

\documentclass{book}
\usepackage{graphics}
\usepackage{caption}
\DeclareCaptionFormat{overlay}{\gdef\capoverlay{#1#2#3\par}}
\DeclareCaptionStyle{overlay}{format=overlay}

\usepackage[skins]{tcolorbox}

\tcbset{
  caption color/.store in=\captioncolor,
  caption color=white}

\newcommand{\mygraphics}[3][]{%
  \tcbincludegraphics[float*,every float=\centering,blanker,
    hbox,% <--- the width is determined by the underlying '\includegraphics'
    finish={%
    \captionsetup{skip=0pt}%
    \tcbsetmacrotowidthofnode\mywidth{interior}%
    \node[above,fill=\captioncolor,fill opacity=0.5,text opacity=1,
      outer sep=0mm,inner sep=2mm,text width=\mywidth-4mm]
      at (interior.south) {\captionof{figure}{#3}};},
    #1]{#2}%
}

\begin{document}

\mygraphics[caption color=blue!50]{Wing.png}
  {Many of the simple expressions in algebra can be thought of
   in terms of the areas of rectangles.}

\mygraphics[graphics options={scale=0.5}]{example-image-a}
  {Many of the simple expressions in algebra can be thought of
   in terms of the areas of rectangles.}

\end{document}
Related Question