图形标题在浮动内,带有浮动

图形标题在浮动内,带有浮动

我有caption with inside float并基于图形浮动内的标题。但是编译时显示! LaTeX Error: Not in outer par mode.如何解决这个问题?

我的 MWE 是:

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

答案1

在我的答案代码中,我删除了float设置,添加了缺失的\newlength{...}设置并删除了\leavevmode。这将给出以下输出:

在此处输入图片描述

代码如下:

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

相关内容