3

The following MWE constructs raster boxes of tcolorbox.

There are two problems with these code:

  1. Boxes do not rank on line, but in different rows.
  2. The second row in #2 box is not filled.

Please see the attached figure for details.

Anyone can help me with the above two problems? Thank you.

MWE:

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

\begin{document}
\newcommand{\myset}[1]{
  \def\columns{#1}
}

\tcbset{a/.style={
    code={\myset{#1}},
    raster multicolumn=\columns,
  }}

\begin{tcbitemize}[title=\#\thetcbrasternum,raster columns=4,raster equal height=rows,raster width=\linewidth]
  \tcbitem 1
  \tcbitem[%
  code={\myset{3}},%
  raster multicolumn=\columns,
  ]
  \begin{tcbitemize}[raster columns=\columns]
    \tcbitem columns now is: \columns \tcbitem 1\tcbitem 1
    \tcbitem[a=2] columns now is: \columns \tcbitem 2 
  \tcbitem columns now is: \columns
  \end{tcbitemize}
  \tcbitem 3
\end{tcbitemize}

\end{document} 

enter image description here

2
  • What is the purpose of the \myset command? Do you really need it afterwards?
    – AndréC
    Aug 24, 2019 at 14:34
  • Yes, \myset is really needed. As an example, it just difine a macro now. Later, more functions will be added to \myset.
    – lyl
    Aug 24, 2019 at 22:36

1 Answer 1

7

Problem 1

You need to replace:

\newcommand{\myset}[1]{
  \def\columns{#1}
}

with:

\newcommand{\myset}[1]{%
  \def\columns{#1}%
}

The code in the argument of /tcb/code is not passed to \pgfkeys, it is used in the body of the LaTeX document. Spaces matter and apparently prevent tcolorbox from seeing the immediately-following tcolorbox here.

Problem 2

In several places, you use code such as raster columns=\columns or raster multicolumn=\columns. This is dangerous, because you don't know whether the underlying code will store the control sequence token \columns or will (maybe) expand it and (hopefully) store the result of this expansion. This is what is causing your second problem. When a=2 is run from \tcbitem[a=2], this modifies the meaning of \columns which is stored as is by a previously-issued raster columns=\columns. I suggest to use the pgfkeys .expand once handler to expand the value of \columns before passing the result to raster columns or other tcolorbox keys, as in:

raster columns/.expand once=\columns

or

raster columns/.expand once={\columns}

Working code

\documentclass[a4paper,12pt]{article}
\usepackage[landscape, hmargin=3cm]{geometry} % example
\usepackage{tcolorbox}
\tcbuselibrary{raster}

\newcommand{\myset}[1]{%
  \def\columns{#1}%
}

\tcbset{a/.style={
    code={\myset{#1}},
    raster multicolumn/.expand once=\columns,
  }}

\begin{document}

\begin{tcbitemize}[title=\#\thetcbrasternum, raster columns=4,
                   raster equal height=rows, raster width=\linewidth]
  \tcbitem 1
  \tcbitem[code={\myset{3}}, raster multicolumn/.expand once=\columns]
    \begin{tcbitemize}[raster columns/.expand once=\columns]
      \tcbitem columns now is: \columns \tcbitem 1\tcbitem 1
      \tcbitem[a=2] columns now is: \columns \tcbitem 2
      \tcbitem columns now is: \columns
    \end{tcbitemize}
  \tcbitem 3
\end{tcbitemize}

\end{document}

Screenshot

Note

Of course, in case \columns is not needed in further code, you can pass the value directly and avoid some occurrences of problem 2 in a more straightforward way:

\tcbset{a/.style={raster multicolumn={#1}, whatever else...}}
4
  • Many thanks for your help. Your version perfectly solve my first problem. It seems that the second problem is still there after compiling your code.
    – lyl
    Aug 24, 2019 at 13:54
  • I had overlooked the second problem. Please see the latest edit.
    – frougon
    Aug 24, 2019 at 14:36
  • Note: someone retracted his upvote at about the same time as your comment was posted. I had already analyzed and fixed problem 1, but apparently this didn't deserve any upvote. Very well, but please bear in mind that I abhor this attitude of dangling a carrot in front of the person who is helping for free. If this kind of behavior continues, I'll stop answering questions and/or leave the site.
    – frougon
    Aug 24, 2019 at 15:21
  • This solution -- .expand once is so great! I really appreciate your help on my question!! And sorry for upvote so late.
    – lyl
    Aug 24, 2019 at 22:41

You must log in to answer this question.

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