tcblisting
软件包提供的环境tcolorbox
允许
在 tcbox 的下半部分显示 LaTeX 代码,在相同的tcbox,使用
text and listing
选项:或者在左侧的 tcbox 中显示 LaTeX 代码,输出外部右边的 tcbox,使用
listing outside text
选项:
有可能吗...
- 底部的 tcbox 中的 LaTeX 代码和
- 输出位于外侧并居中那个 tcbox?
上面两张图片的代码
\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usepackage[most]{tcolorbox}
\begin{document}
\begin{tcblisting}{text and listing,center upper}
\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{tcblisting}
\begin{tcblisting}{listing outside text,center lower,righthand 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{tcblisting}
\end{document}
答案1
我遵循 karlkoeller 的建议并将添加新的选项和text above listing
(即将推出的版本 2.52 或更高版本)。listing above text
tcolorbox
更新:这些选项是tcolorbox
从 开始的一部分version 2.60 (2013/12/17)
。示例的更新代码为:
\documentclass{article}
\usepackage{tikz}
\usepackage[most]{tcolorbox}
\begin{document}
\begin{tcblisting}{text above listing,center lower}
\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{tcblisting}
\begin{tcblisting}{listing above text,center lower}
\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{tcblisting}
\end{document}
答案2
文档中似乎不存在这样的选项。
因此,我尝试text above listing
使用以下代码创建一个名为的新代码:
\makeatletter
\def\tcb@inputlisting@out@textAboveListing{%
\tcb@inputlisting@out@prepare%
\tcbdimto\tcb@w@outside{\tcb@width}%
\tcbdimto\tcb@w@inside{\tcb@width}%
\begin{tcb@sbsfloat}\begin{minipage}{\tcb@width}%
\iftcb@lowervisible\tcb@minipage{\tcb@w@outside}\color{\kvtcb@collower}\kvtcb@fontlower\kvtcb@before@lower\ignorespaces%
\tcbuselistingtext\unskip\kvtcb@after@lower\endminipage\fi\par\vspace*{10pt}%
\tcb@minipage{\tcb@w@inside}%
\begin{tcolorbox}[width=\tcb@w@inside,nobeforeafter,nofloat,sidebyside=false]\tcbuselistinglisting\end{tcolorbox}%
\endminipage%
\end{minipage}\end{tcb@sbsfloat}%
}
\tcbset{%
text above listing/.code={\let\tcb@inputlisting\tcb@inputlisting@out@textAboveListing}%
}
\makeatother
在 MWE 中使用它
\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usepackage[most]{tcolorbox}
\makeatletter
\def\tcb@inputlisting@out@textAboveListing{%
\tcb@inputlisting@out@prepare%
\tcbdimto\tcb@w@outside{\tcb@width}%
\tcbdimto\tcb@w@inside{\tcb@width}%
\begin{tcb@sbsfloat}\begin{minipage}{\tcb@width}%
\iftcb@lowervisible\tcb@minipage{\tcb@w@outside}\color{\kvtcb@collower}\kvtcb@fontlower\kvtcb@before@lower\ignorespaces%
\tcbuselistingtext\unskip\kvtcb@after@lower\endminipage\fi\par\vspace*{10pt}%
\tcb@minipage{\tcb@w@inside}%
\begin{tcolorbox}[width=\tcb@w@inside,nobeforeafter,nofloat,sidebyside=false]\tcbuselistinglisting\end{tcolorbox}%
\endminipage%
\end{minipage}\end{tcb@sbsfloat}%
}
\tcbset{%
text above listing/.code={\let\tcb@inputlisting\tcb@inputlisting@out@textAboveListing}%
}
\makeatother
\begin{document}
\begin{tcblisting}{text above listing,center lower}
\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{tcblisting}
\end{document}
产量:
10pt
您可以通过修改代码中的值来调整分离。
它并不完美,但这是我能做的最好的事情......我建议您为此提出功能请求......