PGF 密钥将参数传递给多个环境

PGF 密钥将参数传递给多个环境

我想将两者结合起来彩色盒子文本位置将框放置到页面中的特定位置。

以下是 MWE:

\documentclass[]{article}


\usepackage{pgfkeys}
\usepackage{tcolorbox}
\usepackage[absolute,overlay]{textpos}

\makeatletter

%% #1: options
%% #2: textblock width
%% #3: textblock position
\newenvironment{tcolorboxat}[3][]{%
  \pgfkeys{/tcbat/.is family,
    /tcbat/.cd,
    ho/.initial = 0,
    vo/.initial = 0,
    #1,
  }%
  \begin{textblock*}{#2}[\pgfkeysvalueof{/tcbat/ho},\pgfkeysvalueof{/tcbat/vo}](#3)%
    \begin{tcolorbox}[]%
}{\end{tcolorbox}\end{textblock*}}%

\makeatother


\begin{document}
\begin{tcolorboxat}[ho=0.5]{5cm}{3cm,8cm}
  This is a tcolorbox placed at 3cm, 8cm from top left corner of the page. Origin point is 1.5cm, 8cm.
\end{tcolorboxat}
\end{document}

这按预期工作。现在我想添加一个tcb opts类似样式的选项来传递给tcolorbox环境:

\newenvironment{tcolorboxat}[3][]{%
  \pgfkeys{/tcbat/.is family,
    /tcbat/.cd,
    ho/.initial = 0,
    vo/.initial = 0,
    tcb opts/.style = { title = box title },
    #1,
  }%
  \begin{textblock*}{#2}[\pgfkeysvalueof{/tcbat/ho},\pgfkeysvalueof{/tcbat/vo}](#3)%
    \begin{tcolorbox}[code=\pgfkeysvalueof{/tcbat/tcb opts}]%
}{\end{tcolorbox}\end{textblock*}}%

不幸的是,这不起作用,该title选项似乎没有传递给tcolorbox

有没有办法将所有tcolorbox选项放入类似于键的样式?

\begin{tcolorboxat}[ho=0.4,
    tcb opts = {
      title = box title,
      colback = red,
  }]{5cm}{3cm,8cm}
  ...
\end{tcolorboxat}

提前致谢。

答案1

您可以尝试这样的操作:

\documentclass[]{article}

\usepackage{pgfkeys}
\usepackage{tcolorbox}
  \tcbuselibrary{breakable}
  \tcbuselibrary{skins}
\usepackage[absolute,overlay]{textpos}

\makeatletter

\pgfkeys{/tcbat/.is family,
  /tcbat/.cd,
  ho/.store in = \mytcbat@ho,
  vo/.store in = \mytcbat@vo,
  tcb opts/.code = {\tcbset{mytcboptions/.style={#1}}},
}%
%

%% #1: options
%% #2: textblock width
%% #3: textblock position
\newenvironment{tcolorboxat}[3][]{%
  \tcbset{mytcboptions/.style={}}%  %% Reinitialize new style
  %
  \pgfkeys{/tcbat, ho=0, vo=0, #1}%  %% Set defaults, whatever you may want them to be...
  %
  \begin{textblock*}{#2}[\mytcbat@ho, \mytcbat@vo](#3)%
    \begin{tcolorbox}[mytcboptions]%
}{\end{tcolorbox}\end{textblock*}}%

\makeatother


\begin{document}
\begin{tcolorboxat}[ho=0.5, tcb opts={unbreakable, enhanced, title={Testing 123}}]{5cm}{3cm,8cm}
  This is a tcolorbox placed at 3cm, 8cm from top left corner of the page. Origin point is 1.5cm, 8cm.
\end{tcolorboxat}

\tcbset{%
  %% Define a new skin for tcolorbox called title3d
  title3d/.style = {%
      skin=enhancedlast jigsaw,
      tikznode boxed title={inner sep=1mm, align=left},
      attach boxed title to top left={xshift=-4mm,yshift=-0.5mm},
      fonttitle=\bfseries\sffamily,
      colbacktitle=blue!45!white,
      colframe=red!50!black,
      interior style={top color=blue!10!white,bottom color=red!10!white},
      boxed title style={empty,arc=0pt,outer arc=0pt,boxrule=0pt},
      underlay boxed title={
          \fill[blue!45!white] (title.north west) -- (title.north east)
              -- +(\tcboxedtitleheight-1mm,-\tcboxedtitleheight+1mm)
              -- ([xshift=4mm,yshift=0.5mm]frame.north east) -- +(0mm,-1mm)
              -- (title.south west) -- cycle;
          \fill[blue!45!white!50!black] ([yshift=-0.5mm]frame.north west)
              -- +(-0.4,0) -- +(0,-0.3) -- cycle;
          \fill[blue!45!white!50!black] ([yshift=-0.5mm]frame.north east)
              -- +(0,-0.3) -- +(0.4,0) -- cycle; },
  },
}

\begin{tcolorboxat}[ho=0.5, tcb opts={unbreakable, enhanced, title3d, title={So pretty}}]{7cm}{6cm,14cm}
  This is a tcolorbox placed at 6cm, 14cm from top left corner of the page.
\end{tcolorboxat}
\end{document}

输出

与上述代码相关的输出

答案2

@feculededentier 这个解决方案可以解决问题。谢谢。

同时,我仍然想知道声明PFG键样式选项的最佳做法是什么,因为此代码也有效:

\newenvironment{tcolorboxat}[3][]{%
  \pgfkeys{/tcb/tcbat/.style = {},
    /tcbat/.is family,
    /tcbat/.cd,
    ho/.initial = 0,
    vo/.initial = 0,
    tcb opts/.code = {\tcbset{tcbat/.style={##1}}},
    #1,
    ho/.get = \tcbat@ho,
    vo/.get = \tcbat@vo,
  }
  \begin{textblock*}{#2} [\tcbat@ho, \tcbat@vo](#3)
    \begin{tcolorbox}[tcbat]%
}{\end{tcolorbox}\end{textblock*}}%

优点是所有声明都在一个范围内。但很多人似乎PGF在环境声明之外声明键。

相关内容