问题是框的大小大于文本宽度,这在我的论文中看起来不太好。我希望框不超过文本宽度。下面是我在 latex 文件中包含的包和包含图形的代码。
我不知道该如何解决这个问题?
\documentclass{article}
\usepackage{float}
\floatstyle{boxed}
\usepackage[framemethod=tikz]{mdframed}
\mdfdefinestyle{myFigureBoxStyle}{tikzsetting={draw=black, line width=1pt}}%
\makeatletter
\newcommand\fs@myRoundBox{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@plain
\def\@fs@pre{\begin{mdframed}[style=myFigureBoxStyle]}%
\def\@fs@mid{\vspace{\abovecaptionskip}}%
\def\@fs@post{\end{mdframed}}\let\@fs@iftopcapt\iffalse}
\makeatother
\begin{document}
\floatstyle{myRoundBox}
\restylefloat{figure}
\begin{figure}[H]
\centering
\includegraphics[width=\dimexpr\textwidth-2\fboxrule-2\fboxsep]{figure}
\caption{Figure 1}
\label{fig:f1}
\end{figure}
\end{document}
我已阅读了之前建议的解决方案,并根据这些解决方案尝试了许多选项,例如、、\fbox
等,但无法获得所需的结果。我要么得到两个框,要么得到错误。\framebox
\resizebox
答案1
我建议使用以下framed
方案来解决:
\documentclass{article}
\usepackage{showframe}
\renewcommand{\ShowFrameLinethickness}{0.2pt}
\usepackage{graphicx}
\usepackage{float, caption}
\floatstyle{boxed}
\usepackage{framed}
\begin{document}
\begin{figure}[H]
\setlength{\FrameRule}{1pt}
\captionsetup{labelsep = period}
\begin{framed}\centering
\includegraphics[width=\dimexpr\textwidth-2\fboxrule-2\fboxsep]{venus-mars-and-cupid}
\captionof{figure}{Piero di Cosimo: Venus, Mars and Cupid (1505)}
\label{fig:f2}
\end{framed}
\end{figure}
\end{document}
答案2
使用tcolorbox
很简单:
\documentclass{article}
\usepackage[hmargin=30mm,vmargin=20mm]{geometry}
\usepackage{graphicx}
\usepackage{xcolor} % had to be loaded before "tcolorbox"
\usepackage{tcolorbox}
\tcbset{colframe=blue!50!black,colback=white,colupper=red!50!black}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\begin{document}
\begin{figure}[ht]
\begin{tcolorbox}
\includegraphics[width=\linewidth]{example-image-duck}
\caption{Figure 1}
\label{fig:f1}
\end{tcolorbox}
\begin{tcolorbox}
\includegraphics[width=\linewidth]{example-image-duck}
\end{tcolorbox}
\caption{Figure 2}
\label{fig:f2}
\end{figure}
\end{document}
(红线表示页面布局)