交替左侧/右侧宽度并检查 tcolorbox 中的奇数页

交替左侧/右侧宽度并检查 tcolorbox 中的奇数页

我正在使用tcolorbox定义选项中的包创建一个新环境,pos=t,b,r,l并使用方案为边距创建一个新环境。我的想法是创建两个新键:lwidthrwidthkey=valuepgfkeys

  1. pos=e交替pos=lpos=r判断页面是偶数还是奇数。像这样:

    pos/e/.style={\ifoddpage pos=l \else pos=r \fi}
    
  2. width=如果pos=lwidth=lwidth,如果pos=rwidth=rwidth,如果pos=t,b width将具有默认值。

这是 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}

答案1

我修改了您的示例,添加了一个pos=e选项,可以根据奇偶页切换左手和右手。我包含了changepage奇偶测试的包。此外,我缩短了新环境的 xparse 定义。

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

在此处输入图片描述

相关内容