为了在我的文档中的代码周围制作灰色框,我使用了:
\usepackage{listings}
\usepackage{xcolor} %custom colours
\usepackage{mdframed} %nice frames
\definecolor{light-gray}{gray}{0.95} %the shade of grey that stack
\begin{mdframed}[backgroundcolor=light-gray, roundcorner=10pt,leftmargin=1, rightmargin=1, innerleftmargin=15, innertopmargin=15,innerbottommargin=15, outerlinewidth=1, linecolor=light-gray]
\begin{lstlisting}[language=Python]
Program here
\end{lstlisting}
\end{mdframed}
有什么方法可以将两个以垃圾开头的命令压缩为一个 begin{program} end{program}?我对 Late 还很陌生,不确定如何制作这些自定义命令。
答案1
您可以使用
\surroundwithmdframed[<options>]{<environment>}
完整示例:
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor} %custom colours
\usepackage[framemethod=tikz]{mdframed} %nice frames
\definecolor{light-gray}{gray}{0.95} %the shade of grey that stack
\surroundwithmdframed[backgroundcolor=light-gray, roundcorner=10pt,leftmargin=1, rightmargin=1, innerleftmargin=15, innertopmargin=15,innerbottommargin=15, outerlinewidth=1, linecolor=light-gray]{lstlisting}
\begin{document}
\begin{lstlisting}[language=Python,caption={A test listing}]
Program here
\end{lstlisting}
\end{document}
结果:
顺便说一句,你应该使用
\usepackage[framemethod=tikz]{mdframed} %nice frames
正如我在示例中所做的那样;否则,某些选项(例如roundcorner
)将被忽略。
不过,我想建议你使用强大的tcolorbox
包及其与以下项的良好交互listings
:
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor} %custom colours
\usepackage[most]{tcolorbox} %nice frames
\definecolor{light-gray}{gray}{0.95} %the shade of grey that stack
\newtcblisting{mytcbox}[1][]{
breakable,
enhanced,
colback=light-gray,
colframe=light-gray,
boxrule=0pt,
arc=10pt,
auto outer arc,
listing only,
listing options={#1}
}
\begin{document}
\begin{mytcbox}[language=Python]
Program here
\end{mytcbox}
\end{document}
结果: