0

This is a similar question to listings with background color and mathescape broken.

Here is my code:

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}

\definecolor{MyGray}{gray}{0.9}

\title{test}

\begin{document}

\maketitle

\section{Example}


\begin{lstlisting}[   % 进行参数设置
         basicstyle=\ttfamily,
         backgroundcolor = \color{MyGray},
         breaklines=true, % 自动换行
         frame=tb,
         mathescape=true
    ]
    It is testing.
    $f(x)=\sum\limits_{n=0}^{\infty}$
    Testing is over.
    
\end{lstlisting}

\end{document}

and it shows something like this:

enter image description here

As we can see, the background color is broken.

According to listings with background color and mathescape broken, it is because listings expects a constant line height. However, its solution there only works for its problem. Are there any general solutions? I have tried \resizebox and it works but the mathematical formulations get to be very small.

updated:

By using the package framed, it works on certain situation. However, it still doesn't work very well. For example, if I am using the code like this:

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{listings}
\usepackage[dvipsnames]{xcolor}
\usepackage{framed}

\definecolor{shadecolor}{gray}{0.9}

\setlength{\FrameSep}{0pt}
\setlength{\FrameRule}{0pt}

\title{test}

\begin{document}

\maketitle

\section{Example}

\begin{shaded}
\begin{lstlisting}[   % 进行参数设置
         basicstyle=\ttfamily\large,
         breaklines=true, % 自动换行
         frame=single,
         mathescape=true,
         belowskip=0pt
    ]
    It is testing.
    $f(x)=\sum\limits_{n=0}^{\infty}$
    Testing is over.
    
\end{lstlisting}
\end{shaded}

\end{document}

It still goes wrong like this:

enter image description here

2
  • 1
    It is often better to combine listings and the tcolorbox package to have tcolorbox add decorations to listings. See the tcolorbox manual.
    – daleif
    Commented Dec 8, 2023 at 12:44
  • 1
    Hope this is helpful, tex.stackexchange.com/questions/30516/…
    – MadyYuvi
    Commented Dec 8, 2023 at 13:10

1 Answer 1

2

I believed that, this was a known issue, you can try with the combination of framed.sty and listings.sty, and the MWE is:

\documentclass{article}
\usepackage{xcolor} 
\usepackage{listings}
\usepackage{framed}
\definecolor{shadecolor}{gray}{0.9}

\title{test}

\lstset{%
  mathescape=true,basicstyle=\ttfamily,
         breaklines=true,belowskip=0pt
}
\begin{document}

\setlength{\FrameSep}{0pt}
\setlength{\FrameRule}{0pt}
\maketitle

\section{Example}

\begin{shaded}
\begin{lstlisting}[frame=tb]
    It is testing.
    $f(x)=\sum\limits_{n=0}^{\infty}$
    Testing is over.
    
\end{lstlisting}
\end{shaded}

\end{document}

enter image description here

2
  • Thank you very much. I understand \FrameSep controls the margin of the background color. However, I still don't understand what \FrameRule is used for. I have looked up the document of the package framed but I still don't understand. I would really appreciate it if you can give me some explanations. Commented Dec 8, 2023 at 17:30
  • @liaoming999 Just for safety purpose, I would made it into 0pt, nothing else :-), sorry for the delay in reply
    – MadyYuvi
    Commented Dec 11, 2023 at 8:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .