Currently I am in the process of preparing a C++ training with the beamer package. So far everything is great!
From the answer to How can I highlight some lines from source code I grabbed a nice solution of highlighting code step by step. Since I have most of the time not much space below the code examples, I tend to explain certain lines in the code with C++ comment.
So with the code from Daniel I set the linebackgroundcolor. Is there a way to set at the same time an other lstlisting property like the commentstyle? My idea here is that the default color for the commentstyle is white and only in the highlighted sections I set a different color, e.g. green. (I tried simply to stay with white, but the contrast between the orange of the box and the white of the comment is from my point of view to low). For better illustration, see please the image below.
Many thanks in advance!
Here is my Latex code excerpt.
\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}
