语境

语境

语境

这个问题是tcolorbox: advance / complex template with arbitrary number of arguments。该问题的目标是将用于制作下图的冗长而重复的代码转换为可多次重复使用的函数。

在此处输入图片描述

@安德鲁给出了一个出色的答案,回答了这个问题(为方便起见,复制粘贴在下面)。但是,它缺少一项功能,我非常想学习如何实现它。在上图中,当谈论一个论点时,它会用椭圆突出显示tcolorbox。@Andrew 的答案对论点的原始外观进行了椭圆突出显示,但没有在其他地方突出显示它们。因此,有人讨论了在论点中添加某种类型的修饰,以便可以识别它们并适当着色,例如一个论点或者借用html的标签样式:<>arg<>。

目标

目标是修改@Andrew的答案(摘录和原文链接如下),以纳入图中所示的论点突出显示类型。突出显示的论点应保持相同的颜色与首次推出时一样。

笔记

解决此问题的一个明显方法就是不要太懒,并在需要时调用以下命令:

\newtcbox{\arg}[1][red]{on line, arc=7pt,colback=#1!10!white,colframe=#1!50!black, before upper={\rule[-3pt]{0pt}{10pt}},boxrule=1pt, boxsep=0pt,left=6pt,right=6pt,top=2pt,bottom=2pt}

\arg[arg-color]{my-arg}

然而,这个问题是关于如何通过根据@Andrew 提供的功能自动为参数着色的华丽或标签来解决这个问题。

@Andrew 的回答

\documentclass{article}
\usepackage{xparse}
\usepackage{framed}
\usepackage{tcolorbox}

\definecolor{eastern-blue}{cmyk}{0.80, 0.13, 0.14, 0.04, 1.00}
\definecolor{orient}{cmyk}{0.85, 0.44, 0.17, 0.14, 1.00}
\definecolor{dove-gray}{cmyk}{0.47, 0.39, 0.35, 0.01, 1.00}

\tcbset{
colback=dove-gray!10!,
coltitle=white,
coltext=black,
colframe=eastern-blue,
fonttitle=\bfseries\large,
titlerule=3mm,
boxrule=1mm,
subtitle style={
    colback=orient,
    fonttitle=\bfseries,
    titlerule=.5mm,
    boxrule=.5mm
    }
}
\pgfkeys{/function/.is family, /function,
  function/.initial=unknown function,
  class/.initial=unknown class,
  description/.initial=no description,
  returns/.initial=None,
  % the remaining code deals with arguments
    arguments/.initial=0, % number of arguments
    % any unknowns are assumed to be arguments with their descriptions
    argument line/.initial={},
    argument description/.initial={},
    .unknown/.code = {
      \pgfmathsetmacro\args{int(\pgfkeysvalueof{/function/arguments}+1)}
      \pgfkeyssetvalue{\pgfkeyscurrentpath/arguments}{\args}
      \xdef\argcol{\pgfkeysvalueof{/function/argument color \args}}
      \xdef\argument{\pgfkeyscurrentname}
      \ifnum\args>1\pgfkeys{/function/argument line/.append code={,\space}}\fi
      \pgfkeys{/function/argument line/.append code/.expanded={\noexpand\xmybox[\argcol]{\argument}}}
      \pgfkeys{/function/argument description/.append code/.expanded={\noexpand\item[\argument]#1}}
    },
    % list of colours used for arguments
    argument color 1/.initial=eastern-blue,
    argument color 2/.initial=blue,
    argument color 3/.initial=orient,
    argument color 4/.initial=dove-gray,
}

\newtcbox{\xmybox}[1][red]{
  on line, arc=7pt,colback=#1!10!white,colframe=#1!50!black,
  before upper={\rule[-3pt]{0pt}{10pt}},boxrule=1pt,
  boxsep=0pt,left=6pt,right=6pt,top=2pt,bottom=2pt
}
\let\anonymous\relax

\DeclareDocumentCommand\SetArguments{ > {\SplitArgument{1}{ = }} m}{\setarguments#1}
\newcommand\setarguments[2]{\def\arg{#1}\def\argdesc{#2}}
\NewDocumentCommand\Function{ m o}{%
  \bgroup% do everything inside a group so that we do not need to reset function values
    \pgfkeys{/function, function=#1}% set function name
    \IfNoValueF{#2}{\pgfkeys{/function, #2}}% set other parameters
    \addcontentsline{toc}{section}{% add toc entry
       \pgfkeysvalueof{/function/class}.\pgfkeysvalueof{/function/function}}
    \begin{tcolorbox}[title={\anonymous\pgfkeys{/function/class}.\pgfkeys{/function/function}}]
       \pgfkeys{/function/description} % add description
       \edef\args{\pgfkeysvalueof{/function/arguments}}
       \ifnum\args>0% add argument block if the function has arguments
         \tcbsubtitle{Arguments}        
         $\Bigl\langle$\pgfkeys{/function/argument line}$\Bigr\rangle$
         \begin{description}\pgfkeys{/function/argument description}\end{description}
       \fi
       \tcbsubtitle{Returns}
       {\anonymous \pgfkeys{/function/function}} returns %
             $\langle$\,\pgfkeys{/function/returns}\,$\rangle$.
    \end{tcolorbox}
  \egroup% close group
}

\begin{document}

  \tableofcontents

  \Function{sort\_by}[%
     class=OrderedNestedDictionary,
     description=Reorders the calling {\anonymous OrderedNestedDictionary} based on a subkey
                 found in each element of the calling {\anonymous OrderedNestedDictionary},
     subkey=a strings representing a subkey found in the values of the callings
             {\anonymous OrderedNestedDictionary} associated keys,
     reverse=a boolean of whether or not to reverse the resulting sorted collection,
  ]

\end{document}

相关内容