lstlinebgrd 套件更新後的清單的最新問題

lstlinebgrd 套件更新後的清單的最新問題

這個問題是之前的問題的延續問題其中它已被修改為linebackground在幾行程式碼中包含背景顏色和顏色。 MWE 如下:

\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}

此 MWE [背景顏色鍵除外] 提供者@moewe。現在,當我使用兩個鍵:linebackgroundcolor和 時backgroundcolor,顏色選項backgroundcolor停止工作。我嘗試聯繫該軟體包的作者,但沒有成功。請提供任何解決方法的建議。

答案1

背景顏色原則上運作正常,問題是您使用的突出顯示巨集不僅填滿線條來突出顯示,它還會用白色填滿所有其他線條,從而掩蓋灰色背景。

作為解決方法,您可以調整\btLstHL如下例所示:

\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}

在此輸入影像描述

相關內容