TransWikia.com

Can I avoid indentation of margin items when using parbox=false in a tcolorbox?

TeX - LaTeX Asked by Sean Fitzpatrick on July 24, 2021

For a book project, examples, theorems, etc. are all styled using tcolorbox. (This part is not really negotiable — the LaTeX code is generated by PreTeXt.)

I’ve been trying to figure out how to get some figures, as well as asides, into the margin.

My most recent attempt used eso-pic, but I ran into a problem: if the figure I want to be in the margin is part of an example (which is a tcolorbox environment) that spans 3 pages, I can’t get a figure on the second page. (The AddToShipoutPicture* command takes its page reference from the top of the tcolorbox.)

I found this example, which creates a tcolorbox in the margin for the figure/aside.

This does almost exactly what I want. But the example tcolorbox uses the option parbox=false, because we want paragraphs in an example. With this option present, the figure in the margin gets indented by 30pt.

Is there a way to avoid this, other than using separate environments for figures inside/outside an example? Here is an example to illustrate:

documentclass[twoside]{article}
usepackage[breakable, theorems, skins]{tcolorbox}
%tcbset{enhanced}
usepackage[showframe,letterpaper]{geometry}
usepackage{ifthen}
usepackage{lipsum}
geometry{inner=1in,textheight=9in,textwidth=320pt,marginparwidth=150pt,marginparsep=20pt,bottom=1in,footskip=29pt}


newtcbtheorem[number within = section]{myexample}{Example}%
    {breakable,
    colback=white, colbacktitle=white, coltitle=black,
        parbox=false,}{ex}

newtcbtheorem[number within=section]{myfig}{My Theorem}%
    {colback=white, colbacktitle=white,coltitle=black,
     fonttitle=bfseries,
     }{th}    

newsavebox{mymargbox}
newcommand{marginfig}[1]{%
sbox{mymargbox}{vbox{%
       linewidth=marginparwidth%
       #1%
   }}
   ifthenelse{isodd{thepage}}{
   leftskip -325pt%
   usebox{mymargbox}
   leftskip 325pt%
   }
   {leftskip 186pt%
   usebox{mymargbox}
   leftskip -186pt%
   }
  vspace*{-htmymargbox}
  }
begin{document}
begin{myexample}{title}{label}
{
lipsum[1]

par

lipsum[2]

{

marginfig{
begin{myfig}{title2}{label2}
begin{itemize}
item An item
item Another item
item A somewhat longer item that is a bit longer than the margin width
end{itemize}
end{myfig}
}

}

lipsum[5]
}
end{myexample}

{

marginfig{
begin{myfig}{title3}{label3}
begin{itemize}
item An item
item Another item
item A somewhat longer item that is a bit longer than the margin width
end{itemize}
end{myfig}
}

}


lipsum[1-2]

{

marginfig{
begin{myfig}{title4}{label4}
begin{itemize}
item An item
item Another item
item A somewhat longer item that is a bit longer than the margin width
end{itemize}
end{myfig}
}

}

lipsum[3]

par

begin{myexample}{Title5}{label5}
lipsum[2]


{

marginfig{
begin{myfig}{title6}{label6}
begin{itemize}
item An item
item Another item
item A somewhat longer item that is a bit longer than the margin width
end{itemize}
end{myfig}
}

}

end{myexample}
end{document}

output of MWE

One Answer

I propose another approach (much easier to understand) to put the theorem boxes on the margin.

Using the package xcoffin you can put any material anywhere on the page. The reference point is the current insertion point.

Because there are some theorems that come from inside a tcolorbox and others from the text area, it takes two macros to account for the different margins involved.

(1) insertTheoBox{<content>}{<vertical shift>}

(2) insertTheoPara{<content>}{<vertical shift>}

The vertical shift allows for a vertical displacement inside the margin area.

Two inner "boxes" are used in the macros: Framex is a zero depth box that allows for the Theox box to be joined at a proper position.

The result of the assembly is then typeset into the margin area.

x

UPDATED Minor issues corrected. No hardwired dimensions.

%%%% version 3

documentclass[twoside]{article}
usepackage[breakable, theorems, skins]{tcolorbox}
%tcbset{enhanced}
usepackage[showframe,letterpaper]{geometry}
usepackage{ifthen}
usepackage{lipsum}
geometry{inner=1in,textheight=9in,textwidth=320pt,marginparwidth=150pt,marginparsep=20pt,bottom=1in,footskip=29pt}

newtcbtheorem[number within = section]{myexample}{Example}%
{breakable,
    colback=white,
    colbacktitle=white,
    coltitle=black,
    parbox=false,
 %  boxsep=14mm,  % for testing
}{ex}

