我非常喜欢 TikZ/PGF 手册中的“并排”代码示例......
... 我想在我正在编写的手册中使用同样的东西。我找到了codeexample
TikZ/PGF 手册用于此目的的环境的源代码(具体来说,它位于/usr/local/texlive/2012/texmf-dist/doc/generic/pgf/macros/pgfmanual-en-macros.tex
)。
我的问题是(可能相当基本):如何codeexample
在自己的文档中使用相同的环境?我之所以问这个问题,是因为我尝试编译下面相关问题中的示例,但找不到该文件。
答案1
这里我使用tcolorbox
类似于 TikZ 示例的视觉外观。我的示例环境的强制参数sidebyside
是图片的宽度。
更新:
前两个示例使用自动不可见tikzpicture
环境来仅显示代码片段。第三个示例则tikzpicture
刻意使用并显示环境。
\documentclass{article}
\usepackage[most]{tcolorbox}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\lstdefinestyle{example}{style=tcblatex,
classoffset=0,
texcsstyle=*\color{blue},%
deletetexcs={begin,end},
moretexcs={,%
pgfdeclarehorizontalshading,pgfuseshading,node,
useasboundingbox,draw}%
classoffset=1,
keywordstyle=\color{blue},%
morekeywords={tikzpicture,shade,fill,draw,path,node,child,line,width,rectangle},
classoffset=0}
\tcbset{%
fillbackground/.style={before lower pre={%
\tikzset{every picture/.style={execute at end picture={\begin{pgfonlayer}{background}
\fill[yellow!25!white]
([xshift=-1mm,yshift=-1mm]current bounding box.south west) rectangle
([xshift=1mm,yshift=1mm]current bounding box.north east);
\end{pgfonlayer}}}}}},
explicitpicture/.style={before lower=\begin{center},after lower=\end{center},fillbackground}}
\newtcblisting{sidebyside}[2][]{%
enhanced,frame hidden,
top=0pt,bottom=0pt,left=0pt,right=0pt,arc=0pt,boxrule=0pt,
colback=blue!25!white,
listing style=example,
sidebyside,text and listing,text outside listing,sidebyside gap=2mm,
lefthand width=#2,tikz lower,fillbackground,
#1}
\begin{document}
\begin{sidebyside}[before lower app={\path[] (-2,-0.5) rectangle (2,0.5);}]{4.2cm}
\pgfdeclarehorizontalshading{myshadingA}
{1cm}{rgb(0cm)=(1,0,0); color(2cm)=(green); color(4cm)=(blue)}
\pgfuseshading{myshadingA}
\end{sidebyside}
\begin{sidebyside}{4.2cm}
\node {root}
child {node {left}}
child {node {right}
child {node {child}}
child {node {child}}
};
\end{sidebyside}
\begin{sidebyside}[explicitpicture]{4.2cm}
\begin{tikzpicture}[line width=20pt]
\useasboundingbox (0,-1.5) rectangle (3.5,1.5);
\draw[red] (0,0) -- (3,0);
\draw[gray,->] (0,0) -- (3,0);
\end{tikzpicture}
\end{sidebyside}
\end{document}