2

I make up a macro \allcw which contains some definition of raster column width style like this:

raster column 1/.style={width=2em},
raster column 2/.style={width=6em},
raster column 3/.style={width=12em}

Please see my code below. But it does not be compiled? What's wrong with my code?

\documentclass{article}

\usepackage[most]{tcolorbox}
\usepackage{xstring}
\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\columnwidthlist{2em,6em,12em,}
\newcounter{step}
\newcommand{\allcw}{%
  \setcounter{step}{0}
  \def\mystore{}
  \foreach \x in \columnwidthlist {
    \stepcounter{step}
    \IfStrEq{\x}{}{}
      {%
       \xappto\mystore{
          raster column \thestep/.style=\{width=\x\},
        }
      }
    }
  \tcbset{cwall/.style={code={\pgfkeysalsofrom{\mystore}}}}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tcbitemize}[raster columns=3,raster force size=false,code={\allcw},cwall]
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
\end{tcbitemize}

\end{document}
M. Al Jumaily
  • 4,035
  • 2
  • 11
  • 26
lyl
  • 2,727
  • 1
    I guess \foreach \x in columnwidthlist doesn't work because columnwidthlist is just a string, not a list. If you replace that by \foreach \x in \columnwidthlist, there is still an error (perhaps unrelated), but the loop works. –  Oct 10 '18 at 14:22
  • Sorry, that is a mistake in writing which I have just correct. The code still can not be compiled. – lyl Oct 10 '18 at 14:25

1 Answers1

3

You only have to add backslash (and remove cwall, which is not defined in your example).

\documentclass{article}

\usepackage[most]{tcolorbox}
\usepackage{xstring}
\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\def\columnwidthlist{2em,6em,12em,}
\newcounter{step}
\newcommand{\allcw}{%
  \setcounter{step}{0}
  \def\mystore{}
  \foreach \x in \columnwidthlist {
    \stepcounter{step}
    \IfStrEq{\x}{}{}
      {%
       \xappto\mystore{
          raster column \thestep/.style=\{width=\x\},
        }
      }
    }
  \tcbset{cwall/.style={code={\pgfkeysalsofrom{\mystore}}}}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tcbitemize}[raster columns=3,raster force size=false] % ,cwall
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
\end{tcbitemize}

\end{document}

enter image description here

As for your real question: analogous things have been answered here and here, for instance. So you might do

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tcbset{set width/.code args={#1}{\stepcounter{step}
\pgfkeysalso{/tcb/raster column \thestep/.style/.expanded={width=#1}}
},
set widths/.code={\setcounter{step}{0}
\pgfkeys{/tcb/set width/.list/.expanded={#1}}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\tcbset{myset/.style args={raster column #1/.style={width=#2}}}
\begin{tcbitemize}[raster columns=3,raster force
size=false,set widths={2em,6em,12em}]
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
\end{tcbitemize}

enter image description here

  • But the cwall/.style is what I need to have each column width modified. And I define this style in my code as like this: \tcbset{cwall/.style={code={\pgfkeysalsofrom{\mystore}}}} – lyl Oct 10 '18 at 14:29
  • 1
    @lyl I guess you need to explain better what you want to do. Where is your command \allcw called that will define the style? –  Oct 10 '18 at 14:33
  • @ marmot I just correct my code and \allcw is called in option part of the tcbitemize. What I want is to set each column width by my self, 2em, 6em ,12em in my example. – lyl Oct 10 '18 at 14:40
  • @lyl I added one possibility, most likely there is a more elegant way based on /.list.... –  Oct 10 '18 at 15:04
  • @ marmot Thank you very much for your help. And could you please tell me what's the meaning "\globaldefs=1\relax"? – lyl Oct 11 '18 at 03:06
  • @lyl I replaced it by a cleaner proposal. –  Oct 11 '18 at 03:34
  • @ marmot but I think {2em, 6em, 12em} is better than {1/2em,2/6em,3/12em} – lyl Oct 11 '18 at 04:17
  • @lyl I modified the version accordingly. –  Oct 11 '18 at 04:30
  • Many thanks for your patience! I find your first version of solution is more understandable.Could you tell me which package define the macro \typeout and its function in this example. Another question, why is \relax needed after \globaldefs=1? – lyl Oct 11 '18 at 06:08
  • 1
    @lyl *DON'T* (note boldface italic all caps) use \globaldefs if you don't precisely know what it's about. It's *VERY* dangerous. – egreg Oct 11 '18 at 10:24
  • @egreg More dangerous than pineapple on a pizza? ;-) –  Oct 13 '18 at 21:53