tcolorbox:如何读出左、右等值,

tcolorbox:如何读出左、右等值,

我如何读出这样的值left, right,..., boxsep
我试过了\pgfkeysvalueof{/tcb/left}。我该怎么办?

在此处输入图片描述

\documentclass{article}
\usepackage[most]{tcolorbox}
\begin{document}
\begin{tcolorbox}[left=22pt]
The left skip was setted to: \pgfkeysvalueof{/tcb/left}  ?
\end{tcolorbox}
\end{document}

答案1

\pgfkeysLastValueOf{<full key>}这里提供了一个新命令。您可以使用\pgfkeysLastValueOf{/tcb/left}它来检索 的当前值(或最近传递的值)/tcb/left

\documentclass{article}
\usepackage{pgfkeys}
\usepackage{xpatch}

\makeatletter
\xpatchcmd\pgfkeys@unpack
  {\pgfkeys@case@one}
  { 
    \pgfkeyslet{\pgfkeyscurrentkey/.@last}{\pgfkeyscurrentvalue}%
    \pgfkeys@case@one
  }
  {}{\fail}

\def\pgfkeysLastValueOf#1{\csname pgfk@#1/.@last\endcsname}
\makeatother

\usepackage[most]{tcolorbox}

\begin{document}
  \begin{tcolorbox}[left=22pt]
    The left skip was set to: \pgfkeysLastValueOf{/tcb/left}\par
    The right skip was set to: \pgfkeysLastValueOf{/tcb/right}\par
    The boxsep was set to: \pgfkeysLastValueOf{/tcb/boxsep}
  \end{tcolorbox}
\end{document}

请注意,为了使修补对加载时最初设置的键起作用tcolorbox,您必须在加载pgfkeys之前加载并修补它。tcolorbox

在此处输入图片描述

更新:改进版本,.last value引入新处理程序

\documentclass{article}
\usepackage{pgfkeys}
\usepackage{xpatch}

%% patch pgfkeys to store current value in subkey ".@last"
\makeatletter
% value passed to keys that execute commands
\xpatchcmd\pgfkeys@case@one
  {\pgfkeysgetvalue}
  {\pgfkeyslet{\pgfkeyscurrentkey/.@last}{\pgfkeyscurrentvalue}
   \pgfkeysgetvalue}
  {}{\fail}

% value passed to keys that store values
\xpatchcmd\pgfkeys@case@two@extern
  {\else}
  {\else
   \pgfkeyslet{\pgfkeyscurrentkey/.@last}{\pgfkeyscurrentvalue}}
  {}{\fail}

% value passed to keys that when key is defined by ".initial"
\pgfkeys{/handlers/.initial/.code=%
  % in handlered key eg "/tcb/xleft/.initial",
  %   \pgfkeyscurrentkey  == "/tcb/xleft/.initial"
  %   \pgfkeyscurrentpath == "/tcb/xleft"
  \pgfkeysifdefined{\pgfkeyscurrentpath/.@cmd}{}
    % only update ".@last" if this key is not previously defined by ".code"
    {\pgfkeyslet{\pgfkeyscurrentpath/.@last}{\pgfkeyscurrentvalue}}%
  \pgfkeyssetvalue{\pgfkeyscurrentpath}{#1}}
\makeatother


\usepackage[minted]{tcolorbox}


% user interface: fully expandable command
\def\pgfkeysLastValueOf#1{\csname pgfk@#1/.@last\endcsname}

% user interface: handler
\pgfkeys{/handlers/.last value/.code={%
  \pgfkeysLastValueOf{\pgfkeyscurrentpath}}}

\begin{document}
  \begin{tcblisting}{left=22pt}
    The left skip was set to: \pgfkeysLastValueOf{/tcb/left}\par
    The right skip was set to: \pgfkeysLastValueOf{/tcb/right}\par
    The boxsep was set to: \tcbset{boxsep/.last value}
  \end{tcblisting}
  
  \begin{tcblisting}{}
    \tcbset{xkey/.store in=\myxkey, xkey=xvalue}
    \tcbset{xkey/.initial=init value}
    
    \tcbset{xkey/.last value} % should be "xvalue", not "init value"
  \end{tcblisting}
\end{document}

在此处输入图片描述

答案2

tcolorboxv4.41 中,两个键/tcb/left/tcb/right被定义为样式键,因此它们的值仅传递给其他键,而不会存储(存储在与这两个键本身直接相关的任何内容中)。

\tcbset{
  left/.style={lefttitle=#1,leftupper=#1,leftlower=#1},
  right/.style={righttitle=#1,rightupper=#1,rightlower=#1},
}

钥匙/tcb/boxsep定义为

\tcbset{
  boxsep/.store in=\kvtcb@boxsep,
}

因此其值存储在 中\kvtcb@boxsep。完整密钥 do 的定义(或“值”)/tcb/boxsep/.@cmd包含\kvtcb@boxsep,尽管是以间接的方式:

\pgfkeysgetvalue{/tcb/boxsep/.@cmd}\temp \meaning\temp
% gives
% \long macro:#1\pgfeov ->\def \kvtcb@boxsep {#1}

一般而言,目前没有简单且一致的方法来检索最近传递给 定义的键的值pgfkeys。粗略地说,\pgfkeysvalueof只能从存储值的键中检索值,而不是宏,请参阅pgfmanualv3.1.6a,第 87.3.4 节。但软件包提供的大多数键都是处理键,请参阅同一手册中的第 87.3.5 节。

相关内容