2

Context

This question is an extension of tcolorbox: advance / complex template with arbitrary number of arguments. The goal of that question was to take verbose and repetitive code used to make the following figure into a function for multiple reuse.

enter image description here

@Andrew produced a remarkable answer which answered the question (for convenience copy-pasted below). However it is missing one capability that I would greatly like to learn how to implement. In the figure above, when an argument is being spoken of, it is highlighted with elliptic tcolorbox. @Andrew's answer made the elliptic highlighting for the original appearance of the arguments, but does not highlight them elsewhere. Thus it was discussed to add some type of flourish to arguments so they can be recognized and colored appropriately e.g. an_argument or borrowing from html's tag style: <>arg<>.

Goal

The goal is to modify @Andrew's answer (excerpt and link to original provided below), to incorporate the kind of argument highlighting seen in the figure. Highlighted arguments should maintain the same color as when first introduced.

Note

One obvious solution to this problem is simply to not be so lazy and call the following whenever needed:

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

However this question is about how to solve this problem via a flourish or tag that automatically colors the arguments according to the function @Andrew provided.

@Andrew's answer

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.