如何仅为列表框架设置顶行的规则颜色?

如何仅为列表框架设置顶行的规则颜色?

我使用以下lstlistinglistings包):

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

如你所见,我将规则颜色设置为灰色。但我只想将框架顶部线条的规则颜色设置为灰色。所有其他线条(左、右、底部)都应保持黑色。我该如何实现呢?

答案1

这只是 Herbert 之前答案的一个小变化(将标题宽度更改为文本宽度,并将标题移近列表)。所以 Herbert 是否可以更新他的答案并删除这个?

如果不深入研究内部结构,改变一帧线的颜色是相当困难的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}

相关内容