我一直在尝试使用该listings
包。问题是我不知道如何为不同的关键词添加不同的颜色。
使用我的代码,我只能将某些关键字设置为特定颜色,例如当我使用“LOW”、“HIGH”、“int”或“float”时,但我想在使用“begin”、“pinMode”或“setCursor”等关键字时获得不同的颜色。我该怎么做?
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset
{
frame=single,
framesep=\fboxsep,
framerule=\fboxrule,
rulecolor=\color{red},
xleftmargin=\dimexpr\fboxsep+\fboxrule,
xrightmargin=\dimexpr\fboxsep+\fboxrule,
breaklines=true,
basicstyle=\small\tt,
keywordstyle=\color{cyan}\sf,
identifierstyle=\color{black},
commentstyle=\color{cyan},
backgroundcolor=\color{yellow!10},
tabsize=2,
columns=flexible,
morekeywords={LOW,HIGH,int,float,const,void,OUTPUT},
}
\begin{document}
\begin{lstlisting}
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,6,5,4,3,2);
int SENSOR;
float TEMPERATURA;
float SUMA;
const int MOTOR=13;
void setup() {
lcd.begin(16,2);
pinMode(MOTOR, OUTPUT);
lcd.setCursor(1,0);
lcd.print("COOLING SYSTEM");
delay(3000);
lcd.clear();
}
void loop() {
SUMA = 0;
for (int i=0; i < 5; i++){
SENSOR = analogRead(A0);
TEMPERATURA = ((SENSOR*5000.0)/1023)/10;
SUMA = TEMPERATURA + SUMA;
delay(100);
}
if (TEMPERATURA > 35 ){
lcd.clear();
digitalWrite(13,HIGH);
lcd.setCursor(0,1);
lcd.print("Ventilador ON");
}
else{
lcd.clear();
digitalWrite(13,LOW);
lcd.setCursor(0,1);
lcd.print("Ventilador OFF");
}
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(SUMA/5.0,1);
lcd.print(" C");
}
\end{lstlisting}
\end{document}
这就是我想要实现的目标:
答案1
您可以设置 classoffset 参数来创建不同的突出显示组。
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\definecolor{arduinoOrange}{RGB}{210,83,102}
\definecolor{arduinoGrayGreen}{RGB}{94,109,3}
\lstset
{
frame=single,
framesep=\fboxsep,
framerule=\fboxrule,
rulecolor=\color{red},
xleftmargin=\dimexpr\fboxsep+\fboxrule,
xrightmargin=\dimexpr\fboxsep+\fboxrule,
breaklines=true,
basicstyle=\small\tt,
keywordstyle=\color{cyan}\sf,
identifierstyle=\color{black},
commentstyle=\color{cyan},
backgroundcolor=\color{yellow!10},
tabsize=2,
columns=flexible,
morekeywords={LOW,HIGH,int,float,const,void,OUTPUT},
classoffset=5,
morekeywords={
LiquidCrystal,begin,pinMode,setCursor,delay,print,
clear, analogRead, delay, digitalWrite
},
keywordstyle={\color{arduinoOrange}},
classoffset=6,
otherkeywords={\#include, >, <},
morekeywords={\#include,setup,loop,for,if, >, <},
keywordstyle={\color{arduinoGrayGreen}}
}
\begin{document}
\begin{lstlisting}
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,6,5,4,3,2);
int SENSOR;
float TEMPERATURA;
float SUMA;
const int MOTOR=13;
void setup() {
lcd.begin(16,2);
pinMode(MOTOR, OUTPUT);
lcd.setCursor(1,0);
lcd.print("COOLING SYSTEM");
delay(3000);
lcd.clear();
}
void loop() {
SUMA = 0;
for (int i=0; i < 5; i++){
SENSOR = analogRead(A0);
TEMPERATURA = ((SENSOR*5000.0)/1023)/10;
SUMA = TEMPERATURA + SUMA;
delay(100);
}
if (TEMPERATURA > 35 ){
lcd.clear();
digitalWrite(13,HIGH);
lcd.setCursor(0,1);
lcd.print("Ventilador ON");
}
else{
lcd.clear();
digitalWrite(13,LOW);
lcd.setCursor(0,1);
lcd.print("Ventilador OFF");
}
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(SUMA/5.0,1);
lcd.print(" C");
}
\end{lstlisting}
\end{document}
这回答了你的问题了吗?