[Tex/LaTex] Alternate colback and colbacklower in tcblisting

colorlistingspgfkeystcolorbox

I have written a new environment based on tcblisting from package tcolorbox. I want the background color for code always in "gray" and the background color of the text compiled in "white".

He held four options, but in a couple of them I switch the values ​​of colback and colbacklower. This is an 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}

And here are my two questions:

  1. It is possible to automatically define the environment?
  2. How to define an alias for "listing and text", "text and listing", "listing side text" and "text side listing", like keyval options (pgfkeys maybe?).

    something like:

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

Best Answer

tcolorbox uses the pgfkeys engine and you may add your own options the same way you made myexample. For your question, an answer is the following:

\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}

enter image description here