[Tex/LaTex] How to set rulecolor in a listings frame only for the top line

framedlistings

I use the following lstlisting (listings package):

   \documentclass{thesis}

\usepackage{ucs}
\usepackage[utf8]{inputenc}
\usepackage{url}
\usepackage{bbding}
\usepackage{listings}
\usepackage{tikz}
\usepackage[center]{caption}
\usepackage{tabularx}
\usepackage{amssymb}
\usepackage{array}
\usepackage{nameref}
\usepackage[section]{placeins}
\usepackage{graphicx}



\usepackage{calc} \newlength\tdima \newlength\tdimb \setlength\tdima{ \fboxsep+\fboxrule} \setlength\tdimb{-\fboxsep+\fboxrule}

\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

\begin{document}

\lstset{language=SQL,morekeywords={PREFIX,java,rdf,rdfs,url}}
\begin{lstlisting}[captionpos=t,label=lst:sparql,caption=SPARQL query to retrieve the names of each package in a software project,frame=tlrb, xleftmargin = \tdima, xrightmargin = \tdimb, rulecolor= \color{gray}]
PREFIX java: <http://evolizer.org/ontologies/seon/2009/06/java.owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?url ?name
WHERE {
   ?url rdf:type java:Package .
   ?url rdfs:label ?name
}
\end{lstlisting}

\end{document}

As one can see I set the rulecolor to gray. But I want to set the rule color only for the top line of the frame to gray. All the other lines (left, right, bottom) should stay black. How can I achieve that?

Best Answer

This is just a small variation on Herbert's previous answer (changes the caption width to text width and moved the caption closer to the listing). So maybe Herbert can update his answer and remove thisone?

Tho change the color of one frame line is quite difficult without digging into the internals of listings

\documentclass{thesis}
\usepackage{calc}
\usepackage[utf8]{inputenc}

\usepackage[T1]{fontenc}
\usepackage[scaled=0.85]{beramono}
\usepackage{textcomp}

\usepackage{xcolor}

\usepackage{listings}
    \newlength\tdima
    \setlength\tdima{\fboxsep+\fboxrule}
    \lstset{language         = SQL,
            morekeywords     = {PREFIX,java,rdf,rdfs,url},
            captionpos       = t,
            belowcaptionskip = -1pt,
            frame            = lrb,
            xleftmargin      = \tdima,
            xrightmargin     = \tdima,
            rulecolor        = \color{black},
            basicstyle       = \color{black}\raggedright\small\ttfamily,
            upquote          = true,}

\usepackage[center]{caption}
    \DeclareCaptionFont{white}{\color{white}}
    \DeclareCaptionFormat{listing}{%
         \colorbox{gray}{\parbox{\textwidth-2\fboxsep}{#1#2#3}}}
    \captionsetup[lstlisting]{%
        format=listing,
        labelfont=white,
        textfont=white}

\begin{document}
\begin{lstlisting}[caption=SPARQL query to retrieve the names of each package in a software project,
                   label=lst:sparql]
PREFIX java: <http://evolizer.org/ontologies/seon/2009/06/java.owl#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?url ?name
WHERE {
   ?url rdf:type java:Package .
   ?url rdfs:label ?name
}
\end{lstlisting}
\end{document}