# Listings with rule above caption to look like \floatstyle{ruled}

I am using the listings package.

I would like my listings to have a heavy rule above the caption, so it looks like the \floatstyle{ruled}.

So it would look like:

a bold line ===

caption

rule ---

code

rule ---

-

## migrated from stackoverflow.comMar 8 '12 at 17:16

This question came from our site for professional and enthusiast programmers.

Give a example file. I will give a solution –  Mu30 Mar 9 '12 at 14:28
Welcome to TeX.SE. Please keep in mind that it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. Basically, show some work by getting the example as far as you can. –  Peter Grill Mar 10 '12 at 3:59

The packages listings provides the option frame=single which draws a single line above and below the environment itself. So you have to add a single bold line before the caption is set. Therefore I am using the command pretocmd provided by etoolbox.

\documentclass{article}
\usepackage{listings}
\usepackage{etoolbox}
\makeatletter
\lstset{frame=lines}
\pretocmd\lst@makecaption{\noindent{\rule{\linewidth}{2pt}}}{}{}
\makeatother

\begin{document}
\begin{lstlisting}[caption={Some Caption}]
static uint64_t i = 0;
void every_cycle()
{
if (i > 0)
i--;
}

uint64_t next_num()
{
return (i += 0x100);
}
\end{lstlisting}
Text
\begin{lstlisting}
static uint64_t i = 0;
void every_cycle()
{
if (i > 0)
i--;
}

uint64_t next_num()
{
return (i += 0x100);
}
\end{lstlisting}
\end{document}


-

You can directly use the ruled style this way:

\usepackage{float}
\floatstyle{ruled}
\newfloat{code}{thp}{lop}
\floatname{code}{Listing}
\numberwithin{code}{chapter}


Then do \begin{code} ... \end{code} around the listing instead of \begin{figure} ... \end{figure}.

-