目标显示:
我的乳胶代码:
% xxxxxxxxxxxxxxxxxxxxxxxxx Code Snippet STARTS xxxxxxxxxxxxxxxxxxxxxx
\lstset{
language=C, % choose the language of the code
stepnumber=1, % the step between two line-numbers. If it's 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}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
tabsize=4, % sets default tabsize to 2 spaces
captionpos=t, % sets the caption-position to top
breaklines=true, % sets automatic line breaking
breakatwhitespace=true, % sets if automatic breaks should only happen at whitespace
% title=\lstname, % show the filename of files included with \lstinputlisting;
identifierstyle=\color{black},
caption={Array of Pointers to Strings},
frame=lrtb,
%keywordstyle=\bfseries\color{OliveGreen}, % keyword style
keywordstyle=[1]\bfseries\color{OliveGreen},
% Define TYPE-1 Keywords
keywords=[1]{
int, char,float, double, unsigned, signed,
goto},
% Define TYPE-2 Keywords
keywordstyle=[2]\bfseries\color{Violet},
keywords=[2]{
%s, %d,
include, define},
% Define TYPE-3 Keywords
keywordstyle=[3]\bfseries\color{Sepia},
keywords=[3]{
return},
commentstyle=\bfseries\color{blue}, % comment style
stringstyle= \color{Magenta!80}, % string literal style
belowcaptionskip = 0.2in, % Space below caption
abovecaptionskip = 0.2in % Space above caption
}
\begin{lstlisting}
#include <stdio.h>
#define SIZE 4
int main()
{
char *strings[SIZE] =
{
"String1",
"String2",
"String3",
"String4"
};
char *ptr_swap; /* A temporary pointer to swap strings */
/* Swap "String2" with "String3" */
ptr_swap = strings [1];
strings [1] = strings [2];
strings [2] = ptr_swap;
printf ("%s %s %s %s", strings[0], strings[1], strings[2], strings[3]);
return 0;
}
\end{lstlisting}
% xxxxxxxxxxxxxxxxxxxxxxxxx Code Snippet ENDS xxxxxxxxxxxxxxxxxxxxxxxx
我的输出如下:
红线下标记的字段与目标的格式不同。
请帮我纠正这些设置,以便我可以获得所需的类似“gvim”的输出。
我如何指定 keywordstyle格式说明符因为,放入:%s,%d->使语句被注释?
谢谢。
答案1
欢迎来到 TeX.SX!请始终添加完全可编译的最小工作示例(MWE)。
我认为你需要这样的东西:
\documentclass{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{xcolor}
\begin{document}
...
\end{document}
现在来看看你的问题。如果你仔细看看listings/lstlang1.sty
,你就会发现,这#include
不是一个keyword
,而是一个directive
。所以你可以说
...
directivestyle=\bfseries\color{Violet},
...
所有这些#...
指令都以紫色显示。
另一个问题是%s
,您可以转义%s
为\%
并给出选项alsoletter={\%}
。但关键字通常不会在字符串中突出显示。因此,这将突出显示自由浮动的%s
,但不会突出显示您示例中的那些,也不会突出显示其他表达式,例如%03d
,这些表达式可以在 中使用printf
。
这个问题也有解决办法,但只是因为listings
软件包中有一个 bug。(另请比较此主题) 您可以给出%s
as otherkeyword
,它将在字符串中突出显示。 Asotherkeyword
自动位于关键字组 1 中,您必须将常规关键字移至另一个组,因为它们应该采用不同的样式。
数字可以按如下方式着色使用列表包时如何更改数字的颜色?。还请注意关于如何在评论中不突出显示的评论。
基本上是<stdio.h>
一个字符串。因此,您可以将<
和>
作为新的字符串分隔符。但请注意,如果它们出现在其他地方,则必须对其进行转义。
我现在无法说如何突出显示后面的常量#define
。SIZE
(如果可能的话)
总的来说,您将获得以下结果:
\documentclass{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{xcolor}
\def\digitcolor{\color{Magenta!80}}
\begin{document}
\lstset{
language=C, % choose the language of the code
stepnumber=1, % the step between two line-numbers. If it's 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}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
tabsize=4, % sets default tabsize to 2 spaces
captionpos=t, % sets the caption-position to top
breaklines=true, % sets automatic line breaking
breakatwhitespace=true, % sets if automatic breaks should only happen at whitespace
% title=\lstname, % show the filename of files included with \lstinputlisting;
identifierstyle=\color{black},
caption={Array of Pointers to Strings},
frame=lrtb,
% Define TYPE-1 Keywords
directivestyle=\bfseries\color{Violet},
keywords={},
otherkeywords={\%s, \%d},
keywordstyle=\bfseries\color{Violet},
keywordstyle=[2]\bfseries\color{OliveGreen},
% Define TYPE-2 Keywords
keywords=[2]{
auto,break,case,char,const,continue,default,do,double,%
else,enum,extern,float,for,goto,if,int,long,register,%
short,signed,sizeof,static,struct,switch,typedef,union,unsigned,%
void,volatile,while},
% int, char,float, double, unsigned, signed,
% goto},
% Define TYPE-3 Keywords
keywordstyle=[3]\bfseries\color{Sepia},
keywords=[3]{
return},
literate=*%
{0}{{{\digitcolor0}}}1
{1}{{{\digitcolor1}}}1
{2}{{{\digitcolor2}}}1
{3}{{{\digitcolor3}}}1
{4}{{{\digitcolor4}}}1
{5}{{{\digitcolor5}}}1
{6}{{{\digitcolor6}}}1
{7}{{{\digitcolor7}}}1
{8}{{{\digitcolor8}}}1
{9}{{{\digitcolor9}}}1,
commentstyle=\bfseries\color{blue}, % comment style
morestring=[b]{<},
morestring=[b]{>},
stringstyle= \color{Magenta!80}, % string literal style
belowcaptionskip = 0.2in, % Space below caption
abovecaptionskip = 0.2in % Space above caption
}
\begin{lstlisting}
#include <stdio.h>
#define SIZE 4
int main()
{
char *strings[SIZE] =
{
"String1",
"String2",
"String3",
"String4"
};
char *ptr_swap; /* A temporary pointer to swap strings */
/* Swap "String2" with "String3" */
ptr_swap = strings [1];
strings [1] = strings [2];
strings [2] = ptr_swap;
printf ("%s %s %s %s", strings[0], strings[1], strings[2], strings[3]);
return 0;
}
\end{lstlisting}
\end{document}