8

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.

12

@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

| improve this answer | |
9

One way to do this is to use the keys grow to left by=<length> and grow to right by=<length>

\begin{tcolorbox}[grow to left by=-1cm,grow to right by=-2cm]

screenshot

Here's a complete MWE to play with.

% arara: pdflatex
\documentclass{article}
\usepackage[showframe=true]{geometry}
\usepackage{tcolorbox}
\usepackage{lipsum}

\begin{document}

\begin{tcolorbox}[grow to left by=-1cm,grow to right by=-2cm]
\lipsum[1]  
\end{tcolorbox}
\begin{tcolorbox}[right=3cm]
\lipsum[2]  
\end{tcolorbox}
\end{document}

There may be another way to do this-perhaps the author will stop by and confirm, he is a regular visitor to the site.

| improve this answer | |
  • 2
    +1 I confirm you answer :-) Nevertheless, I added another answer to add some explanations which would not fit into this comment. – Thomas F. Sturm Apr 6 '14 at 9:33
  • @ThomasF.Sturm wonderful, thanks for confirming, and for this superb package :) – cmhughes Apr 6 '14 at 16:34

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.