我想在 PGF 中创建一个文件或页面状的节点形状,并将一个 minted 环境放入该形状中。参见下图:
有几个问题。一个是文本绘制在底部和右侧的边框上。另一个是我希望能够将 minted 环境的内容缩放 0.5 倍。如果我可以将所有 minted 内容作为属性提供给节点或形状,我也希望如此,\node [draw, shape=document, minted={python,class.py}] {};
但如果这不可能,我会接受。我与 minipage 实现没有任何关系。
对于边框上的文本,我认为可以从 minipage 尺寸中减去当前线宽。我尝试在 minipage 内部使用 scalebox 进行缩放,但 minted verbatim 环境失败了。我真的不知道如何将 minted 参数作为属性传递给节点。
执行:
\documentclass{standalone}
\usepackage{minted}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\renewcommand{\familydefault}{\sfdefault}
\begin{filecontents}{class.py}
class MyClass:
variable = "blah"
def function(self):
print("This is a message inside the class.")
myobjectx = MyClass()
myobjectx.variable
\end{filecontents}
\makeatletter
\pgfdeclareshape{document}{
\inheritsavedanchors[from=rectangle] % this is nearly a rectangle
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{east}
% ... and possibly more
\backgroundpath{% this is new
% store lower right in xa/ya and upper right in xb/yb
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
% compute corner of ‘‘flipped page’’
\pgf@xc=\pgf@xb \advance\pgf@xc by-7.5pt % this should be a parameter
\pgf@yc=\pgf@yb \advance\pgf@yc by-7.5pt
% construct main path
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
\pgfpathclose
% add little corner
\pgfpathmoveto{\pgfpoint{\pgf@xc}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}
\pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node [draw, line width=3pt, shape=document] {\begin{minipage}[t][1cm][t]{4cm}\inputminted{python}{class.py}\end{minipage}};
\end{tikzpicture}
\end{document}
答案1
tcolorbox
虽然我不知道如何剪辑列表,但可以做类似的事情。
\documentclass{article}
\usepackage[most, minted]{tcolorbox}
\tcbset{
mylisting/.style={
listing engine=minted, minted style=trac,
minted language=python, listing only,
enhanced,
boxrule=1mm,
colback=yellow!50,
colframe=yellow!20!black,
sharp corners,rounded corners=northeast,arc is angular,arc=3mm,
underlay={%
\path[fill=tcbcolback] ([yshift=-3mm]interior.north east)-|([xshift=-3mm]interior.north east);
\path[draw=tcbcolframe, shorten <=-0.5mm,shorten >=-0.5mm, line width=1mm] ([yshift=-3mm]interior.north east)-|([xshift=-3mm]interior.north east);},
#1
}
}
\begin{filecontents}{class.py}
class MyClass:
variable = "blah"
def function(self):
print("This is a message inside the class.")
myobjectx = MyClass()
myobjectx.variable
\end{filecontents}
\begin{document}
\tcbinputlisting{mylisting, listing file=class.py}
\end{document}