6

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}

1 Answer 1

6

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}
5
  • How to change the width should be exact figure size instead of textwidth.
    – Balaji
    Feb 4, 2015 at 4:15
  • @Balaji If you add the option hbox, the figure is sized to the 'natural' size of the included image (like using \includegraphics directly without options). Feb 4, 2015 at 7:01
  • Where to add the hbox command. Could you please update in the answer, please.
    – Balaji
    Feb 4, 2015 at 7:22
  • @Balaji See my updated answer. Feb 4, 2015 at 7:31
  • When using inside figure its showing error. Please refer <tex.stackexchange.com/questions/226451/…> and suggest how to fix it.
    – Balaji
    Feb 4, 2015 at 12:41

You must log in to answer this question.

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