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}
2
  • 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. – user121799 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
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

11
  • 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? – user121799 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.... – user121799 Oct 10 '18 at 15:04
  • 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

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.