在动画中更改列表内的多个属性

在动画中更改列表内的多个属性

目前我正在准备使用 beamer 包进行 C++ 培训。到目前为止一切都很顺利!

从答案到我如何突出显示源代码中的某些行我找到了一个很好的解决方案,即逐步突出显示代码。由于大多数时候代码示例下方没有太多空间,因此我倾向于使用 C++ 注释来解释代码中的某些行。

因此使用来自的代码丹尼尔我设置了 linebackgroundcolor。有没有办法同时设置其他 lstlisting 属性,如 commentstyle?我的想法是,commentstyle 的默认颜色是白色,只有在突出显示的部分我才设置不同的颜色,例如绿色。(我尝试只使用白色,但从我的角度来看,框的橙色和注释的白色之间的对比度太低了)。为了更好地说明,请参见下图。

示例图像

提前谢谢了!

这是我的 Latex 代码摘录。

\documentclass{beamer}

\mode<presentation>
{
  \usetheme{Goettingen}
  \usecolortheme{beaver}
}

\usepackage{multirow}
\usepackage{adjustbox}
\usepackage{listings}
\usepackage{pgf, pgffor}
\usepackage{lstlinebgrd}
\usepackage{caption}

% -----------------------------------------------------------------------------------------
% custom makros
% -----------------------------------------------------------------------------------------

\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mymauve}{rgb}{0.58,0,0.82}

\lstset{ %
  backgroundcolor=\color{white},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
  basicstyle=\ttfamily\tiny,       % the size of the fonts that are used for the code
  breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
  breaklines=true,                 % sets automatic line breaking
  captionpos=tr,                   % sets the caption-position to top
  commentstyle=\color{mygreen},      % comment style
  deletekeywords={...},            % if you want to delete keywords from the given language
  escapeinside={/*}{*/},           % if you want to add LaTeX within your code
  extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
  frame=single,                    % adds a frame around the code
  keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  keywordstyle=\color{blue},       % keyword style
  language=C++,                    % the language of the code
  otherkeywords={*,...},           % if you want to add more keywords to the set
  numbers=left,                    % where to put the line-numbers; possible values are (none, left, right)
  numbersep=5pt,                   % how far the line-numbers are from the code
  numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers
  rulecolor=\color{black},         % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
  showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
  showstringspaces=false,          % underline spaces within strings only
  showtabs=false,                  % show tabs within strings adding particular underscores
  stepnumber=1,                    % the step between two line-numbers. If it's 1, each line will be numbered
  stringstyle=\color{mymauve},     % string literal style
  tabsize=2,                       % sets default tabsize to 2 spaces
  title=\lstname                   % show the filename of files included with \lstinputlisting; also try caption instead of title
}

%=====================================================================================================
% Define background box capabilities for source code listings
% Taken from https://tex.stackexchange.com/questions/8851/how-can-i-highlight-some-lines-from-source-code?lq=1
%=====================================================================================================

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% \btIfInRange{number}{range list}{TRUE}{FALSE}
%
% Test in 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}
%
% TODO BUG: \btLstHL commands can not yet be accumulated if more than one overlay spec match.
% 
\newcommand<>{\btLstHL}[1]{%
  \only#2{\btIfInRange{\value{lstnumber}}{#1}{\color{orange!30}\def\lst@linebgrdcmd{\color@block}}{\def\lst@linebgrdcmd####1####2####3{}}}%
}%
\makeatother

\begin{document}

\begin{frame}[fragile]{scoped enums}
    \begin{lstlisting}[
                     linebackgroundcolor={%
                         \btLstHL<2>{2}%                     
                         \btLstHL<3>{3-9}%
                         \btLstHL<4>{15}%
                         \btLstHL<5>{17}%
                         \btLstHL<6>{20}%                         
                    }
    ]

enum class Weekdays {     // enum type
  Monday,                 // enumerated values
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday,
  Sunday
};

auto today = Weekdays::Monday;

enum class HttpCodes {
  Continue = 100,         // initialized with 100
  OK = 200,
  Created,                // initialized with 201
  NotFound = 404,
  InternalError = 500,
  Error = InternalError   // initialized with 500 too
};    
\end{lstlisting}
\end{frame}
\end{document}

相关内容