tcblisting 中的替代 colback 和 colbacklower

tcblisting 中的替代 colback 和 colbacklower

tcblisting我已经基于包编写了一个新环境tcolorbox。我希望代码的背景颜色始终为“灰色”,而编译后的文本的背景颜色为“白色”。

他持有 4 个选项,但在其中几个选项中,我交换了colback和的值colbacklower。这是 MWE:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable,skins}
% tcolorbox settings
\tcbset{
myexample/.style={breakable,skin=bicolor,%
                colback=lightgray,%
                colframe=gray,%
                colbacklower=white,%
                title style={draw=none,fill=none}%
                }%
}% 
% Create myexample enviroment 
\newtcblisting{myexample}[1]{myexample,#1}
\begin{document}
% listing=colback, text=colbacklower OK
\begin{myexample}{listing and text}
The background color of the source code should be in gray.
The background color for text compiled in white.
\end{myexample}
% listing=colbacklower, text=colback Wrong, need listing=colback,text=colbacklower
\begin{myexample}{text and listing}
The background color of the source code should be in gray.
The background color for text compiled in white.
\end{myexample}
% listing=colbacklower, text=colback Wrong, need listing=colback,text=colbacklower 
\begin{myexample}{text side listing}
The background color of the source code should be in gray.
The background color for text compiled in white.
\end{myexample}
% listing=colbacklower, text=colback OK
\begin{myexample}{listing side text}
The background color of the source code should be in gray.
The background color for text compiled in white.
\end{myexample}
\end{document}

以下是我的两个问题:

  1. 可以自动定义环境吗?
  2. 如何为“列表和文本”、“文本和列表”、“列表侧文本”和“文本侧列表”定义别名,如keyval选项(可能是 pgfkeys?)。

    就像是:

    listing and text=top
    text and listing=below
    listing side text=left
    text side listing=right
    

答案1

tcolorbox使用pgfkeys引擎,您可以按照制作的方式添加自己的选项myexample。对于您的问题,答案如下:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable,skins}
% tcolorbox settings
\tcbset{
above/.style={colback=lightgray,colbacklower=white,listing and text},
below/.style={colback=white,colbacklower=lightgray,text and listing},
left/.style={colback=lightgray,colbacklower=white,listing side text},
right/.style={colback=white,colbacklower=lightgray,text side listing},
%
myexample/.style={breakable,skin=bicolor,
                  above,%
                  colframe=gray,%
                  title style={draw=none,fill=none}%
                  }%
}%
% Create myexample enviroment
\newtcblisting{myexample}[1]{myexample,#1}
\begin{document}
% default:
\begin{myexample}{above}
The background color of the source code should be in gray.
The background color for text compiled in white.
\end{myexample}

\begin{myexample}{below}
The background color of the source code should be in gray.
The background color for text compiled in white.
\end{myexample}

\begin{myexample}{right}
The background color of the source code should be in gray.
The background color for text compiled in white.
\end{myexample}

\begin{myexample}{left}
The background color of the source code should be in gray.
The background color for text compiled in white.
\end{myexample}
\end{document}

在此处输入图片描述

相关内容