0

I am using listings to display code and I added firstnumber and caption but it doesn't appears to work.

There's an example:

\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}

But what I obtain are three listings, all with line numbers starting at 1. And no captions.

enter image description here I don't know if I have to define those in \lstset, but I would have to create differents \lstnewenvironment for each box. Is there any solution?

Thanks in advance!

Note: I have an additional question. What does the number in square brackets means in \lstnewenvironment{python}[1][]? I didn't found information about that in the listing package documentation.

4
  • 1
    Welcome to TeX.SX! Would you please make the two code snippets a whole and complete example, which should start with \documentclass and end with \end{document}. Commented Aug 19, 2020 at 13:34
  • That is the number of arguments for the environment, the [] then indicates that this is an optional argument in square brackets. You'll probably have to add , #1 to the \lstset in the Ce definition, but I'd also use [1][] like in the python example
    – daleif
    Commented Aug 19, 2020 at 18:37
  • @daleif thank you very much! You're right. But when I add [Ce][3][] it gives me an error. I had to remove the second square brackets [Ce][3] to make it works. I've seen people using both brackets with no issues.
    – car00
    Commented Aug 20, 2020 at 12:32
  • Ce goes on {Ce} not [Ce]
    – daleif
    Commented Aug 20, 2020 at 12:53

1 Answer 1

0

Thanks to @daleif I solved the issue.

When declaring the enviroment, it is needed to include [] with the number of desired arguments (numbers, firstnumber and caption):

\lstnewenvironment{Ce}[3]
  {\lstset{
    style=styleC,
    frame=single,
    numbers=#1, 
    numberstyle=\normalfont\color{gray}, numbersep=5pt,
    firstnumber = #2,
    caption = #3
    }
  }
  {}

And then, in the document:

\begin{Ce}{left}{100}{Example}
blah
blah
blah
\end{Ce}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .