图中的标题周围有方框

图中的标题周围有方框

我想知道是否有可能在图中只围绕标题而不是整个图形放置一个框。

\documentclass[12pt,letter]{article}    
\usepackage[demo]{graphicx}     

\begin{document}

\begin{figure}
\includegraphics[width=6in]{foo.ps}

\caption{I sure hope I can be boxed separate from the picture...}
%some command that can put a box around the caption
\end{figure}

\end{document}

编辑我在命令[width=6in]上添加了\includegraphics。这是我所包含的图形的必要部分。

答案1

进行以下调整将\@makecaption图形标题放置在可调整标题宽度的框内:

在此处输入图片描述

\documentclass{article}
\usepackage{graphicx}% http://ctan.org/pkg/graphicx

\makeatletter
\long\def\@makecaption#1#2{%
  \vskip\abovecaptionskip
  \sbox\@tempboxa{\fbox{#1: #2}}%
  \ifdim \wd\@tempboxa >\hsize
    \fbox{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{#1: #2}}\par
  \else
    \global \@minipagefalse
    \hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
  \fi
  \vskip\belowcaptionskip}
\makeatother

\begin{document}

\begin{figure}
  \centering
  \includegraphics[width=100pt]{example-image}
  \caption{I sure hope I can be boxed separate from the picture\ldots}
  \caption{I sure hope I can be boxed separate from the picture.
   I sure hope I can be boxed separate from the picture.
   I sure hope I can be boxed separate from the picture.}
\end{figure}

\end{document}

的定义\@makecaption取自(并修改自)article.cls


caption还允许设置标题样式。因此,如果您对固定宽度的标题感兴趣,可以使用类似以下内容:

在此处输入图片描述

\documentclass{article}
\usepackage{caption,graphicx}% http://ctan.org/pkg/{caption,graphicx}

\DeclareCaptionFormat{plain}{%
  \fbox{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{\centering #1#2#3}}}

\begin{document}

\begin{figure}
  \centering
  \includegraphics[width=100pt]{example-image}
  \caption{I sure hope I can be boxed separate from the picture\ldots}
  \caption{I sure hope I can be boxed separate from the picture.
   I sure hope I can be boxed separate from the picture.
   I sure hope I can be boxed separate from the picture.}
\end{figure}

\end{document}

样式设置为,\centering #1#2#3其中#1引用标题标签、#2标题标签分隔符和#3标题文本。请参阅章节4 自身增强功能(第 24 页)caption包装文档

相关内容