1

I'd like to define several option styles in one time, for example, cf1/.style args={#1}{#1},cf2/.style args={#1}{#1},cf3/.style args={#1}{#1},etc. I tried like this:

    \documentclass[12pt,a4paper]{article}
    \usepackage{tikz}
    \usepackage{tcolorbox}
    \tcbuselibrary{skins,breakable,raster}

        \begin{document}
        \newcounter{loopvariable}
        \setcounter{loopvariable}{5}
        \whiledo{\theloopvariable > 1}{%
                \addtocounter{loopvariable}{-1}
                \tcbset{cf\theloopvariable/.style args={#1}{%
raster column \theloopvariable/.style={#1}}}
            }
        %    
            \begin{tcbitemize}
                [raster force size=false,raster columns=4,sharp corners,
                boxrule=3pt,
                raster width=\textwidth,
                raster column skip=0pt,
                raster row skip=0pt,
                cf3={colback=green}]
                \tcbitem some text
                \tcbitem some text
                \tcbitem some text
                \tcbitem some text
            \end{tcbitemize}
    \end{document}

But the typeset is wrong(see the attached figure). So,could anyone tell me how to achieve the function I want?

enter image description here

14
  • Welcome to TeX.SX! Which output is expacted?
    – Bobyandbob
    Aug 12, 2017 at 9:57
  • I want the third column to be colored with green(by cf3={colback=green}), but the the first column is colored by green.
    – lyl
    Aug 12, 2017 at 10:01
  • Your code does not compile, please edit the question.
    – Bobyandbob
    Aug 12, 2017 at 10:10
  • 1
    Your code still doesn't compile.
    – cfr
    Aug 12, 2017 at 13:06
  • 1
    Don't guess - test your code, please! tcolorbox is already loading tikz.
    – cfr
    Aug 12, 2017 at 13:13

1 Answer 1

2

Your example miss the ifthen package, and your use of style args is odd, but your main problem is that the counter is not expanded so you are setting all styles for column one.

\documentclass[12pt,a4paper]{article}
    \usepackage{tikz}
    \usepackage{tcolorbox,ifthen}
    \tcbuselibrary{skins,breakable,raster}

        \begin{document}
        \newcounter{loopvariable}
        \setcounter{loopvariable}{5}
        \whiledo{\theloopvariable > 1}{%
                \addtocounter{loopvariable}{-1}
         \edef\next{%     
                \noexpand\tcbset{cf\number\value{loopvariable}/.style ={%
                raster column \number\value{loopvariable}/.style={##1}}}}
            \next
            }
        %
            \begin{tcbitemize}
                [raster force size=false,raster columns=4,sharp corners,
                boxrule=3pt,
                raster width=\textwidth,
                raster column skip=0pt,
                raster row skip=0pt,
                cf3={colback=green,width=5cm}]
                \tcbitem some text
                \tcbitem some text
                \tcbitem some text
                \tcbitem some text
            \end{tcbitemize}
    \end{document}

enter image description here

4
  • Thank you Fischer, the effect and funtion of your code is exactly what I want. But when I put your code in a defination of newcommand, that is so say,"\newcommand{\cmd}{\whiledo...} and call \cmd later, the code can not be compiled, showing "Illegal parameter number in definition of \next". How to solve this problem? Thank you.
    – lyl
    Aug 13, 2017 at 2:23
  • Use ####1 instead of ##1. Aug 13, 2017 at 7:09
  • Thank you very much for your response. I have never see "##" or "####" in latex book. Does it relate to \edef? Could you please tell me more about it, because maybe this method used in other objects, but I don't quite understand it.
    – lyl
    Aug 13, 2017 at 7:20

You must log in to answer this question.

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