当记录样式文件时,如果能够轻松地将不同的代码示例与它们产生的输出并排放置,那就太好了。
对人们来说,有什么建议可以有效且美观地做到这一点?
答案1
有showexpl
它有LTXexample
专门用于此目的的环境。它基于listings
答案2
我的包裹彩色盒子为这个问题提供了另一种可能的答案。它允许将源代码放在输出之前或之后。使用 2.20 版本,源和输出可以并排设置。输出可以放在彩色框内或外面,在源之前或源之后。
\documentclass{article}
\usepackage[skins,listings]{tcolorbox}
\newtcblisting{exampleA}[2][]{%
colframe=red!50!yellow!50!black,
colback=red!50!yellow!5!white,
coltitle=red!50!yellow!3!white,
bicolor,colbacklower=white,
fonttitle=\sffamily\bfseries,
sidebyside,
title=#2,#1}
\newtcblisting{exampleB}[2][]{%
colframe=red!50!yellow!50!black,
colback=white,
coltitle=red!50!yellow!3!white,
bicolor,colbacklower=red!50!yellow!5!white,
fonttitle=\sffamily\bfseries,
sidebyside,text and listing,
title=#2,#1}
\begin{document}
\begin{exampleA}[righthand width=3.5cm,center lower]{Side-by-side (inside box)}
\begin{tikzpicture}
\path[fill=yellow!50!white] (0,0) circle (11mm);
\path[fill=white] (0,0) circle (9mm);
\foreach \w/\c in {90/red,210/green,330/blue}
{\path[shading=ball,ball color=\c] (\w:1cm) circle (7mm);}
\end{tikzpicture}
\end{exampleA}
\begin{exampleA}[righthand width=3.5cm,listing outside text]{Side-by-side (outside box)}
\begin{tikzpicture}
\path[fill=yellow!50!white] (0,0) circle (11mm);
\path[fill=white] (0,0) circle (9mm);
\foreach \w/\c in {90/red,210/green,330/blue}
{\path[shading=ball,ball color=\c] (\w:1cm) circle (7mm);}
\end{tikzpicture}
\end{exampleA}
\begin{exampleB}[lefthand width=3.5cm]{}
\begin{tikzpicture}
\path[fill=yellow!50!white] (0,0) circle (11mm);
\path[fill=white] (0,0) circle (9mm);
\foreach \w/\c in {90/red,210/green,330/blue}
{\path[shading=ball,ball color=\c] (\w:1cm) circle (7mm);}
\end{tikzpicture}
\end{exampleB}
\begin{exampleB}[lefthand width=3.5cm,text outside listing,
colback=red!50!yellow!5!white,top=0mm,bottom=0mm,left=0mm,right=0mm,
arc=0mm,boxrule=1pt,watermark text=Source,
watermark color=yellow!75!red!30!white]{}
\begin{tikzpicture}
\path[fill=yellow!50!white] (0,0) circle (11mm);
\path[fill=white] (0,0) circle (9mm);
\foreach \w/\c in {90/red,210/green,330/blue}
{\path[shading=ball,ball color=\c] (\w:1cm) circle (7mm);}
\end{tikzpicture}
\end{exampleB}
\end{document}
答案3
tkzexample
我制作了一个用于所有文档的包。我昨天在 ctan 上上传了一个新版本(您需要等待几天才能看到它),但实际上没有文档(我还有一些工作要做才能完成文档。主要宏基\codeexample
于 T. Tantau。Till 允许我使用和修改宏。
\codeexample
不支持重音和 utf8,但tkzexample
接受重音和 utf8。我添加了对行进行编号的可能性,并创建了一个选项,可以使用将示例保存在外部文件中\usepackage[saved]{tkzexample}
。
\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{amsmath,amssymb,booktabs}
\usepackage[frenchb]{babel}
\usepackage{tkzexample}
% possible \usepackage[saved]{tkzexample} permet de sauvegarder l'exemple
\usepackage[protrusion = true,
expansion, final,
verbose = false, babel = true]{microtype}
\DisableLigatures{encoding = T1, family = tt*}
\parindent=0pt
\begin{document}
\colorlet{graphicbackground}{blue!10!white}%
\colorlet{codebackground}{red!10}%
1) How to use
\begin{tkzltxexample}[]
\documentclass{scrartcl}
\usepackage{tkzexample}
\begin{document}
\begin{tkzexample}[]
\begin{tikzpicture}
\path coordinate (A) at (0,0)
coordinate (B) at (-60:12cm)
coordinate (C) at (240:12cm);
\foreach \density in {20,30,...,160}{%
\draw[fill=MidnightBlue!\density] (A)--(B)--(C)--cycle;
\path (A) coordinate (X) -- (B) coordinate[pos=.15](A)
-- (C) coordinate[pos=.15](B) -- (X) coordinate[pos=.15](C);}
\end{tikzpicture}
\end{tkzexample}
\end{document}
\end{tkzltxexample}
2) Usage classique
\begin{tkzexample}[latex=6cm]
\begin{tikzpicture}[scale=.5]
\path coordinate (A) at (0,0)
coordinate (B) at (-60:12cm)
coordinate (C) at (240:12cm);
\foreach \density in {20,30,...,160}{%
\draw[fill=MidnightBlue!\density]
(A)--(B)--(C)--cycle;
\path (A) coordinate (X)
-- (B) coordinate[pos=.15](A)
-- (C) coordinate[pos=.15](B)
-- (X) coordinate[pos=.15](C);
}
\end{tikzpicture}
\end{tkzexample}
3) Un tableau
\begin{tkzexample}[width=6cm,frame tex=Maroon,frame code=Maroon,num ]
\begin{tabular}{ll}
\toprule
Essai avec un tabeau &\\
\midrule
Un premier & texte\\
Un second & texte\\
\bottomrule
\end{tabular}
\end{tkzexample}
4) Les accents en français
\begin{tkzexample}[small,num]
\begin{tikzpicture}
\node[draw] {éè§çà};
\end{tikzpicture}
\end{tkzexample}
\end{document}