lstlisting:如何设置代码颜色和框架颜色

lstlisting:如何设置代码颜色和框架颜色

我用它lstlisting来在 PDF 中显示一些代码。问题是当我使用时:

  basicstyle=\fontfamily{pcr}\selectfont\footnotesize\color{red},

更改代码颜色(不仅仅是语言关键字),框架颜色也会改变。我想要实现的是让源代码全部为红色,框架为黑色。这是我的lstset

\lstset{ 
    language=C++, % choose the language of the code
    basicstyle=\fontfamily{pcr}\selectfont\footnotesize\color{red},
    keywordstyle=\color{black}\bfseries, % style for keywords
    numbers=none, % where to put the line-numbers
    numberstyle=\tiny, % the size of the fonts that are used for the line-numbers     
    backgroundcolor=\color{darkgray},
    showspaces=false, % show spaces adding particular underscores
    showstringspaces=false, % underline spaces within strings
    showtabs=false, % show tabs within strings adding particular underscores
    frame=single, % adds a frame around the code
    tabsize=2, % sets default tabsize to 2 spaces
    rulesepcolor=\color{gray}
    captionpos=b, % sets the caption-position to bottom
    breaklines=true, % sets automatic line breaking
    breakatwhitespace=false, 
}

答案1

包裹listings提供选项rulecolor

rulecolor=\color{black},

要全面了解所有选项,请查看文档

下面给出了您的设置示例。我认为您的设置什么都看不到。

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{ 
    language=C++, % choose the language of the code
    basicstyle=\fontfamily{pcr}\selectfont\footnotesize\color{red},
    keywordstyle=\color{black}\bfseries, % style for keywords
    numbers=none, % where to put the line-numbers
    numberstyle=\tiny, % the size of the fonts that are used for the line-numbers     
    backgroundcolor=\color{darkgray},
    showspaces=false, % show spaces adding particular underscores
    showstringspaces=false, % underline spaces within strings
    showtabs=false, % show tabs within strings adding particular underscores
    frame=single, % adds a frame around the code
    tabsize=2, % sets default tabsize to 2 spaces
    rulesepcolor=\color{gray},
    rulecolor=\color{black},
    captionpos=b, % sets the caption-position to bottom
    breaklines=true, % sets automatic line breaking
    breakatwhitespace=false, 
}
\begin{document}
\begin{lstlisting}

Some Code 

for loop

if

return
\end{lstlisting}
\end{document}

相关内容