我需要用自定义的灰色包围代码列表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}