![tcolorbox:“列出外部(但在上方)文本”选项](https://linux22.com/image/279941/tcolorbox%EF%BC%9A%E2%80%9C%E5%88%97%E5%87%BA%E5%A4%96%E9%83%A8%EF%BC%88%E4%BD%86%E5%9C%A8%E4%B8%8A%E6%96%B9%EF%BC%89%E6%96%87%E6%9C%AC%E2%80%9D%E9%80%89%E9%A1%B9.png)
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
您可以通过修改代码中的值来调整分离。
它并不完美,但这是我能做的最好的事情......我建议您为此提出功能请求......