# How to set rulecolor in a listings frame only for the top line?

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?

-
Can you add a full minimal example so that we can compile this. \tdima and tdimb are undefined. Presumably you want to copy Herbert's answer to this question –  Seamus Apr 5 '11 at 11:25
Yes this is the first question. I changed my question and included a whole example. –  RoflcoptrException Apr 5 '11 at 11:34
This is a question of taste, but I think you should not have complicated or distracting decorations around text. If you want a frame, I think it should be a uniform looking frame, of a unique color. A distraction just attracts the eye and the reader is no longer reading the text. A frame should only indicate that something special is inside, not that it is the frame that is special and important to look at. –  Frédéric Apr 5 '11 at 12:05
Generally I can agree with you, but I think in this case, the frame is more special if it is all gray and not if the frame is black as the text around it. That's why I try to change it. –  RoflcoptrException Apr 5 '11 at 12:12

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}

-