0

This question is in continuation of the earlier question in which it has been modified to include background color along with linebackground color in few lines of code. MWE is given below:

\documentclass{beamer}

\usepackage{listings, pgffor}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % \btIfInRange{number}{range list}{TRUE}{FALSE}
  %
  % Test if int number <number> is element of a (comma separated) list of ranges
  % (such as: {1,3-5,7,10-12,14}) and processes <TRUE> or <FALSE> respectively

\newcount\bt@rangea
\newcount\bt@rangeb

\newcommand\btIfInRange[2]{%
    \global\let\bt@inrange\@secondoftwo%
    \edef\bt@rangelist{#2}%
    \foreach \range in \bt@rangelist {%
        \afterassignment\bt@getrangeb%
        \bt@rangea=0\range\relax%
        \pgfmathtruncatemacro\result{ ( #1 >= \bt@rangea) && (#1 <= \bt@rangeb) }%
        \ifnum\result=1\relax%
            \breakforeach%
            \global\let\bt@inrange\@firstoftwo%
        \fi%
    }%
    \bt@inrange%
}%
\newcommand\bt@getrangeb{%
    \@ifnextchar\relax%
        {\bt@rangeb=\bt@rangea}%
        {\@getrangeb}%
}%
\def\@getrangeb-#1\relax{%
    \ifx\relax#1\relax%
        \bt@rangeb=100000%   \maxdimen is too large for pgfmath
    \else%
        \bt@rangeb=#1\relax%
    \fi%
}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % \btLstHL<overlay spec>{range list}
  %
\newcommand<>{\btLstHL}[1]{%
    \only#2{\btIfInRange{\value{lstnumber}}{#1}{\color{blue!30}}{}}%
}%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % \btInputEmph<overlay spec>[listing options]{range list}{file name}
  %
\newcommand<>{\btLstInputEmph}[3][\empty]{%                                                    
    \only#4{%
        \lstset{linebackgroundcolor=\btLstHL{#2}}%
        \lstinputlisting{#3}%
    }% \only
}%
\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=4, 
%   linebackgroundcolor={%
%       \btLstHL<1>{1,3,5-6}%
%       \btLstHL<2>{4}%
%   },
    backgroundcolor=\color{gray!10}
]
    #include <stdio.h>

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

This MWE [except backgroundcolor key] was provided by @moewe. Now when I use both keys: linebackgroundcolor and backgroundcolor, color option of backgroundcolor stops working. I tried contacting the author of the package but not succeeded. Any suggestion for work around please.

1

The background colour works fine in principle, the problem is that highlighting macro you use fill not only lines to highlight, it fill all other lines with white and thus covers up the gray background.

As a workaround, you can adjust the \btLstHL as in the following example:

\documentclass{beamer}

\usepackage{listings, pgffor}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % \btIfInRange{number}{range list}{TRUE}{FALSE}
  %
  % Test if int number <number> is element of a (comma separated) list of ranges
  % (such as: {1,3-5,7,10-12,14}) and processes <TRUE> or <FALSE> respectively

\newcount\bt@rangea
\newcount\bt@rangeb

\newcommand\btIfInRange[2]{%
    \global\let\bt@inrange\@secondoftwo%
    \edef\bt@rangelist{#2}%
    \foreach \range in \bt@rangelist {%
        \afterassignment\bt@getrangeb%
        \bt@rangea=0\range\relax%
        \pgfmathtruncatemacro\result{ ( #1 >= \bt@rangea) && (#1 <= \bt@rangeb) }%
        \ifnum\result=1\relax%
            \breakforeach%
            \global\let\bt@inrange\@firstoftwo%
        \fi%
    }%
    \bt@inrange%
}%
\newcommand\bt@getrangeb{%
    \@ifnextchar\relax%
        {\bt@rangeb=\bt@rangea}%
        {\@getrangeb}%
}%
\def\@getrangeb-#1\relax{%
    \ifx\relax#1\relax%
        \bt@rangeb=100000%   \maxdimen is too large for pgfmath
    \else%
        \bt@rangeb=#1\relax%
    \fi%
}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % \btLstHL<overlay spec>{range list}
  %
\newcommand<>{\btLstHL}[1]{%
    \only#2{\btIfInRange{\value{lstnumber}}{#1}{\color{blue!30}}{\color{gray!20}}}%
}%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %
  % \btInputEmph<overlay spec>[listing options]{range list}{file name}
  %
\newcommand<>{\btLstInputEmph}[3][\empty]{%                                                    
    \only#4{%
        \lstset{linebackgroundcolor=\btLstHL{#2}}%
        \lstinputlisting{#3}%
    }% \only
}%
\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=4, 
   linebackgroundcolor={%
       \btLstHL<1>{1,3,5-6}%
       \btLstHL<2>{4}%
   },
    backgroundcolor=\color{gray!20}
]
    #include <stdio.h>

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

enter image description here

2
  • this solution stops working with the recent update again :-(
    – Khaaba
    Oct 31 '19 at 13:28
  • Sorry, my bad! It STILL works in Beamer but the same doesn't work on book class. The error message says [log is long and I don't how to share] Missing control sequence inserted. <inserted text> \inaccessible l.190 \newcommand<> {\btLstHL}[1]{%
    – Khaaba
    Oct 31 '19 at 13:50

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.