简体   繁体   English

如何在乳胶上制作字典

[英]How to make a dictionary on the Latex

I want to have latex commands of this kind: 我想要这种乳胶命令:

\\setdictionary{dictionary_name}{key}{value} \\ setdictionary {dictionary_name} {key} {value}

\\getdictionary{dictionary_name}{key} \\ getdictionary {dictionary_name} {key}

These commands should do the same as map<string, string> in C++. 这些命令应与C ++中的map <string,string>相同。

To define an associative container, there are several tools. 要定义一个关联容器,有几种工具。 The oldest is the keyval package, that is somehow superseded by more recent packages as pgfkeys and l3keys from LaTeX3. 最老的是keyval软件包,它以某种方式被LaTeX3的pgfkeys和l3keys等更新的软件包所取代。

l3keys and pgfkeys are initially developed as a mean to pass parameters to a macro in the form key=value . l3keys和pgfkeys最初是作为将参数以key=value形式传递给宏的方法而开发的。 l3keys is the user interface and should be used with l3prop that implements property lists that can be associated to an object and retrieved. l3keys是用户界面,应与l3prop一起使用,该l3prop实现可与对象关联并检索的属性列表。 It is also possible to use directly l3prop, if one does not need the key=value syntax. 如果不需要key=value语法,也可以直接使用l3prop。

pgfkeys is probably simpler to use. pgfkeys可能更易于使用。 You can define keys in a hierarchical way, set values of theses keys and retrieve it. 您可以按分层方式定义键,设置这些键的值并进行检索。

Basic use with pgfkeys and l3 is pgfkeys和l3的基本用法是

\documentclass{article}
\usepackage{pgfkeys}
\usepackage{expl3,xparse}
\begin{document}
% pgfkeys version
\pgfkeyssetvalue{/my dictionary/my entry1}{Hello world!}
\pgfkeysvalueof{/my dictionary/my entry1}

% with latex3 prop
\ExplSyntaxOn
\prop_new:N \mydict    % define a container (property list)
\prop_new:N \myotherdict % and another one
\NewDocumentCommand \AddToDict { O{\mydict} m m } % add a key to a dictionary default to \mydict
{
  \prop_put:Nnn #1 {#2}{#3}
}
\NewDocumentCommand \GetFromDict { O{\mydict} m } % get a key from a dictionary default \mydict
{
  \prop_item:Nn #1 {#2}
}
\ExplSyntaxOff

\AddToDict{my entry1}{Hello again world!}
\GetFromDict{my entry1}

\AddToDict[\myotherdict]{my entry1}{Hello again again world!}
\GetFromDict[\myotherdict]{my entry1}
\end{document}

The LaTeX3 way is a bit more complex as the bundle only contains low level library routines. LaTeX3的方式有点复杂,因为捆绑包仅包含低级库例程。 But it may be more flexible. 但这可能更灵活。 Look at l3prop entry in the interface3 manual. 查看interface3手册中的l3prop条目。

With pgfkeys, there many ways to enter values, retrieve them, set default values, associate code to a key, etc. Look at 'key management' in the tikz manual. 使用pgfkeys,有很多方法可以输入值,检索值,设置默认值,将代码与键关联等。请参阅tikz手册中的“键管理”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM