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

lyl
  • 2,727
  • Welcome to TeX.SX! Which output is expacted? – Bobyandbob Aug 12 '17 at 09: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 '17 at 10:01
  • Your code does not compile, please edit the question. – Bobyandbob Aug 12 '17 at 10:10
  • Sorry, I forgot the "\end{tcbitemize}". I have just re-edit the code. – lyl Aug 12 '17 at 10:25
  • What is the purpose of style args here? – cfr Aug 12 '17 at 13:04
  • 1
    Your code still doesn't compile. – cfr Aug 12 '17 at 13:06
  • the style args is used to give addtional option to tcbitemze, for example, colback=green. cf1/cf2/cf3...means the format of column1/column2/column3... – lyl Aug 12 '17 at 13:09
  • @cfr maybe my code lack "usepackage{tikz}" in the preamble? I just add it. Please try again. Thank you. – lyl Aug 12 '17 at 13:13
  • But you could as easily use .style. There's no need for style args here. You aren't defining a non-standard input format. – cfr Aug 12 '17 at 13:13
  • 1
    Don't guess - test your code, please! tcolorbox is already loading tikz. – cfr Aug 12 '17 at 13:13
  • @cfr I tried my code once more in my computer. It can be compiled, and the result is shown as the figure I give in my question. – lyl Aug 12 '17 at 13:22
  • The reason why I use "style args" instead of "style" is, the style of cf contains not only the addtional options (such as "colback=green", but also some fix options (for example, raster column 3/.style={width=0.3\textwidth}. If I want to add "colback=green" to the column number 3, I must try to keep the width=0.3\textwidth. So the best way is the "style args". When I wan to add some options to the column number 3, I just input "cf3={colback=green}, meanwhile, the width of column number 3 won't be changed. – lyl Aug 12 '17 at 13:30
  • That's not what style args does. my style/.style args={#1:#2 over #3}{...} defines a style with the syntax my style=<value>:<value> over <value> or whatever. It doesn't add to an existing list of options. Possibly you want append style instead. As for the code, all I can say is that it doesn't compile here and I don't believe that exactly that code compiles elsewhere either. But maybe I just have a deficient LaTeX format. – cfr Aug 12 '17 at 16:08
  • @cfr. Sorry, I think Ulrike Fischer is right. My code lacks declaraction of the package "ifthen" on web page. In my Latex IDE, this package is not necessary to specially declare. – lyl Aug 13 '17 at 02:31

1 Answers1

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

Ulrike Fischer
  • 327,261
  • 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 '17 at 02:23
  • Use ####1 instead of ##1. – Ulrike Fischer Aug 13 '17 at 07: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 '17 at 07:20
  • See https://tex.stackexchange.com/questions/42463/what-is-the-meaning-of-double-pound-symbol-number-sign-hash-character-1-in – Ulrike Fischer Aug 13 '17 at 08:50