4

I'm a new user (I used to ask some questions here but it's long time ago) and have a problem about an example box created by tcolorbox on scrbook class. I would like to ask all guru some questions as following:

  1. I want a box to span across the margin as shown in figure no matter what odd/even page. As you can see, box(or blue frame) spans to outer of margin but keep text in text area and still allow to put figure in margin too.

    evenpage example

    oddpage example

My MWE.

\documentclass[twoside,chapterprefix]{scrbook}
\usepackage{showframe}
\usepackage{xcolor,graphicx,kantlipsum,multicol}
\usepackage[papersize={21cm,23cm},top=1.8cm, bottom=2cm, outer=7cm, inner=2cm, heightrounded, marginparwidth=5.0cm, marginparsep=0.5cm]{geometry}
\usepackage{sidenotes}
%The pack­age pro­vides an en­vi­ron­ment for coloured and framed text boxes with a head­ing line
\usepackage[most]{tcolorbox}
\usepackage{wallpaper}

%Define framed box for example problem
\newtcolorbox[auto counter,number within=chapter]{examplebox}[2][]{breakable,fonttitle=\bfseries, title=\Large{\textbf{Problem.}}~\thetcbcounter #2,#1}

\begin{document}

\chapter{Introduction}

\begin{examplebox}[label={myautocounter},left=1pt]{}
    \begin{marginfigure}[0cm]
        \includegraphics[scale=0.25]{example-image}
        \label{fig:dsc4336}
        \caption{I am a figure}
    \end{marginfigure}
I USE MY "EXAMPLEBOX" AS USUAL TEXT AND MARGIN FIGURE WORK AS THEY SHOULD BE BUT I WANT MY EXAMPLE BOX SPREAD OUT TO THE RIGHT MARGIN ON AN ODD PAGE, BREAKABLE, AND SPREAD OUT TO THE LEFT MARGIN ON THE EVEN PAGE.    
\end{examplebox}    

\newpage

\begin{examplebox}[label={myautocounter},left=1pt]{}
    \begin{marginfigure}[0cm]
        \includegraphics[scale=0.25]{example-image}
        \label{fig:dsc4336}
        \caption{I am a figure}
    \end{marginfigure}
    I USE MY "EXAMPLEBOX" AS USUAL TEXT AND MARGIN FIGURE WORK AS THEY SHOULD BE BUT I WANT MY EXAMPLE BOX SPREAD OUT TO THE RIGHT MARGIN ON AN ODD PAGE, BREAKABLE, AND SPREAD OUT TO THE LEFT MARGIN ON THE EVEN PAGE.    
\end{examplebox}


\end{document}

In my MWE, when I try to expand it, it will take text stretch out to outer margin too. I need to make a breakable box that cover all pagewidth (textwidth+marginparsep+marginparwidth) but still be able to insert text into text area and insert figure into margin area as the same time.

myresult1

enter image description here

Any helps, suggestions, comments, are very appreciate.

Have a nice day.

more than 1 page example

  • 1
    Welcome! Please provide some code so we can see what you've got, preferably for a minimal but compilable document which reproduces the problem. – cfr Mar 1 '18 at 3:58
  • Sorry, but your example is not compilable -- font settings no one else can use, wrong \label settings, \caption that does appear in floating environments – user31729 Mar 1 '18 at 9:27
  • @kmmm: I've compiled with xelatex – user31729 Mar 1 '18 at 10:01
  • @Christian: I modified MWE again. Thanks in advance for your help. – kmmm Mar 1 '18 at 10:54
  • Your code cannot be compiled without error, so how can we help when you aren't asking about an error? You need to give us code we can compile to reproduce the problem. If that's an error, it should give the relevant error. If it is the result, it should give the result. – cfr Mar 6 '18 at 4:12
3

I think that you cannot have everything you want. I am almost certain you can't have everything you want without serious fiddlery.

Here are two possible approaches. Both use tcolorbox for both the text and marginal image content.

  1. This uses a modified form of the examplebox environment. The disadvantage is that the marginal image must be specified first on even pages, but second on odd pages i.e. the content switch is not automatic.

  2. This switches the content automatically. The disadvantage is that you must use \tcbsidebyside[example box]{<text column content>}{<marginal image column content>} to specify the text and marginal image. If you want a title, it is passed as an argument to example box.

I illustrate this with 4 examples: one each of each kind on each of an odd and even page.

\begin{examplebox}{}
  I use my ``examplebox'' as usual text and margin figure work as they should be but I want my example box spread out to the right margin on an odd page, breakable, and spread out to the left margin on the even page.
  \tcblower
  \includegraphics[scale=0.25]{example-image}
  \captionof{figure}{I am figure}\label{fig:example-image}
\end{examplebox}

Here, the marginal image goes after \tcblower and the text before it. The environment can be passed an optional argument, to override settings locally. The mandatory argument contains the title (here empty).

\tcbsidebyside[example box=Title here]{%
  I use my ``examplebox'' as usual text and margin figure work as they should be but I want my example box spread out to the right margin on an odd page, breakable, and spread out to the left margin on the even page.%
}{%
  \includegraphics[scale=0.25]{example-image-a}
  \captionof{figure}{I am a figure}\label{fig:example-image-a}%
}

Here, the example box style is used, with the title Title here. The text goes in the first mandatory argument; the marginal image goes in the second.

\newpage

So ends our odd page.

\begin{examplebox}{}
  \includegraphics[scale=0.25]{example-image-b}
  \captionof{figure}{I am b figure}\label{fig:example-image-b}
  \tcblower
  I use my ``examplebox'' as usual text and margin figure work as they should be but I want my example box spread out to the right margin on an odd page, breakable, and spread out to the left margin on the even page.
\end{examplebox}

Here, the marginal image goes before \tcblower, while the text goes after. Arguments as before.

\tcbsidebyside[example box]{% with no title
  I use my ``examplebox'' as usual text and margin figure work as they should be but I want my example box spread out to the right margin on an odd page, breakable, and spread out to the left margin on the even page.%
}{%
  \includegraphics[scale=0.25]{example-image-c}
  \captionof{figure}{I am c figure}\label{fig:example-image-c}%
}

Here, the example box style is invoked without an argument, so the title is empty in this case. As before, the text is specified in the first mandatory argument; the marginal image in the second. As we are on an even page, this order will be automatically switched when typesetting i.e. the marginal image will be set in the left of the box and the text in the right.

Hence ends our even page.

The first approach with the examplebox environment has been modified to use a custom counter, in order to illustrate how the second approach could be integrated into a single numbering sequence, in case you want to use a mixture of such approaches. If not, you could more conveniently stick to tcbcounter.

four boxes

\documentclass[twoside,chapterprefix]{scrbook}
\usepackage{graphicx}
\usepackage[showframe, papersize={21cm,23cm},top=1.8cm, bottom=2cm, outer=7cm, inner=2cm, heightrounded, marginparwidth=5.0cm, marginparsep=0.5cm]{geometry}% I guess you know this package tramples wantonly on KOMA-script's careful typographic design?!
%The pack­age pro­vides an en­vi­ron­ment for coloured and framed text boxes with a head­ing line
\usepackage[most]{tcolorbox}

\NewTColorBox
[%
  use counter=mytcbcounter, 
]{examplebox}{ O{} +m }{%
  breakable, 
  toggle enlargement=evenpage, 
  grow to right by=\marginparwidth+\marginparsep, 
  sidebyside, 
  lower separated=false, 
  if odd page={lefthand width=\textwidth-2.5mm-2pt, before lower app=\centering,}{righthand width=\textwidth-2.5mm-2pt, before upper app=\centering},
  boxrule=.5mm, 
  boxsep=1mm, 
  left=1pt,
  right=1pt, 
  sidebyside gap=\marginparsep+2mm+2pt, 
  fonttitle=\bfseries\Large, 
  title={Problem~\themytcbcounter #2}, 
  #1,
}

\tcbset{%
  example box/.style={
    breakable, 
    toggle enlargement=evenpage, 
    grow to right by=\marginparwidth+\marginparsep, 
    sidebyside, 
    lower separated=false, 
    if odd page={lefthand width=\textwidth-2.5mm-2pt, before lower app=\centering,}{before upper app=\centering, righthand width=\textwidth-2.5mm-2pt, sidebyside switch},% p.125 of manual
    boxrule=.5mm, 
    boxsep=1mm, 
    left=1pt,
    right=1pt,      
    sidebyside gap=\marginparsep+2mm+2pt, 
    fonttitle=\bfseries\Large, 
    code=\refstepcounter{mytcbcounter},
    title={Problem~\themytcbcounter{} #1}, 
  },
}

\newcounter{mytcbcounter}
\setcounter{mytcbcounter}{0}
\numberwithin{mytcbcounter}{chapter}

\begin{document}

\chapter{Introduction}

\begin{examplebox}{}
  I use my ``examplebox'' as usual text and margin figure work as they should be but I want my example box spread out to the right margin on an odd page, breakable, and spread out to the left margin on the even page.
  \tcblower
  \includegraphics[scale=0.25]{example-image}
  \captionof{figure}{I am figure}\label{fig:example-image}
\end{examplebox}


\tcbsidebyside[example box=Title here]{%
  I use my ``examplebox'' as usual text and margin figure work as they should be but I want my example box spread out to the right margin on an odd page, breakable, and spread out to the left margin on the even page.%
}{%
  \includegraphics[scale=0.25]{example-image-a}
  \captionof{figure}{I am a figure}\label{fig:example-image-a}%
}

\newpage

\begin{examplebox}{}
  \includegraphics[scale=0.25]{example-image-b}
  \captionof{figure}{I am b figure}\label{fig:example-image-b}
  \tcblower
  I use my ``examplebox'' as usual text and margin figure work as they should be but I want my example box spread out to the right margin on an odd page, breakable, and spread out to the left margin on the even page.
\end{examplebox}

\tcbsidebyside[example box]{% with no title
  I use my ``examplebox'' as usual text and margin figure work as they should be but I want my example box spread out to the right margin on an odd page, breakable, and spread out to the left margin on the even page.%
}{%
  \includegraphics[scale=0.25]{example-image-c}
  \captionof{figure}{I am c figure}\label{fig:example-image-c}%
}

\end{document}
  • Wow!. That's awesome. Thank you very much. I really appreciate your help. It's work but like you said, we can't get everything as we want. The box is unbreakable through the second page. – kmmm Mar 7 '18 at 6:07
  • @kmmm Glad it helps. I know it doesn't do everything you hoped for. I think that may not be possible (without rewriting fundamental things - certainly well beyond my abilities). There are limitations on what can be made breakable in tcolorbox. See the manual for details (though the details aren't all grouped together, unfortunately.) A tcolorbox expert could tell you for sure, but breakable may just be ignored here if the option isn't compatible with the rest. – cfr Mar 7 '18 at 18:23
  • @kmmm You could try asking in chat. Christian Hupfer is a real wiz with this package, for example. – cfr Mar 7 '18 at 18:25
  • I really appreciate your help and time. At least, we knew that using tcbsidebyside will make the box unbreakable. How to ask Christian Hupfer in chat as you mentioned?. – kmmm Mar 8 '18 at 14:22
  • cfr: What's the different between normal \newtcolorbox and your \NewTColorBox ? – kmmm Mar 14 '18 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.