5

I am trying to change the size of a colorbox based on inner vs. outer pages (odd vs. even). The commented code works, but it looks good only on odd pages. The code below that is (obviously) wrong, but it expresses that I'd like to change the four horizontal size parameters depending on whether the page is odd or even.

Thanks in advance for your help!

\documentclass[twoside]{scrbook}
\usepackage[outer=2.25in, inner=.75in]{geometry}
\usepackage[many]{tcolorbox}
\usepackage{background}
\usepackage{lipsum}
\usepackage{ifthen,ifoddpage}
\SetBgContents{}

% this code runs
% \newtcolorbox{graybox}[1][]{#1,enhanced,width=5in,left=0cm,right=-2cm,extrude left by=1cm,extrude right by=3cm,colback=black!10!white,fontupper=\footnotesize}

% doesn't work but the sort of thing I want
\newtcolorbox{graybox}[1][]{#1,enhanced,width=5in,
    \checkoddpage
    \ifoddpageoroneside
      left=0cm, right=-2cm, extrude left by=1cm, extrude right by=3cm,
    \else
      left=-1cm, right=0cm, extrude left by=2cm, extrude right by=1cm,
    \fi
    colback=black!10!white,fontupper=\footnotesize}

\begin{document}
\lipsum[1-3]
\begin{graybox} \lipsum[1] \end{graybox}
\lipsum[3-4]
\begin{graybox} \lipsum[1] \end{graybox}
\lipsum[5-6]
\end{document}

1 Answer 1

3

As seen on Tcolorbox Package Manual on page 107, you can use the if odd page. I have created a code with \newenvironment instead of \newtcolorbox.

    \documentclass[twoside]{scrbook}
    \usepackage[outer=2.25in, inner=.75in]{geometry}
    \usepackage[many]{tcolorbox}
    \usepackage{background}
    \usepackage{lipsum}
    \usepackage{ifthen,ifoddpage}
    \SetBgContents{}
    
    \newenvironment{mytcb}[1]
    {
    \begin{tcolorbox}[if odd page={enhanced,width=5in,left=0cm, right=-2cm, extrude left
 by=1cm, extrude right by=3cm,colback=black!10!white,fontupper=\footnotesize}
    {enhanced,width=5in,left=-1cm, right=0cm, extrude left by=2cm, extrude right
 by=1cm,colback=black!10!white,fontupper=\footnotesize}]
    #1
    \end{tcolorbox}
    }
    
    \begin{document}
    \lipsum[1-3]
    \begin{mytcb}{\lipsum[1]}  \end{mytcb}
    \lipsum[1-3]
    \begin{mytcb}{\lipsum[2]} \end{mytcb}
    \lipsum[1-3]
    \begin{mytcb}{\lipsum[3]} \end{mytcb}
    
    \end{document}
4
  • Thank you for your quick reply. However, the code works only when the box is on odd pages, but crashes for boxes on even pages ("TeX capacity exceeded [input stack size=5000]"). Jan 18, 2021 at 8:25
  • Update -- I found the error, which was a newline character between the two {}{} fields. Backspacing fixed the problem. Thanks again for your help. Jan 18, 2021 at 8:32
  • 1
    @ferahfeza I don't see the reason for a newenvironment instead of a newtcolorbox. It's just \newtcolorbox{mytcb}{if odd page={....}}.
    – Ignasi
    Jan 18, 2021 at 9:21
  • @Ignasi thanks, your suggestion is simpler and easier because now I don't have to encase the text inside {}. Jan 18, 2021 at 9:55

You must log in to answer this question.

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