16

I'd like to know if there is a way to place tcolorboxes like this:

-------- --------
| box1 | |      |
-------- | box3 |  
-------- |      | 
| box2 | |      |
-------- --------

To do that I'm actually placing the boxes inside tikz nodes, but that seems like an overkill solution. Is there a tikz-free approach?

15

Obviously, you would use the \tcbox macro version for your boxes, rather than the environment form. Calculation of the stackgap is shown automated in this MWE. (Note: this example works starting with V2.30 of tcolorbox).

\documentclass{article}
\usepackage{stackengine}
\usepackage{tcolorbox}
\begin{document}

\def\boxone{\tcbox[nobeforeafter]{This is a test}}
\def\boxtwo{\tcbox[nobeforeafter]{Another test}}
\def\boxthree{\tcbox[nobeforeafter,minipage,width=0.7in]{this is a very long box}}

\stackon[\heightof{\boxthree}-\heightof{\boxone}-\heightof{\boxtwo}]%
  {\boxtwo}{\boxone}~\boxthree

\end{document}

enter image description here

  • Does this work with tcolorbox? – Werner Jul 3 '13 at 14:14
  • Should work with tcolorbox; just remember to give the boxes a fixed width (and, maybe, height). – Thomas F. Sturm Jul 3 '13 at 14:17
  • never heard of this package before, thanks. Anyway, is it possible to align the third box with the first, assuming that the third box height is not equal to the sum of the other two boxes? @Werner just tested, it works. – Lep Jul 3 '13 at 14:17
  • 1
    @Lep The stackengine package is new. I just wrote it in recent weeks. – Steven B. Segletes Jul 3 '13 at 14:27
  • 1
    @StevenB.Segletes Actually, \tcbox was buggy before 2.30 because it slurped the before and after code. After bug-fixing it has the expected behavior. That means, on the other hand, that you have to clear the before and after code if you want to use \tcbox like \mbox. I've edited your example and added nobeforeafter to do that. Otherwise, you could make a new box by \newtcbox{\mybox}[1][]{nobeforeafter,#1}. This \mybox behaves like the buggy old \tcbox and you have the bug as a feature :-) – Thomas F. Sturm Jul 4 '13 at 6:05
11

Taking a similar tack to Steven Segletes's answer but using the xcoffins package, which allows us to pick points on the various boxes to line up:

\documentclass{article}
\usepackage{tcolorbox}

\usepackage{xcoffins}
\NewCoffin{\CoffinA}
\NewCoffin{\CoffinB}
\NewCoffin{\CoffinC}
\begin{document}
% Set up content in three coffins
\SetHorizontalCoffin{\CoffinA}{%
  \begin{tcolorbox}[width = 1 in]
    This is a \textbf{tcolorbox}.%
  \end{tcolorbox}
}%
\SetHorizontalCoffin{\CoffinB}{%
  \begin{tcolorbox}[width = 1 in]
    This is a \textbf{tcolorbox}.%
  \end{tcolorbox}
}%
\SetHorizontalCoffin{\CoffinC}{\rule{1 in}{2.5 in}}%
% Join the first small coffin onto the big one at the top
\JoinCoffins{\CoffinC}[t,l]{\CoffinA}[t,r](-0.1 in, 0 in)%
% Join the second small coffin onto the big one at the bottom
\JoinCoffins{\CoffinC}[b,\CoffinC-l]{\CoffinB}[b,r](-0.1 in, 0 in)%
\TypesetCoffin{\CoffinC}

\end{document}

Here, I've added a small gap between the coffins (it's negative as I've done everything relative to the large item, \CoffinC.)

The 'coffin' system works by having 'handles' on the boxes it creates: we can address those by letters, e.g. t for 'top', and also refer to points on coffins we've already combined. This allows relatively easy construction of complex box relationships.

  • The package xcoffin is a great interface to l3coffin. Unfortunately it is very unpopular yet. – Marco Daniel Jul 3 '13 at 17:09
  • @MarcoDaniel Maybe because it is in the experimental branch? – clemens Jul 6 '13 at 15:58
1

tcbraster is a tcolorbox library for distribution of boxes inside another box.

With a raster inside another raster is easy to obtain something similar to desired distribution.

\documentclass{article}
\usepackage[most]{tcolorbox}
\begin{document}

% External raster
\begin{tcbraster}[%
    raster width=.6\linewidth,
    raster columns=2, 
    % Forces same height in all columns on same row
    raster equal height=rows,
    raster every box/.style={valign=center, halign=center}, 
    ]
% Inner raster: left column boxes
\begin{tcboxedraster}[%
    raster columns=1, 
    raster width=\linewidth, %otherwise inherits `.6\linewidth`
    ]{blankest} %<- no drawn box or white space around inner boxes
    \begin{tcolorbox}
        This is a text
    \end{tcolorbox}
    \begin{tcolorbox}[blankest, height=1cm]
        % This is a phantom box. It introduces a vertical gap
    \end{tcolorbox}
    \begin{tcolorbox}
        This is another text
    \end{tcolorbox}
\end{tcboxedraster}
% <--- Important: No white space or break line between elements
% Box for right hand column.
\begin{tcolorbox}
    This is a very long long text
\end{tcolorbox}
\end{tcbraster}

\end{document}

enter image description here

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.