3

The attached figure is constructed by raster of tcolorbox. Though the option raster equal height=rows is used, the height of left column differs from that of right column because the content of box in right column is too big(please see the attatched figure). That is to say, though the option raster equal height=rows does work fine -- the height of the left tcbitemize is equal to the right box -- unwanted blank is produced.

My question is: how to automatically and evenly adjust the height of boxes in left column to get a visually equal height.

MWE:

\documentclass[a4paper,12pt]{article}

\usepackage[most]{tcolorbox}
\tcbuselibrary{raster}

\begin{document}
\begin{tcbitemize}[raster equal height=rows,
raster every box/.style={colframe=red!50!black,colback=red!10!white}]
\tcbitem[blankest]
\begin{tcbitemize}[raster columns=1,raster row skip=0pt]
\tcbitem One
\tcbitem Two
\end{tcbitemize}
\tcbitem \huge tcbitemize\\tcbitem\\tcbitemize
\end{tcbitemize}
\end{document}

enter image description here

2 Answers 2

3

I propose to use /tcb/add to natural height in the subitems that may need to grow. Using this key, one can pass the excess height to distribute to each subitem, as a length computed from a macro defined by the /tcb/space to key used in the first \tcbitem (/tcb/space to substracts the natural height of the box from the height that was asked for and stores the result in the macro whose name was passed in the argument). With this method, you need to write the number of items that may need to grow to match the space requirements in one place. Example for two items:

code={\setlength{\mylen}{\dimexpr \myspace/2}}

The main advantage of this solution is that is doesn't require to hardcode any height. The right column may be taller or shorter than the left one, this doesn't cause any problem: boxes in the shortest column automatically adapt to the height of the taller column.

Several compilation runs are needed for convergence (it seems that three compilation runs are usually enough).

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{raster, skins}

\newlength{\mylen}

\begin{document}

\begin{tcbitemize}[raster equal height=rows,
                   raster every box/.style={colframe=red!50!black,
                                            colback=red!10!white}]
  \tcbitem[blankest, space to=\myspace,
           code={\setlength{\mylen}{\dimexpr \myspace/2}}]
  \begin{tcbitemize}[
    raster columns=1, raster row skip=0pt,
    raster every box/.append style={add to natural height=\mylen}]
    \tcbitem One
    \tcbitem Two
  \end{tcbitemize}
  \tcbitem \huge tcbitemize\\ tcbitem\\ tcbitemize
\end{tcbitemize}

\end{document}

Screenshot

Variant that shows how to automatically center (both horizontally and vertically) the contents of the boxes in the left column:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{raster, skins}
\usepackage{lipsum}

\newlength{\mylen}

\begin{document}

\begin{tcbitemize}[raster equal height=rows,
                   raster every box/.style={colframe=red!50!black,
                                            colback=red!10!white}]
  \tcbitem[blankest, space to=\myspace,
           code={\setlength{\mylen}{\dimexpr \myspace/2}}]
  \begin{tcbitemize}[
    raster columns=1, raster row skip=0pt,
    raster every box/.append style={halign=center, valign=center,
                                    add to natural height=\mylen}]
    \tcbitem One
    \tcbitem Two
  \end{tcbitemize}
  \tcbitem \lipsum[1]
\end{tcbitemize}

\end{document}

Screenshot

What happens when the stacked items on the left are taller (in total) than the natural height of the box in the second column:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{raster, skins}

\newlength{\mylen}

\begin{document}

\begin{tcbitemize}[raster equal height=rows,
                   raster every box/.style={colframe=red!50!black,
                                            colback=red!10!white}]
  \tcbitem[blankest, space to=\myspace,
           code={\setlength{\mylen}{\dimexpr \myspace/2}}]
  \begin{tcbitemize}[
    raster columns=1, raster row skip=0pt,
    raster every box/.append style={add to natural height=\mylen}]
    \tcbitem One
    \tcbitem Two
  \end{tcbitemize}
  \tcbitem Foo bar.
\end{tcbitemize}

\end{document}

Screenshot

Finally, with three items in the left column (any number can be used, of course):

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{raster, skins}
\usepackage{lipsum}

\newlength{\mylen}

\begin{document}

\begin{tcbitemize}[raster equal height=rows,
                   raster every box/.style={colframe=red!50!black,
                                            colback=red!10!white}]
  \tcbitem[blankest, space to=\myspace,
           code={\setlength{\mylen}{\dimexpr \myspace/3}}]
  \begin{tcbitemize}[
    raster columns=1, raster row skip=0pt,
    raster every box/.append style={add to natural height=\mylen}]
    \tcbitem One
    \tcbitem Two
    \tcbitem Three
  \end{tcbitemize}
  \tcbitem \lipsum[1]
\end{tcbitemize}

\end{document}

Screenshot

Note: as it may not be obvious to everyone reading this, the reason why the outer tcbitemize has two columns in each example is that the initial value of /tcb/raster columns is 2 (cf. Library 'raster' in the tcolorbox manual). This initial value is used implicitly in the sample code from the OP, and I kept it so in all examples above.

4
  • Many many thanks for this so detail solution and your kindly help which solve my long time puzzle!! And, may I ask a further question that if there is a way to calculate rows number in tcbitemize? Thus the height of box in it can be adjusted automatically?
    – lyl
    Commented Aug 26, 2019 at 0:57
  • Not simply. I think the items are discovered on the fly and might come from the expansion of macros, so collecting the tcbitemize body (as done by amsmath's align or beamer's frame envs.) in order to count the items would probably be unreliable. More reliable would be to write the number of items to .aux when the env finishes, but this would require redefining the environment and would add complexity + one more compilation run. Not sure it is worth it.
    – frougon
    Commented Aug 26, 2019 at 5:59
  • Maybe Thomas F. Sturm (the author of tcolorbox) would be open to implement the “write the number of \tcbitems to the .aux file so that it can be used in the next run via a user-specified macro” approach if you kindly ask and provide him with the context—and maybe he would have a better solution than mine after seeing the problem.
    – frougon
    Commented Aug 26, 2019 at 8:04
  • Thank you very much for your proposal!
    – lyl
    Commented Aug 26, 2019 at 11:30
3

Like this? You can adjust the box height with raster height=0.075\textheight,

\documentclass[a4paper,12pt]{article}
\usepackage[most]{tcolorbox}
\tcbuselibrary{raster}

\begin{document}
\begin{tcbitemize}[raster columns=2, raster equal height,raster height=0.075\textheight,
raster every box/.style={colframe=red!50!black,colback=red!10!white}]
\tcbitem[raster multirow=4, blankest]
\begin{tcbitemize}[raster columns=1, raster rows=1]
\tcbitem One 
\tcbitem Two
\end{tcbitemize}
\tcbitem[enhanced,raster multicolumn=1, raster multirow=4] \huge tcbitemize\\tcbitem\\tcbitemize
\end{tcbitemize}
\end{document}

enter image description here

1
  • Thank you for your solution. In this example, there are two rows in the left column. In fact, the number of rows in left column is not certain, also the content of the right box. So, raster multirow=4, raster rows=1 and raster height=0.075\textheight are only fit for this example. I wonder if there is an universal method to fill vertical blank by evenly adjustment of box height in sub-tcbitemize so that equal height is abtained.
    – lyl
    Commented Aug 25, 2019 at 11:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .