# Issue with firstnumber Option from minted Package

I don't understand why the code in the next MWE does not lead to the Cpp-language lines being numbered starting from 1 in the resulting pdf document (I'm on pdfTeX).

\documentclass{memoir}

\usepackage{xcolor,minted}
\usemintedstyle{friendly}
\newminted{cpp}{
linenos=true,
fontsize=\footnotesize,
fontseries=m,
frame=leftline,
tabsize=4,
firstnumber=1,
stepnumber=5
}
\newmintedfile{cpp}{
linenos=true,
fontsize=\footnotesize,
fontseries=m,
samepage=false,
tabsize=4,
firstnumber=1,
stepnumber=5
}
\newmint{cpp}{
linenos=true,
fontsize=\footnotesize,
fontseries=m,
frame=leftline,
tabsize=4,
firstnumber=1,
stepnumber=5
}
\renewcommand{\theFancyVerbLine}{\tiny\ttfamily%
\textcolor[rgb]{0,0,0}{\arabic{FancyVerbLine}}%
}

\begin{document}
\end{document}


The Cpp-language code in the file Task4.cpp is as follows:

void xchanger(int na, int nb)
{
int naux;
naux = na, na = nb, nb = naux;
printf("Now, numbers are as follows within the function:\n");
printf("Number 1: %d\n", na);
printf("Number 2: %d\n", nb);
return;
}


Anyone out there can replicate this misbehavior?

EDIT: To be true, the code in the Cpp file is larger than that, and I'm typesetting only the above fraction by issuing

\cppfile[linenos=true,firstline=14,lastline=25]{Task4.cpp}


It doesn't seem to make for a difference, though.

• Your code behaves nicely and prints the number of the 5th line. You should set stepnumber=1 if your intention was to have each line numbered. – DG' Sep 10 '13 at 18:58
• @DG': My intention is to have the first line numbered 1 and after that have only one out of each 5 lines numbered. Moreover, in my actual setting (see my EDIT) the first line numbered appears numbered as 2 (it's the second line in the code). – Marcos Sep 10 '13 at 19:02
• I can’t see a way to convince pygments to print the first number, als linenostepis defined as "If set to a number n > 1, only every nth line number is printed." This also seems to be the reason, why the second line of your example is printed: pygments prints the numbers of the lines 5, 10, 15 and so on. You are then skipping the first 14 lines so the first number to be printed is the second of the remaining file. – DG' Sep 10 '13 at 19:52
• With the listings package you could do something like: lstinputlisting[language=C++,numberstyle={\footnotesize},numbers=left,numberfirstline=true,firstnumber=1,firstline=14,lastline=25,stepnumber=5]{Task4.cpp} which would get the numbers right, although all the color would be gone . . . – DG' Sep 10 '13 at 20:19
• @DG': I appreciate your comments and your interest. From them it seems that the only possibility is changing or patching the package's code itself. Maybe that's the appropriate answer to my post, but I'd rather wait a little to see if some answer in the affirmative shows up. – Marcos Sep 10 '13 at 22:05

You can get the first line numbered by patching fancyvrb. Insert the following code in the preamble. I've marked the section I added; it's at the end.

\makeatletter
%% DG modification begin - Dec. 20, 1995
%%\def\FV@Numbers@left{%
%% \def\FV@LeftListNumber{\hbox to\z@{%
%% \hss\theFancyVerbLine\kern\FV@NumberSep}}}
\def\FV@Numbers@left{%
%% DG/SR modification begin - Apr. 28, 1998
\let\FV@RightListNumber\relax
%% DG/SR modification end
\def\FV@LeftListNumber{%
\@tempcnta=\FV@CodeLineNo
\@tempcntb=\FV@CodeLineNo
\divide\@tempcntb\FV@StepNumber
\multiply\@tempcntb\FV@StepNumber
\ifnum\@tempcnta=\@tempcntb
%% DG/SR modification begin - Apr. 28, 1998
%% \hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\if@FV@NumberBlankLines
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\else
\ifx\FV@Line\empty
\else
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\fi
\fi
%% DG/SR modification end
%% Line 1 modification begin
\else
\ifnum\@tempcnta=1
\if@FV@NumberBlankLines
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\else
\ifx\FV@Line\empty
\else
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\fi
\fi
\fi
%% Line 1 modification end
\fi}}
\makeatother


Edit

The original version worked with external files, but only when firstline=1. Here's a modified version that works in all cases. As an added bonus, it always numbers lines that are an exact multiple of stepnumber, rather than multiples with an offset.

\makeatletter
\newcount\FancyVerbOffset
\newcount\@tempcntc
%% DG modification begin - Dec. 20, 1995
%%\def\FV@Numbers@left{%
%% \def\FV@LeftListNumber{\hbox to\z@{%
%% \hss\theFancyVerbLine\kern\FV@NumberSep}}}
\def\FV@Numbers@left{%
%% DG/SR modification begin - Apr. 28, 1998
\let\FV@RightListNumber\relax
%% DG/SR modification end
\def\FV@LeftListNumber{%
\@tempcnta=\FV@CodeLineNo
\@tempcntb=\FV@CodeLineNo
%% Line 1 modification begin
\ifnum\FancyVerbStartNum<\tw@\else
\@tempcntc=\FancyVerbStartNum
\divide\@tempcntc\FV@StepNumber
\multiply\@tempcntc\FV@StepNumber
\FancyVerbOffset=\FancyVerbStartNum
\fi
%% Line 1 modification end
\divide\@tempcntb\FV@StepNumber
\multiply\@tempcntb\FV@StepNumber
\ifnum\@tempcnta=\@tempcntb
%% DG/SR modification begin - Apr. 28, 1998
%% \hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\if@FV@NumberBlankLines
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\else
\ifx\FV@Line\empty
\else
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\fi
\fi
%% DG/SR modification end
%% Line 1 modification begin
\else
\ifnum\c@FancyVerbLine=1
\if@FV@NumberBlankLines
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\else
\ifx\FV@Line\empty
\else
\hbox to\z@{\hss\theFancyVerbLine\kern\FV@NumberSep}%
\fi
\fi
\fi
%% Line 1 modification end
\fi}}
\makeatother

• Your code works alright for minted environments in which the code lines are typed, but not when they are taken from a file like in the EDIT to my OP. Do you have any suggestions for this? – Marcos Sep 13 '13 at 23:26