以下代码目前用于显示图形:
\begin{figure}[ht]
\lineskip=-\fboxrule
\fbox{\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule}
\centering
\includegraphics[width=0.5\textwidth]{example-image}
\end{minipage}}
\fbox{\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule}
\caption{The caption text}
\end{minipage}}
\label{fig:test}
\end{figure}
问题:我如何调整代码,使整个框仅占页面的 0.5\textwidth,而图像占整个宽度之内盒子?
答案1
\documentclass{article}
\usepackage[export]{adjustbox}
\begin{document}
\begin{figure}[ht]
\lineskip=-\fboxrule
\fbox{\parbox{0.5\linewidth}{\includegraphics[width=\linewidth]{example-image}}}\\ \fbox{\parbox{0.5\linewidth}{
\caption{The caption text}
\label{fig:test}
}}
\end{figure}
\end{document}
补充: 如果你有许多这样的数字,那么将新命令定义为:
\newcommand\fparbox[2]{\fbox{\parbox{#1}{#2}}}
然后按如下方式使用它:
\documentclass{article}
\usepackage[export]{adjustbox}
\newcommand\fparbox[2]{\fbox{\parbox{#1}{#2}}}
\begin{document}
\begin{figure}[ht]
\lineskip=-\fboxrule
\fparbox{0.5\linewidth}{\includegraphics[width=\linewidth]{example-image}}\\
\fparbox{0.5\linewidth}{
\caption{The caption text}
\label{fig:test}
}
\end{figure}
\end{document}
结果和以前一样。
答案2
您将图像放在 minipage 中,人为地扩大了框。如果您希望框的宽度与图像相同,只需删除 minipage 即可:
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\begin{figure}[ht]
\centering
\lineskip=-\fboxrule
\fbox{%
% \begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule}
% \centering
\includegraphics[width=0.5\textwidth]{example-image}%
% \end{minipage}
}%
\fbox{%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule}
\caption{The caption text}
\end{minipage}
}
\label{fig:test}
\end{figure}
\end{document}