我正在使用listings
它来显示代码并且我添加了firstnumber
但caption
它似乎不起作用。
有一个例子:
\documentclass[11pt,fleqn]{book} % Default font size and left-justified equations
\usepackage{epstopdf}
\usepackage{verbatim}
\usepackage[dvipsnames]{xcolor} % Required for specifying colors by name
\usepackage[utf8]{inputenc} % Required for including letters with accents
\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage{csquotes}
\usepackage{listings}
\lstdefinestyle{styleC}
{
language= C,
commentstyle = {\color{ForestGreen}},
stringstyle = {\color{NavyBlue}},
keywordstyle = {\color{RedViolet}},
keywordstyle = [2]{\color{OliveGreen}},
keywordstyle = [3]{\color{Fuchsia}},
keywordstyle = [4]{\color{BlueViolet} \itshape}, %poner italic
keywordstyle = [5]{\color{orange}},
basicstyle = {\ttfamily \color{black}},
backgroundcolor = {\color{white}},
sensitive = false,
breaklines = true,
showstringspaces= false,
showspaces= false,
extendedchars= true,
keywords = {for, typeof, new, true, false, function, return, null, switch, var, if, in, while, do, else, case, break, class, export, boolean, throw, implements, import, this, constructor, string, number, public, private, static, const, var, let, void},
morekeywords = [2]{uint32_t, float32_t, arm_matrix_instance_f32, uint8_t, uint16_t, arm_status},
morekeywords = [3]{arm_rfft_fast_instance_f32, arm_mat_scale_f32, arm_mat_init_f32, arm_mat_mult_f32, arm_mat_add_f32, arm_cmplx_mag_f32, arm_rfft_fast_f32, exp, log10, sprintf},
morekeywords = [4]{ARM_MATH_SUCCESS},
otherkeywords = {;},
comment = [l]{//},
morecomment = [s]{/*}{*/},
morestring = [b]',
morestring = [b]",
}
\lstnewenvironment{Ce}
{\lstset{
style=styleC,
frame=single,
numbers=left, numberstyle=\normalfont\color{gray}, numbersep=5pt
}
}
{}
\begin{document}
\chapter{C code}
In this chapter only the edited code and generated code is shown.
\begin{Ce}[caption=Example 1, firstnumber=7]
blah
blah
blah
\end{Ce}
In the next box, the modified function \textit{makeRecording()} is exposed.
\begin{Ce}[caption=Example 2, firstnumber=100]
blah
blah
blah
\end{Ce}
To debug the system, all needed information was saved as a text file. The code is in the next box.
\begin{Ce}[caption=SD code, numbers=none]
blah
blah
blah
\end{Ce}
\end{document}
但我得到的是三个列表,所有行号都从 1 开始。并且没有标题。
我不知道是否必须在 中定义它们\lstset
,但我必须\lstnewenvironment
为每个框创建不同的内容。有什么解决办法吗?
提前致谢!
注意:我还有一个问题。 中方括号内的数字是什么意思\lstnewenvironment{python}[1][]
?我在列表包文档中没有找到有关此信息。
答案1
感谢@daleif,我解决了这个问题。
声明环境时,需要包含[]
所需参数的数量(数字、第一个数字和标题):
\lstnewenvironment{Ce}[3]
{\lstset{
style=styleC,
frame=single,
numbers=#1,
numberstyle=\normalfont\color{gray}, numbersep=5pt,
firstnumber = #2,
caption = #3
}
}
{}
然后,在文档中:
\begin{Ce}{left}{100}{Example}
blah
blah
blah
\end{Ce}