
リストを使用して 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}