我正在用列表重现 C 代码风格。我已将其定义""
为字符串,因此当我添加库时,"ff.h"
它会被视为字符串并打印为蓝色。为了避免这种情况,我使用literate
。
例如:
\documentclass[11pt,fleqn]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{listings}
\lstdefinestyle{styleC}{
language = C,
commentstyle = {\color{ForestGreen}},
stringstyle = {\color{NavyBlue}},
comment = [l]{//},
morecomment = [s]{/*}{*/},
morestring = [b]',
morestring = [b]",
}
\lstnewenvironment{C}{
\lstset{
style=styleC,
frame=single,
literate = {"ff.h"}{"ff.h"}6 {"math.h"}{"math.h"}8
}
}
{}
\begin{document}
\begin{C}
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <complex.h>
#include "ff.h"
#include "ffconf.h"
#include "math.h"
#include "arm_math.h"
#include "audioMoth.h"
\end{C}
\end{document}
但是当我将其应用到所有库时:
literate = {"ff.h"}{"ff.h"}6 {"ffconf.h"}{"ffconf.h"}10 {"math.h"}{"math.h"}8 {"arm_math.h"}{"arm_math.h"}12 {"audioMoth.h"}{"audioMoth.h"}13
结果如下:
会发生什么事?
答案1
如果数字大于 9,则必须使用括号括起来:
\documentclass[11pt,fleqn]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{listings}
\lstdefinestyle{styleC}{
language = C,
commentstyle = {\color{ForestGreen}},
stringstyle = {\color{NavyBlue}},
comment = [l]{//},
morecomment = [s]{/*}{*/},
morestring = [b]',
morestring = [b]",
}
\lstnewenvironment{C}{
\lstset{
style=styleC,
frame=single,
%literate = {"ff.h"}{"ff.h"}6 {"math.h"}{"math.h"}8
literate = {"ff.h"}{"ff.h"}6 {"ffconf.h"}{"ffconf.h"}{10} {"math.h"}{"math.h"}8 {"arm_math.h"}{"arm\_math.h"}{12} {"audioMoth.h"}{"audioMoth.h"}{13}
}
}
{}
\begin{document}
\begin{C}
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <complex.h>
#include "ff.h"
#include "ffconf.h"
#include "math.h"
#include "arm_math.h"
#include "audioMoth.h"
\end{C}
\end{document}