3

I am creating a new environment using the tcolorbox package in the define options pos=t,b,r,l and lwidth and rwidth for margins, using the scheme key=value of pgfkeys. My idea is to create two new keys:

  1. pos=ealternate pos=l and pos=r if page is even or odd. Like this:

    pos/e/.style={\ifoddpage pos=l \else pos=r \fi}
    
  2. width=if pos=l then width=lwidth, if pos=r then width=rwidth, if pos=t,b width will have the default value.

This is the MWE:

\documentclass[10pt]{article}
\usepackage{lmodern}
\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable,skins,xparse}
% Colors
\definecolor{hellgelb}{rgb}{1,1,0.85}
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colComments}{rgb}{1,0,0}
\definecolor{colString}{rgb}{0,0.5,0}
% Listings settings 
\lstset{
    basicstyle=\ttfamily\small, %
    identifierstyle=\color{colIdentifier}, %
    keywordstyle=\color{colKeys}, %
    stringstyle=\color{colString}, %
    commentstyle=\color{colComments}, %
    columns=flexible, %
    extendedchars=true, %
    showspaces=false, %
    showstringspaces=false, %
    breaklines=true, %
    breakautoindent=true,%
    includerangemarker=false,%
    numbers=left,%
    numbersep=3pt,%
    numberstyle=\tiny\color{red}%
    }
