15

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.

Khaaba
  • 1,848
  • 1
  • 22
  • 34
  • 1
    A real MWE would be `\documentclass{article} \usepackage{listings} \usepackage{lstlinebgrd}

    \begin{document} \begin{lstlisting} Lorem \end{lstlisting} \end{document}.listingsrecently changed some internals, maybelstlinebgrd` 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
    https://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

1 Answers1

20

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. (edit: Reported at https://bitbucket.org/martin_scharrer/lstaddons/issues/2/lstlinebgrd-broken-with-recent-update-of)

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}
moewe
  • 175,683
  • 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
  • 1
    @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 https://tex.stackexchange.com/a/371424/35864). – moewe Sep 19 '18 at 13:17
  • Sorry I could not give you an example sooner. However, both (one reference in the question itself in article class and my MWE in beamer) give the same error whether I supress your macro. – Khaaba Sep 19 '18 at 17:13
  • 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 05:48
  • 2
    @Khaaba OK. I found https://tex.stackexchange.com/q/30877/35864. If I compile https://gist.github.com/moewew/f6f9e42275b18ba7c0c1680ced001603 it works brilliantly for me. – moewe Sep 20 '18 at 06:12
  • Thanks. I got it. I was thinking yesterday in a similar line! – Khaaba Sep 20 '18 at 06:47
  • I forgot to add earlier. The key/option, backgroundcolor, of original lstlisting fails again. Any work around for this too? – Khaaba Feb 24 '19 at 03:01
  • 1
    @Khaaba Contact the author and if you need a workaround ask a new question – moewe Feb 24 '19 at 06:32
  • I have put up a new question for workaround at here – Khaaba Feb 25 '19 at 02:33
  • 2
    This is now affecting me under Cygwin. The answer from @moewe works: edit C:\cygwin64\usr\share\texmf-dist\tex\latex\lstaddons\lstlinebgrd.sty by changing the three ampersands (&) to colons (:). I found them after none, left and right. – user4417 Sep 03 '19 at 10:00
  • Thank you for reporting this upstream, but I’m not sure Martin Scharrer is still around. His Bitbucket page is empty now. – Franklin Yu Sep 07 '21 at 08:07
  • They broke this as well: https://tex.stackexchange.com/questions/51825/position-tikz-pictures-at-left-margin-of-a-listing-code-block (identical issue) – blahblahblahdev Aug 04 '22 at 06:44
  • Why does locate lstlinebgrd.sty not work for me on Debian? (I found it anyway, at /usr/share/texlive/texmf-dist/tex/latex/lstaddons/lstlinebgrd.sty.) – user4417 Sep 30 '23 at 11:03
  • 1
    @user4417 Not sure about the exact behaviour of locate, but for system files that have to do with TeX I usually use kpsewhich to find their location. That is the library that is used internally by most TeX tools, so you can usually be sure that the file that is found is the actual file used by TeX and friends. – moewe Sep 30 '23 at 16:04