5

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 ---

  • Give a example file. I will give a solution – Mu30 murugans2katgmail 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
7

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}

enter image description here

| improve this answer | |
0

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}.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.