2

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}%
  \addtolength{\textboxwidth}{-\figureboxwidth}%
  \addtolength{\textboxwidth}{-1.5em}%
  \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. – David Carlisle 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. – Thomas F. Sturm Feb 4 '15 at 14:13
  • @Thomas: I am unable to find missing \newlength{....} and could you advise/update which part is missing? – Balaji Feb 4 '15 at 19:30
  • @Balaji I added the corrected code as an answer. – Thomas F. Sturm Feb 5 '15 at 6:51
4

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

enter image description here

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}%
  \addtolength{\textboxwidth}{-\figureboxwidth}%
  \addtolength{\textboxwidth}{-1.5em}%
  \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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.