pgfkeys :按调用顺序打印值

pgfkeys :按调用顺序打印值

我想创建一个环境,按照使用 pgfkeys 调用的顺序打印参数。

\documentclass{article}
\usepackage{pgfkeys}

\pgfkeys{
/info/.is family, /info,
default/.style = {
    email=\empty,
    phone=\empty,
    linkedin=\empty,
    address=\empty
},
email/.estore in = \email,
phone/.estore in = \phone,
linkedin/.estore in = \linkedin,
address/.estore in = \address
}
\newcommand{\separator}{\hspace{0.3cm}\textbullet{}\hspace{0.3cm}}
\newcounter{paramcounter}
\newenvironment{info}[1][]{%
\pgfkeys{/info, default, #1}%
\setcounter{paramcounter}{0}
\ifx\email\empty\else \stepcounter{paramcounter} \noindent Email: \email\fi%
\ifx\phone\empty\else\ifnum\value{paramcounter}>0\separator\fi \stepcounter{paramcounter} Phone: \phone\fi%
\ifx\linkedin\empty\else\ifnum\value{paramcounter}>0\separator\fi \stepcounter{paramcounter} LinkedIn: \linkedin\fi%
\ifx\address\empty\else\ifnum\value{paramcounter}>0\separator\fi \stepcounter{paramcounter} Address: \address\fi%
}

\begin{document}

\info[linkedin=john, phone=0630801266, address=Rabat, email= [email protected]]\\
\end{document}

在上面的例子中,参数总是按以下顺序打印:

电子邮件:[电子邮件保护]• 电话:0630801266 • LinkedIn:john • 地址:拉巴特

我想要做的是,如果我以这种方式调用环境:

\info[linkedin=john, phone=0630801266, address=Rabat, email= [email protected]]

它应该按照我调用的顺序打印这些值:

LinkedIn: john • 电话: 0630801266 • 地址: 拉巴特 • 电子邮件:[电子邮件保护]

答案1

键值结构的理念是键的顺序无关紧要,因此我认为这在 中很难实现pgfkeys。但是,您可以自己编写一个简单的键值解析器,使用listofitems包保留顺序,该包提供了一个宏\foreachitem来迭代列表。

梅威瑟:

\documentclass{article}
\newcommand{\separator}{\hspace{0.3cm}\textbullet{}\hspace{0.3cm}}
\usepackage{listofitems}
\usepackage{ifthen}
\usepackage[left=1in,right=1in]{geometry}
\newcounter{paramcounter}
\newcommand{\info}[1]{%
\def\args{#1}%
\setsepchar{,}%
\readlist*\argslist\args%
\setsepchar{=}%
\setcounter{paramcounter}{0}%
\foreachitem\currentarg\in\argslist{%
\readlist*\argparts\currentarg%
\ifnum\value{paramcounter}>0\separator\fi%
\ifthenelse{\equal{\argparts[1]}{linkedin}}{LinkedIn}{}%
\ifthenelse{\equal{\argparts[1]}{phone}}{Phone}{}%
\ifthenelse{\equal{\argparts[1]}{address}}{Address}{}%
\ifthenelse{\equal{\argparts[1]}{email}}{E-mail}{}%
: \argparts[2]%
\stepcounter{paramcounter}%
}}
\begin{document}
\info{linkedin=john, phone=0630801266, address=Rabat, email= [email protected]}

\info{address=Main Street, phone=1234567, [email protected], linkedin=mary}
\end{document}

结果:

在此处输入图片描述

答案2

如果您只想按照指定的顺序打印信息,那么您可以让 PGFKeys 来做到这一点。

在这里,我使用\begingroup … \endgroup将更改保存到utils/separator本地,我们也可以使用类似于密钥的东西default

我添加了该hyperref包来展示使用其宏的参数创建自定义链接是多么容易\href。(不过,请随意调整颜色设置。)

代码

\documentclass{article}
\usepackage{pgfkeys}
\usepackage[colorlinks]{hyperref}
\pgfkeys{
  /info/.is family, /info,
  utils/create info/.style 2 args={#1/.code={\pgfkeysalso{utils/separator}#2}},
  utils/separator/.style={utils/separator/.code=\hspace{0.3cm}\textbullet\hspace{0.3cm}},
  utils/create info=   {email}{Email:    \href{mailto:#1}{#1}},
  utils/create info=   {phone}{Phone:    \href{tel:#1}{#1}},
  utils/create info={linkedin}{LinkedIn: \href{https://linkedin.com/in/#1}{#1}},
  utils/create info= {address}{Address:  #1}
}
\newcommand*\info[1]{\par\noindent\begingroup\pgfkeys{/info,#1}\endgroup\par}%
\begin{document}
\info{linkedin = john, phone = 0630801266, address = Rabat, email = [email protected]}
\info{address = Home, email = [email protected], phone = 0123456789, linkedin = pete}
\end{document}

输出

在此处输入图片描述

相关内容