9

I am using listing environment (listings package) with the background parameter. I want to highlight some lines of code and I am using the \colorbox, \makebox and \framebox to achieve this. However, a problem with my solution is that the background is broken (space without background in the listing environment).

Would you please to help me to solve out this problem.

\documentclass{beamer}
\usetheme{Montpellier}
\usepackage{listings}
\usepackage{xcolor}

\begin{document}

\begin{frame}[fragile]
\frametitle{Background broken}
\begin{lstlisting}[escapechar=$, backgroundcolor=\color{lightgray}]
int a = 0;
$\colorbox{green}{int b = 0;}$
int c = 0;
$\framebox{int d = 0;}$
int e = 0;
int f = 0;
int g = 0;
\end{lstlisting}

\end{frame}

\end{document}

enter image description here

9

You can use the very powerful tcolorbox package, so the background is colored on a global basis, rather than line by line:

\documentclass{beamer}
\usetheme{Montpellier}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\newcommand{\lcolorbox}[2]{%
  \hspace*{-\fboxsep}\colorbox{#1}{#2}%
}
\newcommand{\lfbox}[1]{%
  \hspace*{-\fboxsep}\hspace*{-\fboxrule}\fbox{#1}%
}

\usepackage{xcolor}

\begin{document}

\begin{frame}[fragile]
\frametitle{Background broken}
\lstset{escapechar=$}
\begin{tcblisting}{
  listing only,
  colback=lightgray,
}
int a = 0;
$\lcolorbox{green}{int b = 0;}$
int c = 0;
$\lfbox{int d = 0;}$
int e = 0;
int f = 0;
int g = 0;
\end{tcblisting}

\end{frame}

\end{document}

I have also provided some commands for adjusting the horizontal position of the boxes.

enter image description here

5
  • You get a gray line between consecutive lines if you highlight them. That might not be the desired result.
    – maiermic
    Jun 22 '19 at 20:41
  • @maiermic I'm not sure what you mean.
    – egreg
    Jun 22 '19 at 20:51
  • Replace int c = 0; with $\lcolorbox{green}{int c = 0;}$ and you get a gray line between the two green boxes.
    – maiermic
    Jun 22 '19 at 21:01
  • @maiermic And what's the problem? If you need no separation between highlighted lines, that's the object of a new question.
    – egreg
    Jun 22 '19 at 21:03
  • That's what I did: tex.stackexchange.com/q/496920/71644
    – maiermic
    Jun 22 '19 at 21:15
6

If you want to highlight entire lines, you can exploit the features of the lstlinebgrd package.

Here's an example:

\documentclass{beamer}
\usetheme{Montpellier}
\usepackage{listings}
\usepackage{lstlinebgrd}


\begin{document}

\begin{frame}[fragile]
\frametitle{Background broken}
\begin{lstlisting}[escapechar=$,linebackgroundcolor={\ifnum\value{lstnumber}=2\color{green}\else\color{lightgray}\fi}]
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
int g = 0;
\end{lstlisting}

\end{frame}

\end{document} 

enter image description here

4

You can set \fboxsep to a suitable value. \fboxsep=1.5pt seems to work fine.

\documentclass{beamer}
\usetheme{Montpellier}
\usepackage{listings}
\usepackage{xcolor}
\fboxsep=1.5pt                     %% adjust
\begin{document}

\begin{frame}[fragile]
\frametitle{Background broken}
\begin{lstlisting}[escapechar=$, backgroundcolor=\color{lightgray}]
int a = 0;
$\colorbox{green}{int b = 0;}$
int c = 0;
$\framebox{int d = 0;}$
int e = 0;
int f = 0;
int g = 0;
\end{lstlisting}

\end{frame}

\end{document}

enter image description here

With basicstyle=\footnotesize\ttfamily:

\documentclass{beamer}
\usetheme{Montpellier}
\usepackage{listings}
\usepackage{xcolor}
\fboxsep=1.5pt                     %% adjust
\begin{document}

\begin{frame}[fragile]
\frametitle{Background broken}
\begin{lstlisting}[escapechar=$, backgroundcolor=\color{lightgray},basicstyle=\footnotesize\ttfamily]
int a = 0;
$\colorbox{green}{int b = 0;}$
int c = 0;
$\framebox{int g = 0;}$
int e = 0;
int f = 0;
int g = 0;
\end{lstlisting}

\end{frame}

\end{document}

enter image description here

5
  • this does not work if we use the ttfamily font (basicstyle={\footnotesize\ttfamily}) Feb 10 '14 at 6:32
  • @AnCongTran Is it? See the edit.
    – user11232
    Feb 10 '14 at 7:22
  • If you replace "int d = 0;" by "int g = 0;" you will see the problem. Feb 10 '14 at 8:13
  • @AnCongTran Sorry, but what is the problem?
    – user11232
    Feb 10 '14 at 8:23
  • It is so weird. If I replace "int d = 0;" by "int g = 0;", a gap appears between "int g" and "int d" (as in my question). I do not know how to attach the figure in this section. Feb 10 '14 at 10:04

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.