为什么包含多栅格列 n/.style 的宏定义在 tcolorbox 的 \tcbset 中失败

为什么包含多栅格列 n/.style 的宏定义在 tcolorbox 的 \tcbset 中失败

我编写了一个宏\allcw,其中包含一些栅格列宽度样式的定义,如下所示:

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

请查看下面的代码。但是它无法编译?我的代码有什么问题?

\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}

答案1

您只需添加反斜杠(并删除cwall,这在您的示例中未定义)。

\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}

在此处输入图片描述

至于你真正的问题:类似的问题已经得到解答这里这里例如。因此,您可以这样做

\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}

在此处输入图片描述

相关内容