Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers 3

up vote 8 down vote accepted

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

share|improve this answer
add comment

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

share|improve this answer
add comment

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

share|improve this answer
    
this does not work if we use the ttfamily font (basicstyle={\footnotesize\ttfamily}) –  An Cong Tran Feb 10 at 6:32
    
@AnCongTran Is it? See the edit. –  Harish Kumar Feb 10 at 7:22
    
If you replace "int d = 0;" by "int g = 0;" you will see the problem. –  An Cong Tran Feb 10 at 8:13
    
@AnCongTran Sorry, but what is the problem? –  Harish Kumar Feb 10 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. –  An Cong Tran Feb 10 at 10:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.