# figure caption inside float with float

I have caption with inside float and created based on Caption inside figure float. But when compiling it's showing ! LaTeX Error: Not in outer par mode. How do solve this?

My MWE is:

\documentclass{book}
\usepackage{lipsum,fancybox,pbox,graphics}
\usepackage{caption}
\makeatletter
\newlength{\raisefigure}
\setlength{\raisefigure}{-0.75em}
\newenvironment{textbyfigure}[2][0.75em]{%
\leavevmode
\setlength{\raisefigure}{-#1}% Added in version 1.4
\begin{Sbox}\hbox{#2}\end{Sbox}%
\settowidth{\figureboxwidth}{\TheSbox}%
\setlength{\textboxwidth}{\linewidth}%
\begin{Sbox}\hbox{#2}\end{Sbox}%
\setlength{\saveparskip}{\parskip}
\noindent\begin{minipage}[t]{\textboxwidth}\ignorespaces
}{%
\end{minipage}%
\hfill
\begin{minipage}[t]{\figureboxwidth}
\vskip\raisefigure\TheSbox
\end{minipage}\par
\vspace{1ex}
}
\makeatother

\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}

\lipsum[1-2]

\begin{textbyfigure}{\mygraphics{Wing.png}{The Roman numerals on     this stone show the distance to the next village.}}
Throughout the ages and in different countries, number systems were     developed and used to help people
count and communicate with numbers. From the ancient Egyptians to     the modern day, different systems have
used pictures and symbols to represent whole numbers. Some of the     well-known number systems are the
Egyptian, Babylonian, Roman, modern Chinese and the Hindu-Arabic or     decimal system.
\end{textbyfigure}
\lipsum[3]

\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}

• It is not clear what the intention of your code is, but a float has to be at the top level so that it can float . You can not use it inside minipage or inside \sbox You need to do any boxing or measuring inside the float, measuring its contents. Feb 4 '15 at 9:55
• As David Carlisle wrote, you cannot have a float inside a minipage. If you want to have your image to be non-floating, you should replace float*,every float=\centering by nobeforeafter. After adding some missing \newlength{...}, your code compiles. Feb 4 '15 at 14:13
• @Thomas: I am unable to find missing \newlength{....} and could you advise/update which part is missing? Feb 4 '15 at 19:30
• @Balaji I added the corrected code as an answer. Feb 5 '15 at 6:51

In my answer code, I removed the float settings, added the missing \newlength{...} settings and removed the \leavevmode. This gives the following output:

The code is:

\documentclass{book}
\usepackage{lipsum,fancybox,pbox,graphics}
\usepackage{caption}
\makeatletter
\newlength{\raisefigure}
\newlength{\figureboxwidth}
\newlength{\textboxwidth}
\newlength{\saveparskip}

\setlength{\raisefigure}{-0.75em}

\newenvironment{textbyfigure}[2][0.75em]{%
%\leavevmode%
\setlength{\raisefigure}{-#1}% Added in version 1.4
\begin{Sbox}#2\end{Sbox}%
\settowidth{\figureboxwidth}{\TheSbox}%
\setlength{\textboxwidth}{\linewidth}%
\begin{Sbox}\hbox{#2}\end{Sbox}%
\setlength{\saveparskip}{\parskip}
\noindent\begin{minipage}[t]{\textboxwidth}\ignorespaces
}{%
\end{minipage}%
\hfill
\begin{minipage}[t]{\figureboxwidth}
\vskip\raisefigure\TheSbox
\end{minipage}\par
\vspace{1ex}
}
\makeatother

\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,
nobeforeafter,
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}

\lipsum[1-2]

\begin{textbyfigure}{\mygraphics[graphics options={scale=0.5}]{Wing.png}{The Roman numerals on     this stone show the distance to the next village.}}
Throughout the ages and in different countries, number systems were     developed and used to help people
count and communicate with numbers. From the ancient Egyptians to     the modern day, different systems have
used pictures and symbols to represent whole numbers. Some of the     well-known number systems are the
Egyptian, Babylonian, Roman, modern Chinese and the Hindu-Arabic or     decimal system.
\end{textbyfigure}
\lipsum[3]

\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}

• I would like to print the caption bottom of the figure. So i have changed to \node[above, to \node[below,. But the caption are merged with next paragraph. How to avoid this. Feb 5 '15 at 7:02
• @Balaji That is another question. I think, Gonzalo Medina has answered it in tex.stackexchange.com/questions/226566/… and will continue the matter in tex.stackexchange.com/questions/226566/… in short time. Feb 5 '15 at 7:10