tcolorbox:“列出外部(但在上方)文本”选项

tcolorbox:“列出外部(但在上方)文本”选项

tcblisting软件包提供的环境tcolorbox允许

  1. 在 tcbox 的下半部分显示 LaTeX 代码,在相同的tcbox,使用text and listing选项: 在此处输入图片描述

  2. 或者在左侧的 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 texttcolorbox

更新:这些选项是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您可以通过修改代码中的值来调整分离。

它并不完美,但这是我能做的最好的事情......我建议您为此提出功能请求......

相关内容