语境
我正在为自己开发的软件包编写文档(不是乳胶)。当然,必须定义一组函数、它们的参数和相关的返回值,有时还要定义示例等。
为此,我以tcolorbox
一种从美学上来说不是最差的方式来编写这些信息。(见下图)。
目标
我的问题大致如下:给定下面的代码,我怎样才能将其变成一个函数,以便可以像这样调用它
\function-blur{sort\_by}[
class = OrderedNestedDictionary,
description = Reorders....,
argument-list = [subkey, reverse]
argument-description = [subkey = text...., reverse = text...]
returns = returns None.
]
例如:
参数列表根据预定义的顺序自动为参数涂上颜色(例如东方蓝色、蓝色、洋红色、鸽灰色……)
如果在参数描述或返回中引用了参数,则会适当地突出显示(像在主参数列表中一样圈出)。
奖金
回答这个子问题可获得加分。
- 如何使
tcbox
内容在目录中显示为一个部分。
更新
根据 Andrew 的要求,我将包括一个可自行编译的 MWE。因此,我将删除单独的定义。
\documentclass[]{article}
\usepackage[utf8]{inputenc}
\usepackage{MySpecialFonts} %This is where Anonymous comes from
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% GRAPHICS AND COLORS %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{graphicx}
\usepackage{xcolor}
\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}
%% Default tcbox behavior
\usepackage{framed}
\usepackage{tcolorbox}
\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
}
}
\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}
\usepackage{tikz}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% MATH PACKAGES %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{amssymb}
\usepackage{amsmath} %braces
\usepackage{mathtools} %brackets
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% My-Class %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%
%% my\_function %%
%% %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{my\_function}
\begin{tcolorbox}[title={\anonymous MyClass.my\_function}]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% DESCRIPTION %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
This is a description of what this functions does when called by an instance of {\anonymous MyClass} based on some of the arguments.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% ARGUMENTS %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tcbsubtitle{Arguments}
$\langle$\xmybox[eastern-blue]{arg1}, \xmybox[blue]{arg2}, \xmybox[magenta]{arg3}$\rangle$
\begin{description}
\item[arg1] is a string that does something
\item[arg2] is an iterator that is used in conjuction with \xmybox[eastern-blue]{arg1}
\item[arg3] is an optional parameter that may act on \xmybox[eastern-blue]{arg1}
\end{description}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% RETURNS %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tcbsubtitle{Returns}
{\anonymous my\_function} returns a thing which is determined by \xmybox[eastern-blue]{arg1} and \xmybox[blue]{arg2}. If \xmybox[magenta]{arg3} is given with a value $X$ then $Y$ occurs to \xmybox[eastern-blue]{arg1}.
\end{tcolorbox}
\end{document}
定义
字体
\newfontfamily\anonymous{AnonymousPro}[
Path = ./Fonts/Anonymous/,
Extension = .ttf,
UprightFont = *-Regular,
BoldFont = *-Bold,
ItalicFont = *-Italic,
BoldItalicFont = *-BoldItalic
]
答案1
要将各种代码块参数传递给宏,最简单的方法是使用众多键值系统之一。正如您使用的那样彩色盒子我们可以使用\tcbset
,但这只是调用\pgfkeys
,所以我用了那个。
我创建了一个新的/function
pgfkeys 系列。该系列接受名称function
、函数class
名称、值的键,其余键被假定为函数的参数,值是它们的描述。要指定示例函数,请使用:description
return
\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,
]
宏的工作方式\Function
是先设置键。当它遇到未知键时,它被假定为函数的参数。处理程序.unknown/.code
被调用,它(递归地)定义键argument line
并argument description
列出参数及其描述。在这两个键中,参数按它们提供给宏的顺序出现\Function
。完成后,宏将像在 OP 中一样构造您的 tcolorbox,只是它使用存储在 pgfkeys 中的数据。作为奖励,宏\Function
还会将一个部分条目添加到目录中。
完整代码如下:
\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}
输出如下:
请注意,我没有使用您喜欢的匿名字体,因为我没有这个(而是我设置了\let\anonymous\relax
)。
最后,原贴作者还说“如果在参数描述或返回中引用了参数,则会适当地突出显示(像在主参数列表中一样圈出)”,但我不清楚这是什么意思,因为没有给出例子。如果这个问题还没有解决,我建议再问一个问题:)