[Tex/LaTex] Outer margin of tcolorbox

tcolorbox

I would like to be able to set outer margins in a tcolorbox environment. Something similar to the options leftmargin and rightmargin of the mdframed environment. Is it possible?

It seems to have options only for the width and for the inner margins.

Best Answer

@cmhughes has given the answer yet, which I gladly confirm hereby :-) The philosophy of the outer margins is slightly different between mdframed and tcolorbox. In tcolorbox, you manipulate the bounding box to achieve several effects like overlapping to the page margin and so on.

To get the same outer margins as in mdframed, you have to enlarge the bounding box to, say, all four directions. Since you do not want the box to overlap into the page margin, you have to shrink the width accordingly. This is, what the keys grow to left by and grow to right by do automatically (here: they shrink the box). For top and bottom, the height of the box has not to be changed; just enlarge the bounding box:

\documentclass{article}

\usepackage{lipsum}
\usepackage[many]{tcolorbox}

\begin{document}

\lipsum[1]

\begin{tcolorbox}[enhanced,colframe=blue,
  grow to left by=-2cm,%   equivalent to negative mdframed 'leftmargin'
  grow to right by=-2cm,%  equivalent to negative mdframed 'rightmargin'
  enlarge top by=1cm,%     equivalent to mdframed 'skipabove'
  enlarge bottom by=1cm,%  equivalent to mdframed 'skipbelow'
  show bounding box,% display the bounding box
  overlay app={% draw the green arrow lines 
    \begin{scope}[very thick,<->,green!50!black]
    \draw (frame.west) -- node[above] {2cm} ++(-2cm,0);
    \draw (frame.east) -- node[above] {2cm} ++(2cm,0);
    \draw (frame.north) -- node[right] {1cm} ++(0,1cm);
    \draw (frame.south) -- node[right] {1cm} ++(0,-1cm);
    \end{scope}}]
\lipsum[2]
\end{tcolorbox}

\lipsum[3]

\end{document}

enter image description here

Related Question