TransWikia.com

tcolorbox: How to read out values like left, right,

TeX - LaTeX Asked on November 24, 2020

How can I read out values like left, right,..., boxsep?
I tried pgfkeysvalueof{/tcb/left}. What do I have to do?

enter image description here

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

2 Answers

Here a new command pgfkeysLastValueOf{<full key>} is provided. You can use pgfkeysLastValueOf{/tcb/left} to retrieve the current (or, most recently passed) value of /tcb/left.

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

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

defpgfkeysLastValueOf#1{csname pgfk@#1/.@lastendcsname}
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}

Note that to make the patching work for keys initially set when loading tcolorbox, you have to load pgfkeys and patch it before tcolorbox is loaded.

enter image description here

Update: a refined version, with new handler .last value introduced

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

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

% value passed to keys that store values
xpatchcmdpgfkeys@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
defpgfkeysLastValueOf#1{csname pgfk@#1/.@lastendcsname}

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

enter image description here

Correct answer by muzimuzhi Z on November 24, 2020

In tcolorbox v4.41, both keys /tcb/left and /tcb/right are defined as a style key, hence their values are just passed to other keys, and never stored (in anything directly related to those two keys themselves).

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

Key /tcb/boxsep is defined by

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

hence its value is stored in kvtcb@boxsep. The definition (or "value") of full key /tcb/boxsep/.@cmd do contains kvtcb@boxsep, though in an indirect way:

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

In general, currently there is no easy and consistent way to retrieve the value most recently passed to a key defined by pgfkeys. Roughly speaking, pgfkeysvalueof can only retrieve values, rather than macros, from keys that store values, see pgfmanual v3.1.6a, sec. 87.3.4. But most keys provided by packages are handled keys, see sec. 87.3.5 from the same manual.

Answered by muzimuzhi Z on November 24, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP