# Add a rule above and below each figure (including caption)

I have a document with a lot of figures and I want to modify them so that there is a horizontal rule above and below each figure so it stands out from the text. That is, I want it to look something like this:

text .... text .... text ....

text .... text .... text ....

-------------------------------- % rule

FIGURE HERE

...........

caption of figure: blah blah

------------------------------- % another rule

text .... text .... text ....

I know I can individually go and modify each figure individually, but I'm wondering if there's a way to do this automatically to all figures....

• See the caption package documentation, § 4, Own Enhancements, the command DeclareCaptionFormat. – Bernard Jul 25 '19 at 0:23
• I will look into this, but my understanding is that the caption package allows me to add rules above and below the caption, but not above the figure. – johnymm Jul 25 '19 at 0:31
• Oh! It seems I misunderstood. Do you want a rule above the figure, and another below the caption? – Bernard Jul 25 '19 at 1:11
• That is exactly right. I want a rule above the figure, and one below the caption, so that the entire thing is separated visual from the regular text. – johnymm Jul 25 '19 at 1:20
• I think tcolorbox might help. And the rules should have the width of the figure, more or less? – Bernard Jul 25 '19 at 1:27

You can use float.

\documentclass{article}
\usepackage{graphicx}
\usepackage{float}

\makeatletter
\newcommand\fs@tbruled{%
\def\@fs@cfont{\normalfont}%
\let\@fs@capt\floatc@plain
\def\@fs@pre{\hrule\kern2pt}%
\def\@fs@post{\kern2pt\hrule\relax}%
\def\@fs@mid{\vspace{\abovecaptionskip}}%
\let\@fs@iftopcapt\iffalse
}
\makeatother

\floatstyle{tbruled}
\restylefloat{figure}

\begin{document}

\begin{figure}[htp]
\centering

\includegraphics{example-image-a}
\caption{My caption}

\end{figure}

\end{document}


Let's see what the commands do:

\def\@fs@cfont{\normalfont}%              The font for the caption label
\let\@fs@capt\floatc@plain %              Use plain style caption
\def\@fs@pre{\hrule\kern2pt}%             The rule at the top
\def\@fs@post{\kern2pt\hrule\relax}%      The rule at the bottom
\def\@fs@mid{\vspace{\abovecaptionskip}}% Material to be inserted before the caption
\let\@fs@iftopcapt\iffalse %              We don't want the caption is moved at the top


Should you want more space after the top rule, change into

\def\@fs@pre{\hrule\kern12pt}%


or whatever space you deem good. Similarly for the rule at the bottom.

• Excellent. You don't have to do this, but I would love to learn what each of those commands do in the definition of tbruled. Also, how can I adjust the vertical space below and above the rules ? – johnymm Jul 25 '19 at 13:26
• @johnymm Your wish is my command – egreg Jul 25 '19 at 13:33
• Thank you very much. – johnymm Jul 25 '19 at 13:36

\documentclass{article}
\usepackage{graphicx}
\newenvironment{myfigure}%
{\begin{figure}[htb]\hrule\vspace{3ex}\centering}%
{\vspace{3ex}\hrule\end{figure}}

\begin{document}

• This works, but it would involve me going into the multiple tex.files that I have and change every instance of \begin{figure} ... \end{figure}, which I was hoping to avoid. – johnymm Jul 25 '19 at 13:25