答案1
像这样?您可以根据自己的喜好修改颜色。有关特定关键字的更多详细信息,请参阅清单手册。
\documentclass[11pt]{article}
\usepackage{xcolor}
\usepackage{listings}
\lstdefinestyle{mystyle}
{
language=C++,
basicstyle=\ttfamily,
commentstyle=\color{green},
keywordstyle=\color{blue},
stringstyle=\color{red},
%basewidth={0.5em,0.5em},
breakatwhitespace=false,
breaklines=true,
captionpos=t,
frame=single,
framerule=1pt,
columns=flexible,
keepspaces=true,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=4,
morekeywords={[2]{uint32_t}},
keywordstyle={[2]\color{magenta}},
}
\begin{document}
\begin{lstlisting}[style=mystyle]
static void func(void)
{
if(condition1) {
printf("Statement 1");
} else {
uint32_t var = value;
printf("Statement 2");
}
}
\end{lstlisting}
\end{document}
输出:
编辑
要添加行号,请使用这些设置。
\documentclass[11pt]{article}
\usepackage{xcolor}
\usepackage{listings}
\lstdefinestyle{mystyle}
{
language=C++,
basicstyle=\ttfamily,
commentstyle=\color{green},
keywordstyle=\color{blue},
stringstyle=\color{red},
%basewidth={0.5em,0.5em},
breakatwhitespace=false,
breaklines=true,
captionpos=t,
frame=single,
framerule=1pt,
columns=flexible,
keepspaces=true,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=4,
morekeywords={[2]{uint32_t}},
keywordstyle={[2]\color{magenta}},
numberstyle=\sffamily\footnotesize\color{gray},
numbers=left,
numbersep=10pt,
}
\begin{document}
\begin{lstlisting}[style=mystyle]
static void func(void)
{
if(condition1) {
printf("Statement 1");
} else {
uint32_t var = value;
printf("Statement 2");
}
}
\end{lstlisting}
\end{document}
输出: