Your code uses nothing involving the algorithm2e
package and the use of the packages conflict with one another. Instead add the algorithm
package to your preamble.
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Euclid's algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The gcd of $a$ and $b$}
\State $r \gets a \bmod b$
\While{$r \neq 0$}\Comment{We have the answer if $r$ is $0$}
\State $a \gets b$
\State $b \gets r$
\State $r \gets a \bmod b$
\EndWhile\label{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is $b$}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}
I suggest the following:
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}
Best Answer
The second table started with
instead of
meaning it missed a
\
and that was what gave the error of a caption outside a float.