我正在尝试在文档中添加java
和代码。我正在使用包,我将使用为每种语言定义不同的样式。我试图实现的是编辑器使用的颜色和强调,如下图所示c++
LaTeX
listings
\lstdefinestyle{myStyle}{<options>}
Kate
例如,查看注释的不同样式。注释以 开头时//
为灰色,而注释被 包围时/**comment*/
为绿色。
或者也许是关键词的不同颜色:int
是深蓝色,而static final int
是较亮的。
还要看一下数字:它们是黄色的。或者例如琴弦是红色的。我曾尝试重现这些,但我无法做到这一点(数字)或使用的紫色方法。
例如,我无法为不同的注释或关键词定义不同的颜色。我无法为所有数字和方法着色get***
。我的代码是
\documentclass{article}
\usepackage{listings}
\usepackage{textcomp}
\usepackage{xcolor}
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\lstdefinestyle{JavaStyle}{
backgroundcolor=\color{lbcolor},
tabsize=4,
% rulecolor=,
language=Java, % choose the language of the code
basicstyle=\scriptsize, % the size of the fonts that are used for the code
upquote=true,
aboveskip={1.5\baselineskip},
columns=fixed,
showstringspaces=false,
extendedchars=false,
breaklines=true,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=single,
numbers=left,
showtabs=false,
showspaces=false,
showstringspaces=false,
identifierstyle=\ttfamily,
keywordstyle=\color[rgb]{0,0,1},
%commentstyle=\color[rgb]{0.026,0.112,0.095},
commentstyle=\itshape\color{green!40!black},
stringstyle=\itshape\color{red!90!black},
numberstyle=\itshape\color{yellow!50!black}
}
\begin{document}
\begin{lstlisting}[style=JavaStyle]
int[] ones = {1,1};
int points = 10;
int[] region = new int[points];
int timeTicks;
int totalTime;
int LiveTime = 0;
int totcount;
int evType = 1; //The Type ID for these kinds of events
SimpleDateFormat today = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat now = new SimpleDateFormat("HH:mm:ss:SSS");
Map<String,KmaxHist> histograms = new HashMap<String,KmaxHist>();
static final int evSize = 6;//The num of parameters per event of this type
static final int BUF_SIZE = evSize*1000;// Buffer size
static final int LP_MEM_TOP = 0xFFFF00;//Memory size 16MB
static final int READ_START = LP_MEM_TOP - BUF_SIZE;//We start the read/write pointer 1 buffer before the end
int[] blkData = new int[BUF_SIZE];
/**
* The 'init' method is executed at compile time.
*/
public void init(KmaxToolsheet toolsheet) {
tlsh = toolsheet;//Save this reference for use in the toolsheet
dev = tlsh.getKmaxDevice("DEV1");
dataField = tlsh.getKmaxWidget("$R_DATA");
countField = tlsh.getKmaxWidget("$R_COUNT");
liveTime = tlsh.getKmaxWidget("LIVE_TIME");//Live Time
realTime = tlsh.getKmaxWidget("REAL_TIME");//Real Time
deadTime = tlsh.getKmaxWidget("DEAD_TIME");//Dead Time
hist1 = tlsh.getKmaxHist("DATA1");
hist2 = tlsh.getKmaxHist("DATA2");
hist3 = tlsh.getKmaxHist("DATA3");
hist4 = tlsh.getKmaxHist("DATA4");
hist5 = tlsh.getKmaxHist("DATA5");
hist6 = tlsh.getKmaxHist("DATA6");
histoLowX = tlsh.getKmaxHist("HIST_LOW_X");
histoLowY = tlsh.getKmaxHist("HIST_LOW_Y");
histograms.put("DATA1", hist1);
histograms.put("DATA2", hist2);
histograms.put("DATA3", hist3);
\end{lstlisting}
\end{document}
输出为
关于如何重现第一张图片中的代码,您有什么想法吗?
答案1
关于如何重现第一张图片中的代码,您有什么想法吗?
是的,我有一些想法,请参见下文。
然而,以健壮的方式突出显示数字(包括十六进制表达式,例如0xFFFF00
)虽然并非不可能,但却非常困难。如果你决心实施解决方案,你可以使用我的答案如何突出显示紧跟等号的所有形式为 [0-9][A-Za-z0-9]* 的单词?作为起点。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.85]{beramono}
\usepackage{listings}
\usepackage{textcomp}
\usepackage{xcolor}
\usepackage{lstautodedent}
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\lstdefinestyle{JavaStyle}{
language=Java, % choose the language of the code
deletekeywords={new,public},
keywords=[2]{HashMap,Map,SimpleDateFormat,String},
keywords=[3]{getKmaxDevice,getKmaxWidget,getKmaxHist,init},
basicstyle=\scriptsize\ttfamily,
keywordstyle=\color[RGB]{69,97,189},
keywordstyle=[2]{\color{cyan}},
keywordstyle=[3]\color[RGB]{137,77,155},
commentstyle=\itshape\color{green!60!black},
moredelim=[l][\itshape\color{gray}]{//}, %<--- overrides line-comment style
stringstyle=\color[RGB]{192,8,8},
numberstyle=\itshape\color{yellow!50!black},
% backgroundcolor=\color{lbcolor},
tabsize=4,
% rulecolor=,
upquote=true,
aboveskip={1.5\baselineskip},
columns=fixed,
showstringspaces=false,
extendedchars=false,
breaklines=true,
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
frame=single,
numbers=left,
showtabs=false,
showspaces=false,
showstringspaces=false,
autodedent,%<--- removes indentation
}
\begin{document}
\begin{lstlisting}[style=JavaStyle]
int[] ones = {1,1};
int points = 10;
int[] region = new int[points];
int timeTicks;
int totalTime;
int LiveTime = 0;
int totcount;
int evType = 1; //The Type ID int for these kinds of events
SimpleDateFormat today = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat now = new SimpleDateFormat("HH:mm:ss:SSS");
Map<String,KmaxHist> histograms = new HashMap<String,KmaxHist>();
static final int evSize = 6;//The num of parameters per event of this type
static final int BUF_SIZE = evSize*1000;// Buffer size
static final int LP_MEM_TOP = 0xFFFF00;//Memory size 16MB
static final int READ_START = LP_MEM_TOP - BUF_SIZE;//We start the read/write pointer 1 buffer before the end
int[] blkData = new int[BUF_SIZE];
/**
* The 'init' method is executed at compile time.
*/
public void init(KmaxToolsheet toolsheet) {
tlsh = toolsheet;//Save this reference for use in the toolsheet
dev = tlsh.getKmaxDevice("DEV1");
dataField = tlsh.getKmaxWidget("$R_DATA");
countField = tlsh.getKmaxWidget("$R_COUNT");
liveTime = tlsh.getKmaxWidget("LIVE_TIME");//Live Time
realTime = tlsh.getKmaxWidget("REAL_TIME");//Real Time
deadTime = tlsh.getKmaxWidget("DEAD_TIME");//Dead Time
hist1 = tlsh.getKmaxHist("DATA1");
hist2 = tlsh.getKmaxHist("DATA2");
hist3 = tlsh.getKmaxHist("DATA3");
hist4 = tlsh.getKmaxHist("DATA4");
hist5 = tlsh.getKmaxHist("DATA5");
hist6 = tlsh.getKmaxHist("DATA6");
histoLowX = tlsh.getKmaxHist("HIST_LOW_X");
histoLowY = tlsh.getKmaxHist("HIST_LOW_Y");
histograms.put("DATA1", hist1);
histograms.put("DATA2", hist2);
histograms.put("DATA3", hist3);
\end{lstlisting}
\end{document}