How to get continuos line numbers for minted code blocks

I use minted in following way:

\documentclass[a4paper, 11pt, twoside]{article}
\usepackage{xltxtra}
\usepackage{xunicode}
\usepackage{polyglossia}
\usepackage[svgnames]{xcolor}

% Syntax higlight
\usepackage{minted}
\usemintedstyle{vim}
\newminted{bash}{bgcolor=Beige, fontfamily=tt,
gobble=2, linenos, numbersep=-3pt}
\newmint{bash}{}
\renewcommand{\theFancyVerbLine}{
\sffamily
\textcolor{BlueViolet}{
\scriptsize
\oldstylenums{
\arabic{FancyVerbLine}
}
}
}

\begin{document}

AAAA

\begin{bashcode}
BBBB
CCCC
\end{bashcode}

DDDD

\begin{bashcode}
EEEE
FFFF
\end{bashcode}

GGGG
\end{document}


With this settings I have separate line number for each code block, i.e.

1 AAAA
2 BBBB


and later

1 EEEE
2 FFFF


I'd like to get continuous all the time increasing line numbers, i.e.

1 AAAA
2 BBBB


and later

3 EEEE
4 FFFF


According to fancyvrb documentation I tried to add option firstnumber=last to \newminted definition, but it produces totally weird numbers, e.g. first code block starts with 16 and ends with 32, next starts with 43...

• firstnumber=last doesn't distinguish between different Verbatim based environments. – egreg Dec 17 '15 at 17:30
• I do not have any other Verbatim based environments. Well, I think. Only enumerate, captions and table. – Tilia Dec 18 '15 at 8:22

It looks like you are using minted 1.7 or earlier. Version 2.0 and later includes patches for some fancyvrb bugs. This should make firstnumber=last work correctly. (At least, it works for me.) Also, since 2.0 there is a package option langlinenos that does per-language line numbering when firstnumber=last, so that you can keep bash line numbers separate from any other code.

If this doesn't work for you with minted 2.0+, then there is probably a bug in minted.

• Indeed, in openSUSE repository there is outdated minted package. The newest works fine with the setting. I have requested its update i repository. – Tilia Dec 18 '15 at 8:34
• If I only use bashcode environments, there's no problem. But any Verbatim in between will spoil the numbering, for obvious reasons. You may consider a new option for newminted, say firstnumber=* which would set up a new counter like in my answer and pass firstnumber=\value{<thisnewcounter>} to fancyvrb. – egreg Dec 18 '15 at 9:24
• @egreg The package option langlinenos creates a per-language counter. This is passed to fancyvrb as necessary, and then the original fancyvrb counter value is restored afterward, so that non-minted use of fancyvrb isn't affected. I suppose I could also consider an option to add a counter strictly restricted to a given custom command. – G. Poore Dec 18 '15 at 13:00

The firstnumber=last option doesn't distinguish between different environments based on Verbatim (like bashcode). Thus, if you have other minted environments between two bashcode ones, the numbering will be somewhat unpredictable.

You can set a specific bashnumber counter that's stepped along with bashcode lines and set \firstnumber=\value{bashnumber}.

\documentclass[a4paper, 11pt, twoside]{article}
\usepackage{polyglossia}
\usepackage[svgnames]{xcolor}
\usepackage{minted}
\usepackage{etoolbox}

% fonts
\setmainfont{Latin Modern Roman}
\setsansfont{Latin Modern Sans}
\setmonofont{Latin Modern Mono}

% Syntax higlight

\usemintedstyle{vim}
\newminted{bash}{bgcolor=Beige, fontfamily=tt,
gobble=2, linenos, numbersep=6pt,firstnumber=\value{bashnumber}}
\newmint{bash}{}
\renewcommand{\theFancyVerbLine}{%
\sffamily
\textcolor{BlueViolet}{%
\scriptsize
\oldstylenums{%
\stepcounter{bashnumber}%
\arabic{FancyVerbLine}%
}%
}%
}
\newcounter{bashnumber}
\setcounter{bashnumber}{1}

\begin{document}

AAAA

\begin{bashcode}
BBBB
CCCC
\end{bashcode}

DDDD

\begin{minted}{c}
abc
\end{minted}

XXXXX

\begin{bashcode}
EEEE
FFFF
\end{bashcode}

GGGG
\end{document}


Note the proper formatting of the redefinition of \theFancyVerbLine so the numbersep option can have a decent value.

• This is perfectly working, but I consider it as a workaround as it works as expected with newest version of minted package. For some reason there is outdated version in openSUSE repository... – Tilia Dec 18 '15 at 8:40