\surroundwithmdframed 不环绕 \lstinputlisting

\surroundwithmdframed 不环绕 \lstinputlisting

我需要用自定义的灰色包围代码列表mdframed

为此,我使用了非常方便的宏

\surroundwithmdframed[options]{environment}

指示所有lstlisting环境都必须被 包围mdframed

虽然这对于环境内的列表来说效果很好lstlisting,但对于从文件加载的列表却不起作用\lstinputlisting

我怎样才能知道mdframed周围还装载了什么列表\lstinputlisting

\documentclass[12pt]{article}

\usepackage{xcolor}
\definecolor{light-gray}{gray}{0.95} %  gray color for code background

\usepackage[framemethod=TikZ]{mdframed}

\mdfdefinestyle{codeframe}{
  backgroundcolor=light-gray,
  linecolor=light-gray,
  roundcorner=5pt
}

\usepackage{listings}
\lstset{
  basicstyle=\ttfamily,
  breaklines=true,
}

% This tells mdframed to surround every lstlisting environment.
\surroundwithmdframed[style=codeframe]{lstlisting}

\begin{document}
    
    % This listing is surrounded with an mdframed.
    \begin{lstlisting}
    foo
    \end{lstlisting}
    
    % But this one is not!
    \lstinputlisting{foo.txt}
    
\end{document}

在此处输入图片描述

答案1

下列的@daleif 的建议我换成了tcolorbox并能够得到期望的结果:

在此处输入图片描述

\documentclass[12pt]{article}

\usepackage{xcolor}
\definecolor{light-gray}{gray}{0.95} %  gray color for code background

\usepackage[most]{tcolorbox}

\tcbset{code/.style={
  boxsep=1pt,
  boxrule=2pt,
  arc=3mm, 
  auto outer arc,
  colframe=light-gray,
  colback=light-gray,
  listing only
  }
}


\begin{document}

\begin{tcblisting}{code}
foo
\end{tcblisting}

\tcbinputlisting{code,listing file=foo.txt}

\end{document}

相关内容