3

I could run the program without any problem while highlighting few lines of code using listings and lstlinebgrd packages. But the recent update of listings package v1.7 is giving me error even on the earlier working programs. For example, this program here. I am giving a similar program, but this time, in beamer.

\documentclass{beamer}

\usepackage{xcolor,listings, lstlinebgrd}

%   \makeatletter
%   \let\old@lstKV@SwitchCases\lstKV@SwitchCases
%   \def\lstKV@SwitchCases#1#2#3{}
%   \makeatother
%   \usepackage{lstlinebgrd}
%   \makeatletter
%   \let\lstKV@SwitchCases\old@lstKV@SwitchCases
%   
%   \lst@Key{numbers}{none}{%
%       \def\lst@PlaceNumber{\lst@linebgrd}%
%       \lstKV@SwitchCases{#1}%
%       {none:\\%
%        left:\def\lst@PlaceNumber{\llap{\normalfont
%                   \lst@numberstyle{\thelstnumber}\kern\lst@numbersep}\lst@linebgrd}\\%
%        right:\def\lst@PlaceNumber{\rlap{\normalfont
%                   \kern\linewidth \kern\lst@numbersep
%                   \lst@numberstyle{\thelstnumber}}\lst@linebgrd}%
%       }{\PackageError{Listings}{Numbers #1 unknown}\@ehc}}
%   \makeatother

\begin{document}

\begin{frame}[fragile]{Problem}
Test programme:
\begin{lstlisting}[language=C, gobble=6, linebackgroundcolor={%
    \btLstHL<1>{1,3,5-6}%
    \btLstHL<2>{4}%
}]
    #include <stdio.h>

    int main(void) {%
    printf("Hello World!");  
        return 0;
    }
\end{lstlisting}
\end{frame}
\end{document}

Both give same error as

! Package Listings Error: Numbers none unknown.

See the Listings package documentation for explanation.
Type  H <return>  for immediate help.
...                                              

l.36 ...Error{Listings}{Numbers #1 unknown}\@ehc}}

Is there any work around? May be the lstlinebgrd package author can give me some work around in case if there is no solution at the moment. I am sorry for reporting/posting issues with recent updates.

  • 1
    A real MWE would be \documentclass{article} \usepackage{listings} \usepackage{lstlinebgrd} \begin{document} \begin{lstlisting} Lorem \end{lstlisting} \end{document}. listings recently changed some internals, maybe lstlinebgrd relied on them and broke. – moewe Sep 19 '18 at 12:35
  • Yes, I believe so. lstlinebgrd was created from this post here. lstlisting works fine without lstlinebgrd package, however, I would like to have few lines of code highlighted which I used to achieve using lstlinebgrd package earlier. – Khaaba Sep 19 '18 at 12:43
  • 1
    tug.org/pipermail/tex-live-commits/2018-September/007278.html shows that listings moved from & to : in \lstKV@SwitchCases. This is what lstlinebgrd should do as well. Contact the package maintainer. – moewe Sep 19 '18 at 12:44
4

Recently (https://tug.org/pipermail/tex-live-commits/2018-September/007278.html) listings switched the internal separator in the macro \lstKV@SwitchCases from & to :. According to the comments in the source that had to happen because of adding frame=none to lstlisting options in \lstdefinestyle gives error with tabular.

You should contact the maintainer of lstlinebgrd via the official channels mentioned in the documentation and ask him to update his package.

In the meantime here is a very crude workaround that fixes the definition of the affected \lst@Key{numbers}. This should restore lstlinebgrd to its former glory, but it is not very elegant because it essentially ends up copying the definition of \lst@Key{numbers} from lstlinebgrd.sty.

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\makeatletter
\let\old@lstKV@SwitchCases\lstKV@SwitchCases
\def\lstKV@SwitchCases#1#2#3{}
\makeatother
\usepackage{lstlinebgrd}
\makeatletter
\let\lstKV@SwitchCases\old@lstKV@SwitchCases

\lst@Key{numbers}{none}{%
    \def\lst@PlaceNumber{\lst@linebgrd}%
    \lstKV@SwitchCases{#1}%
    {none:\\%
     left:\def\lst@PlaceNumber{\llap{\normalfont
                \lst@numberstyle{\thelstnumber}\kern\lst@numbersep}\lst@linebgrd}\\%
     right:\def\lst@PlaceNumber{\rlap{\normalfont
                \kern\linewidth \kern\lst@numbersep
                \lst@numberstyle{\thelstnumber}}\lst@linebgrd}%
    }{\PackageError{Listings}{Numbers #1 unknown}\@ehc}}
\makeatother

\begin{document}
\begin{lstlisting}[linebackgroundcolor={\ifodd\value{lstnumber}\color{lightgray}\fi}]
Lorem
ipsum
dolor
\end{lstlisting}
\end{document}
  • thanks for the tips. I knew zebra works without lstlinebgrd package. However, I am trying to highlight few specific lines of code elsewhere instead of the entire code. It was working earlier before that commit you've highlighted though. – Khaaba Sep 19 '18 at 13:06
  • @Khaaba Well, the code here should actually fix the lstlinebgrd issue. So whatever worked before with lstlinebgrd should work with the fix. linebackgroundcolor specifically needs lstlinebgrd and so I thought that sufficient to show that my code indeed fixes lstlinebgrd. If that does not work for you, please add a MWE to your question. (In fact I just checked that my fix also works with the code from tex.stackexchange.com/a/371424/35864). – moewe Sep 19 '18 at 13:17
  • 1
    @Khaaba Do not load lstlinebgrd (in \usepackage{xcolor,listings, lstlinebgrd}) before you apply the fix. If you do that you already get an error. Load \usepackage{xcolor,listings} and then apply the fix, it will load lstlinebgrd for you. I can't show you that the code in your question does work, because on my machine the command \btLstHL is unknown. But that is unrelated to lstlinebgrd. – moewe Sep 20 '18 at 5:48
  • 1
    @Khaaba OK. I found tex.stackexchange.com/q/30877/35864. If I compile gist.github.com/moewew/f6f9e42275b18ba7c0c1680ced001603 it works brilliantly for me. – moewe Sep 20 '18 at 6:12
  • 1
    @Khaaba Contact the author and if you need a workaround ask a new question – moewe Feb 24 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.