% General tcb options
\tcbset{
    title style={draw=none,fill=none},left=3.5mm,right=1mm,top=-1.5mm,%
    bottom=-1.5mm,sidebyside gap=5mm,middle=5mm,boxsep=0mm,%
    graphic/.style 2 args={comment={\includegraphics[#1]{#2}}},% image coment
    pos/.is choice,% Define pos=t,b,l,r
    fcolor/.style={colframe=#1},% frame color
    bcolor/.style={colback=#1},% background color
    scolor/.style={drop fuzzy shadow=#1},% shadow color
    lwhidth/.style={lefthand width=#1},%  width left
    rwhidth/.style={righthand width=#1},%  width right
    hsep/.style={sidebyside gap=#1},% Horizontal sep
    vsep/.style={middle=#1}% Vertical sep,
    }
% Position 
\tcbset{
    pos/r/.style={colback=red!5!white,colframe=red!75!black,center lower,%
        listing outside text,freelance,drop fuzzy shadow=blue!50!black!50!white},%
    pos/l/.style={colback=gray!50!white,center lower,%
        text outside listing,freelance,drop fuzzy shadow=blue!50!black!50!white},%
    pos/t/.style={colback=gray!50!white,center lower,%
        listing above text,freelance,drop fuzzy shadow=blue!50!black!50!white},%
    pos/b/.style={center upper,colback=gray!50!white,%
        text above listing,freelance,drop fuzzy shadow=blue!50!black!50!white},%
    myexample/.style={pos=l,listing options={language=TeX}}%,
        }
% Define newtcblistin environment
\newtcblisting{exampleinner}[1]{myexample,#1}
% Define MYexample environment whit [] using xparse
\ExplSyntaxOn
\NewDocumentEnvironment{MYexample}{O{}}
 {\exampleinner{#1}}{\endexampleinner}
\ExplSyntaxOff
\begin{document}

\begin{MYexample}[pos=l,hsep=1cm,lwhidth=2cm,fcolor=blue,bcolor=lightgray]
Text pos=l
Text pos=l
Text pos=l
Text pos=l
Text pos=l
\end{MYexample}

\end{document}
4
  • You don't have to sign with your name since it automatically appears in the lower right corner of your post.
    – jub0bs
    Apr 22, 2014 at 12:07
  • Thanks for the comment, it is a custom of the Spanish forums... Apr 22, 2014 at 12:40
  • @PabloGonzálezL: tcolorbox offers toggle left and right (page 30) and check odd page (page 71) which, I think, can help you. Did you look at them?
    – Ignasi
    Apr 26, 2014 at 8:10
  • thank, but, not found in this case.... Apr 26, 2014 at 16:28

1 Answer 1

1

I modified your example with a pos=e option which toggles lefthand and righthand according to even/odd page. I included the changepage package for the even/odd test. Additionally, I shortened the xparse definition of your new environment.

\documentclass[10pt]{article}
\usepackage{lmodern}
\usepackage{tcolorbox}
\usepackage{changepage}
\tcbuselibrary{listings,breakable,skins,xparse}
% Colors
\definecolor{hellgelb}{rgb}{1,1,0.85}
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colComments}{rgb}{1,0,0}
\definecolor{colString}{rgb}{0,0.5,0}
% Listings settings
\lstset{
    basicstyle=\ttfamily\small, %
    identifierstyle=\color{colIdentifier}, %
    keywordstyle=\color{colKeys}, %
    stringstyle=\color{colString}, %
    commentstyle=\color{colComments}, %
    columns=flexible, %
    extendedchars=true, %
    showspaces=false, %
    showstringspaces=false, %
    breaklines=true, %
    breakautoindent=true,%
    includerangemarker=false,%
    numbers=left,%
    numbersep=3pt,%
    numberstyle=\tiny\color{red}%
    }
% General tcb options
\tcbset{
    title style={draw=none,fill=none},left=3.5mm,right=1mm,top=-1.5mm,%
    bottom=-1.5mm,sidebyside gap=5mm,middle=5mm,boxsep=0mm,%
    graphic/.style 2 args={comment={\includegraphics[#1]{#2}}},% image coment
    pos/.is choice,% Define pos=t,b,l,r
    fcolor/.style={colframe=#1},% frame color
    bcolor/.style={colback=#1},% background color
    scolor/.style={drop fuzzy shadow=#1},% shadow color
    lwhidth/.style={lefthand width=#1},%  width left
    rwhidth/.style={righthand width=#1},%  width right
    hsep/.style={sidebyside gap=#1},% Horizontal sep
    vsep/.style={middle=#1}% Vertical sep,
    }
% Position
\tcbset{
    mywidth/.store in=\mywidth,
    pos/r/.style={colback=red!5!white,colframe=red!75!black,center lower,righthand width=\mywidth,%
        listing outside text,freelance,drop fuzzy shadow=blue!50!black!50!white},%
    pos/l/.style={colback=gray!50!white,center lower,lefthand width=\mywidth,%
        text outside listing,freelance,drop fuzzy shadow=blue!50!black!50!white},%
    pos/t/.style={colback=gray!50!white,center lower,%
        listing above text,freelance,drop fuzzy shadow=blue!50!black!50!white},%
    pos/b/.style={center upper,colback=gray!50!white,%
        text above listing,freelance,drop fuzzy shadow=blue!50!black!50!white},%
    pos/e/.code={\ifoddpage\tcbset{pos=l}\else\tcbset{pos=r}\fi},%
    myexample/.style={pos=l,listing options={language=TeX}}%,
        }
% Define newtcblisting environment
\NewTCBListing{MYexample}{O{}}{myexample,#1}

\begin{document}

\begin{MYexample}[pos=l,hsep=1cm,mywidth=2cm,fcolor=blue,bcolor=lightgray]
Text pos=l
Text pos=l
Text pos=l
Text pos=l
Text pos=l
\end{MYexample}

\begin{MYexample}[pos=r,hsep=1cm,mywidth=2cm,fcolor=blue,bcolor=lightgray]
Text pos=r
Text pos=r
Text pos=r
Text pos=r
Text pos=r
\end{MYexample}

\begin{MYexample}[pos=e,hsep=1cm,mywidth=2cm,fcolor=blue,bcolor=lightgray]
Text pos=e
Text pos=e
Text pos=e
Text pos=e
Text pos=e
\end{MYexample}

\clearpage

\begin{MYexample}[pos=e,hsep=1cm,mywidth=2cm,fcolor=blue,bcolor=lightgray]
Text pos=e
Text pos=e
Text pos=e
Text pos=e
Text pos=e
\end{MYexample}

\end{document}

enter image description here

1
  • Thank you very much, it's just what I wanted, I never thought to use tcbset in tcbset, lwidtht and rwidth are no longer needed (suppose). I had forgotten that xparsewas available within tcolorbox, the code is more clear. May 6, 2014 at 11:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .