我遇到了一个问题(可能是三个,我会详细描述它们),我正在尝试使用listings
包在 latex 中添加代码列表。首先使用限制配置:
\usepackage{listings}
\definecolor{myblue}{RGB}{20,105,176}
\lstset{ %
language=C, % choose the language of the code
basicstyle=\footnotesize, % the size of the fonts that are used for the code
numbers=left, % where to put the line-numbers
numberstyle=\footnotesize, % the size of the fonts that are used for the line-numbers
stepnumber=1, % the step between two line-numbers. If it is 1 each line will be numbered
numbersep=5pt, % how far the line-numbers are from the code
backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color}
identifierstyle=\bfseries\color{black},
directivestyle=\bfseries\color{violet},
tabsize=4, % sets default tabsize to 2 spaces
% captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
escapeinside={\%*}{*)}, % if you want to add a comment within your code
% Define TYPE-1 Keywords
keywords={},
otherkeywords={\%s, \%d},
keywordstyle=\bfseries\color{violet},
% Define TYPE-2 Keywords
keywordstyle=[2]\bfseries\color{blue}\ttfamily,
keywords=[2]{
auto,break,case,char,const,continue,default,do,double,%
else,enum,extern,float,for,goto,if,int,long,register,%
void,volatile,while,return},
% literate=
% {\{}{{{\color{white}{\{}}}}{1}
% {\}}{{{\color{white}{\}}}}}{1}
% {(}{{{\color{white}{(}}}}{1}
% {)}{{{\color{white}{)}}}}{1}
% {>}{{{\color{white}{$>$}}}}{1}
% {=}{{{\color{white}{$=$}}}}{1}
% {;}{{{\color{white}{$;$}}}}{1},
stringstyle=\color{red}\ttfamily,
commentstyle=\color{blue}\ttfamily,
directivestyle=\bfseries\color{violet},
morecomment=[l][\color{magenta}]{\#}
% escapeinside=!!
}
我在上面展示了我的代码。使用此配置,一切正常。然后我需要将背景更改为黑色,因此我只需更改:
backgroundcolor=\color{black},
identifierstyle=\bfseries\color{white},
在上面的配置中,但更改后,{
、}
、(
、)
和其他类似运算符:=
无法显示,我认为它们的颜色保持为黑色。因此,我打开了配置literate
:
literate=*%
{\{}{{{\color{white}{\{}}}}{1}
{\}}{{{\color{white}{\}}}}}{1}
{(}{{{\color{white}{(}}}}{1}
{)}{{{\color{white}{)}}}}{1}
{>}{{{\color{white}{$>$}}}}{1}
{=}{{{\color{white}{$=$}}}}{1}
{;}{{{\color{white}{$;$}}}}{1},
修改之后,我遇到一个问题,)
颜色无法改变。因此,我在这里找到了一些主题,发现有些人写literate
如下:
literate=*%
{\{}{{\color{white}{\{}}}{1}
{\}}{{\color{white}{\}}}}{1}
{(}{{\color{white}{(}}}{1}
{)}{{\color{white}{)}}}{1}
{>}{{\color{white}{$>$}}}{1}
{=}{{\color{white}{$=$}}}{1}
{;}{{\color{white}{$;$}}}{1},
{
}
每行有一对less。我应用了这个,它确实改变了)
颜色,但它导致所有其余字符都无法显示,而且行号显示也有问题:
I type some characters here and no problem to display
\begin{lstlisting}
\end{lstlisting}
I type some characters here and it can NOT be displayed and I think colour of these characters changed to white.
我该如何修改列表配置并解决这个问题?
================更新================
我刚刚分享了一个可以重现此问题的示例代码:
\documentclass[a4paper,11pt]{book}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\usepackage{xcolor,colortbl}
\usepackage{listings}
\usepackage{listings}
\definecolor{myblue}{RGB}{20,105,176}
\lstset{ %
language=C, % choose the language of the code
basicstyle=\footnotesize, % the size of the fonts that are used for the code
numbers=left, % where to put the line-numbers
numberstyle=\footnotesize, % the size of the fonts that are used for the line-numbers
stepnumber=1, % the step between two line-numbers. If it is 1 each line will be numbered
numbersep=5pt, % how far the line-numbers are from the code
backgroundcolor=\color{black}, % choose the background color. You must add \usepackage{color}
identifierstyle=\bfseries\color{white},
directivestyle=\bfseries\color{violet},
% showspaces=false, % show spaces adding particular underscores
% showstringspaces=false, % underline spaces within strings
% showtabs=false, % show tabs within strings adding particular underscores
% frame=signal, % adds a frame around the code
tabsize=4, % sets default tabsize to 2 spaces
% captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
% breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
escapeinside={\%*}{*)}, % if you want to add a comment within your code
% Define TYPE-1 Keywords
keywords={},
otherkeywords={\%s, \%d},
keywordstyle=\bfseries\color{violet},
% Define TYPE-2 Keywords
keywordstyle=[2]\bfseries\color{yellow}\ttfamily,
keywords=[2]{
auto,break,case,char,const,continue,default,do,double,%
else,enum,extern,float,for,goto,if,int,long,register,%
void,volatile,while,return},
literate=*%
{\{}{{\color{white}{\{}}}{1}
{\}}{{\color{white}{\}}}}{1}
{(}{{\color{white}{(}}}{1}
{)}{{\color{white}{)}}}{1}
{>}{{\color{white}{$>$}}}{1}
{=}{{\color{white}{$=$}}}{1}
{;}{{\color{white}{$;$}}}{1},
stringstyle=\color{red}\ttfamily,
commentstyle=\color{green}\ttfamily,
directivestyle=\bfseries\color{violet},
morecomment=[l][\color{magenta}]{\#}
% escapeinside=!!
}
\begin{document}
I type some characters here and no problem to display
\begin{lstlisting}[frame=single, label=118x_power_off,caption=213x platform power ON event detection]
int main(/*@ unused @*/int argc, /*@ unused @*/char *argv[]) //comment for lint
{
#if defined(test) //howchen, for SHUTDOWN module
/* test comment, (, ), >, <, -, =, .*/
/* test keyword */
auto,break,case,char,const,continue,default,do,double,
else,enum,extern,float,for,goto,if,int,long,register,
void,volatile,while,return;
item.a[1] = 0;
item->b[1] = 1;
foo();
/* test string */
printf(" >> test <<\r\n");
return 0;
#endif
}
\end{lstlisting}
I type some characters here and it can NOT be displayed and I think colour of these characters changed to white.
\end{document}
请使用以上代码查看结果。
\end{lstlisting}
您可以在无法显示后找到字符,并且行号停止在 1
答案1
这里确实不需要literate
。如果您希望文本为白色,只需设置
basicstyle=\color{white}\footnotesize
另外,由于行号默认采用 样式basicstyle
,因此需要设置
numberstyle=\color{black}\footnotesize
(后设置的值basicstyle
)。最后,为了避免框架颜色出现问题,您应该使用
rulecolor = \color{white}
见下文。请注意,屏幕截图中穿过列表的细水平白线只是我的 PDF 查看器造成的。
\documentclass[a4paper,11pt]{book}
\usepackage{etoolbox}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}
\usepackage{xcolor,colortbl}
\usepackage{listings}
\usepackage{listings}
\definecolor{myblue}{RGB}{20,105,176}
\lstset{ %
language=C,
basicstyle=\color{white}\footnotesize,
rulecolor = \color{white},
numbers=left,
numberstyle=\color{black}\footnotesize,
stepnumber=1,
numbersep=5pt,
backgroundcolor=\color{black},
identifierstyle=\bfseries\color{white},
directivestyle=\bfseries\color{violet},
% showspaces=false,
% showstringspaces=false,
% showtabs=false,
% frame=signal,
tabsize=4,
% captionpos=b,
breaklines=true,
% breakatwhitespace=false,
escapeinside={\%*}{*)},
% Define TYPE-1 Keywords
keywords={},
otherkeywords={\%s, \%d},
keywordstyle=\bfseries\color{violet},
% Define TYPE-2 Keywords
keywordstyle=[2]\bfseries\color{yellow}\ttfamily,
keywords=[2]{
auto,break,case,char,const,continue,default,do,double,%
else,enum,extern,float,for,goto,if,int,long,register,%
void,volatile,while,return},
stringstyle=\color{red}\ttfamily,
commentstyle=\color{green}\ttfamily,
directivestyle=\bfseries\color{violet},
morecomment=[l][\color{magenta}]{\#}
% escapeinside=!!
}
\begin{document}
I type some characters here and no problem to display
\begin{lstlisting}[frame=single, label=118x_power_off,caption=213x platform power ON event detection]
int main(/*@ unused @*/int argc, /*@ unused @*/char *argv[]) //comment for lint
{
#if defined(test) //howchen, for SHUTDOWN module
/* test comment, (, ), >, <, -, =, .*/
/* test keyword */
auto,break,case,char,const,continue,default,do,double,
else,enum,extern,float,for,goto,if,int,long,register,
void,volatile,while,return;
item.a[1] = 0;
item->b[1] = 1;
foo();
/* test string */
printf(" >> test <<\r\n");
return 0;
#endif
}
\end{lstlisting}
I type some characters here and it can NOT be displayed and I think colour of these characters changed to white.
\end{document}