可能重复:
如何在小页面中使用图形?
对于某种报告,我必须将每个部分写在一个方框内。为此,我使用以下内容:
\newcommand{\textbox}[1]{
\hfill
\centering
\fbox{
\begin{minipage}{0.95\textwidth}
\vspace{0.2cm}
#1
\end{minipage}
\newline
}
\vspace{1cm}
}
使用方法很简单:
\textbox{
This is my first part.
}
\textbox{
This is my second part.
}
如何将图形放入这些框中?如果我尝试:
\textbox {
This is my third part.
\begin{figure}
\includegraphics[width=9cm]{myfigure.png}
\vspace{-12pt}
\caption{My caption.}
\label{fig:myfigure}
\end{figure}
}
失败并出现以下错误:LaTeX Error : Float(s) lost.
如何解决这个问题呢 ?
答案1
正如 lockstep 在链接的答案中所说,您不能将figure
a 放在里面minipage
,因为前者是浮点数而后者不是。解决方案是定义一个单独的figurebox
除了,textbox
如 MWE 中所示。您还必须%
在适当的位置添加以避免出现虚假空格。
\documentclass{article}
\usepackage{graphicx}
\newcommand{\textbox}[1]{%
\par
\noindent
\fbox{%
\begin{minipage}{0.95\textwidth}%
\vspace{0.2cm}
\centering
#1
\end{minipage}%
}
\vspace{1cm}
}%
%
\newcommand{\figurebox}[4]{%
\begin{figure}[htb]%
\fbox{%
\centering
\begin{minipage}[c]{0.95\textwidth}
\centering #1\par
\includegraphics[width=3.0in]{#2}
\caption{#3}
\label{#4}
\end{minipage}%
}%
\end{figure}%
}%
\begin{document}
\noindent
\textbox{This is my first part.}%
\textbox{This is my second part.}
%% syntax is \figurebox{text}{figure file name}{caption}{label}
\figurebox{This is my third part}{example-image-a}{My figure}{fig:first}
\end{document}
选项 -2
如果您也想textbox
对图形使用相同的命令,那么您必须避免使用浮动figure
来获取您可以使用的标题caption
或capt-of
包。
\documentclass{article}
\usepackage{graphicx,capt-of}
\newcommand{\textbox}[1]{%
\par
\noindent
\fbox{%
\begin{minipage}{0.95\textwidth}%
\vspace{0.2cm}
\centering
#1
\end{minipage}%
}
\vspace{1cm}
}%
\begin{document}
\noindent
\textbox{This is my first part.}%
\textbox{This is my second part.}
\textbox {%
This is my third part.
\includegraphics[width=9cm]{example-image-a}
\vspace{-12pt}
\captionof{figure}{My caption.}
\label{fig:myfigure}
}%
\end{document}