newtcbtheorem[number within=section]{myfig}{My Theorem}%
{colback=white, colbacktitle=white,coltitle=black,
    fonttitle=bfseries,
}{th}  


%%*************************************************************** v3
usepackage{xcoffins} %added
NewCoffinFramex
NewCoffinTheox

usepackage{changepage} %added
strictpagecheck

newlength{Textw} % save textwidth outside the boxes
setlength{Textw}{textwidth}
newlength{Hshift}
newlength{Mshift}

newcommand*{calculateMshift}{%
    checkoddpage
    ifoddpage
        setlength{Mshift}{marginparsep}
    else
        setlength{Mshift}{dimexpr-marginparsep-textwidth-marginparwidthrelax}
    fi}

    newcommand*{calculateHshift}{%
    checkoddpage
    ifoddpage
        setlength{Hshift}{dimexprTextw/2-tcbtextwidth/2relax}
    else
        setlength{Hshift}{dimexpr-Textw/2+tcbtextwidth/2relax}
    fi}
    
newcommand{insertTheoBox}[2]{% v2 updated         
    par %start a new line
    calculateMshift
    calculateHshift    
    SetHorizontalCoffinFramex{color{blue}rule{tcbtextwidth}{0pt}} %clear box Framex
    SetVerticalCoffinTheox{marginparwidth}{#1}% fill box Theox
    JoinCoffins*Framex[r,vc]Theox[l,vc](dimexprMshift+Hshiftrelax,#2)%join boxes
    noindentTypesetCoffinFramex[-2baselineskip] %typeset assembly
}

newcommand{insertTheoPara}[2]{% v2 updated
    par
    calculateMshift    
    SetHorizontalCoffinFramex{color{red}rule{textwidth}{0pt}}
    SetVerticalCoffinTheox{marginparwidth}{#1}
    JoinCoffins*Framex[r,vc]Theox[l,vc](Mshift,#2)  
    noindentTypesetCoffinFramex[-2baselineskip]
}
%%***************************************************************

begin{document}    

    begin{myexample}{title}{label}
        1. lipsum[1]           
        insertTheoBox{%
            begin{myfig}{title1}{label1}
                begin{itemize}
                    item An item
                    item Another item
                    item A somewhat longer item that is a bit longer than the margin width
                end{itemize}
            end{myfig}
        }{70pt}         
                
    2.  lipsum[2]          
    insertTheoBox{%
        begin{myfig}{title2}{label2}
            begin{itemize}
                item An item
                item Another item
                item A somewhat longer item that is a bit longer than the margin width
            end{itemize}
        end{myfig}
    }{50pt}
            
    3.  lipsum[5]      
    end{myexample}

    insertTheoPara{%       
    begin{myfig}{title3}{label3}
        begin{itemize}
            item An item
            item Another item
            item A somewhat longer item that is a bit longer than the margin width
        end{itemize}
    end{myfig}
        }{-40pt}    

    
    4.  lipsum[1-2]                    
    insertTheoPara{%
    begin{myfig}{title4}{label4}
        begin{itemize}
            item An item
            item Another item
            item A somewhat longer item that is a bit longer than the margin width
        end{itemize}
    end{myfig}
    }{-20pt}
    
    5.  lipsum[3]  
    
        insertTheoPara{%
        begin{myfig}{title5}{label7}
            begin{itemize}
                item An item
                item Another item
                item A somewhat longer item that is a bit longer than the margin width
            end{itemize}
        end{myfig}
    }{-250pt}
    
    begin{myexample}{Title5}{label5}
    6.  lipsum[2]  
    
        insertTheoBox{%
        begin{myfig}{title6}{label6}
            begin{itemize}
                item An item
                item Another item
                item A somewhat longer item that is a bit longer than the margin width
            end{itemize}
        end{myfig}
    }{60pt}     
            
    end{myexample}
end{document}

The margin boxes and their positions will automatically adjust to changes in the page geometry or the internal geometry of the boxes in the text area.

For the next figure

geometry{inner=1in,textheight=9in,
    textwidth=220pt, % changed <<<<<
    marginparwidth=220pt,  % changed <<<<<
    marginparsep=40pt,  % changed <<<<<
    bottom=1in,footskip=29pt}

    newtcbtheorem[number within = section]{myexample}{Example}%
{breakable,
    colback=white,
    colbacktitle=white,
    coltitle=black,
    parbox=false,
    boxsep=14mm,  % added <<<<<
}{ex}

y

Correct answer by Simon Dispa on July 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP