我使用的listings
包如下:
\usepackage{listings}
\lstnewenvironment{pythoncode}[1][]
{\lstset{language=Python,
basicstyle=\ttfamily\footnotesize,
frame=shadowbox,
frameround=tttt,
rulecolor=\color{black},
rulesepcolor=\color{gray},
#1}
}{}
我想让圆角和阴影 (shadowbox) 结合在一起。可以吗?虽然框架边框本身有圆角,但阴影有方角,这有点难看。
答案1
下一个代码用于tcolorbox
格式化通过包插入代码的阴影框listing
。阴影的大小、颜色和格式可以根据个人喜好进行调整。
\documentclass{article}
\usepackage[most]{tcolorbox}
\newtcblisting{pythoncode}[2][]{%
enhanced, title=#2, colframe=blue!50!black,
colback=blue!10!white,
fonttitle=\ttfamily, coltitle=white,
attach boxed title to top left = {xshift=5mm,yshift=-2mm} ,
boxed title style={size=small, colback=blue!75!black},
width=.5\linewidth,
listing only,listing options={language=Python, basicstyle=\ttfamily\footnotesize},#1}
\begin{document}
\begin{pythoncode}[drop shadow]{hello.py}
#!/usr/bin/env python
def main():
print "Hello, World!"
if __name__ == '__main__':
main()
\end{pythoncode}
\begin{pythoncode}[drop fuzzy shadow]{hello.py}
#!/usr/bin/env python
def main():
print "Hello, World!"
if __name__ == '__main__':
main()
\end{pythoncode}
\begin{pythoncode}[drop lifted shadow]{hello.py}
#!/usr/bin/env python
def main():
print "Hello, World!"
if __name__ == '__main__':
main()
\end{pythoncode}
\end{document}
答案2
你是指这样的吗?
\documentclass{beamer}
\usepackage{listings}
\begin{document}
\begin{frame}[fragile]{demo}
\begin{beamerboxesrounded}[shadow]{A listing}
\begin{lstlisting}
foo
bar
baz
\end{lstlisting}
\end{beamerboxesrounded}
\end{frame}
\end{document}
答案3
这是使用 TikZ 的示例。也可以与 beamer 配合使用
\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols,plotmarks,decorations.markings,shadows}
\begin{document}
\begin{tikzpicture}[x=1cm,y=1cm]
\node[drop shadow,fill=white,draw,rounded corners]
{$\sum\limits_{i=1}^N (x_i-\mu_i)^2$};
\end{tikzpicture}
\end{document}
本来想发布一张照片,但我还没有足够的声誉来这样做。
使用列表可能看起来像这样:
\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{listings}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,shapes,%
matrix,shapes.symbols,plotmarks,decorations.markings,shadows}
%%%%%%%%%%%%%%%%%%%%%
% Code Listing
%%%%%%%%%%%%%%%%%%%%%
\lstnewenvironment{CODEENV}{
\lstset{
language=Matlab,
numbers=left,
numberstyle=\tiny,
stepnumber=1,
numbersep=1em,
tabsize=1,
}
}{}
%%%%%%%%%%%%%%%%%%%%%
% Document
%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{tikzpicture}[x=1cm,y=1cm]
\node[drop shadow,fill=white,draw,rounded corners,inner sep=1em] {
\begin{CODEENV}
N = 128;
sigma_r = 1;
% Leistungsdichte nach Clark
nu_max = 1;
nu = (-nu_max):(2*nu_max/(N-1)):(nu_max);
% Bessel & Clark
xlambda = (0:2/(N-1):2);
rho_R = sigma_r^2*besselj(0, 2*pi*xlambda);
figure;
subplot(211);
plot(xlambda,rho_R/max(rho_R));
subplot(212);
plot(nu, abs(fft(rho_R)).^2);
\end{CODEENV}
};
\end{tikzpicture}
\end{document}
请注意,如果您有较长的代码片段,则可能需要增加节点的“inner sep”参数,因为行号是在列表框之外完成的(看起来是这样)。