
我正在用清單重現 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}