假设我们正在使用一些名为“个人信息“,这需要两个米-andatory 和一个o-可选参数:
\DeclareDocumentCommand \PersonInformation { m m o } {%
...
}
太棒了。正如你可能猜到的,第一个米该命令的 -andatory 参数将包含此人的名字。
现在,我们假设我们将使用这个名为“个人信息“在我们的文件中很常见......也就是说......它将连续发给多人。
每次命令关闭后,可能会出现更多关于该人的段落,并且在每个人的信息的最后,都会有一个指向某个数据库中该人的个人资料的超链接。
\href{http://a.beautiful.url/?searchid=...}
例如,假设某人的名字是NameMeAName
。我们的“个人信息“将如下所示:
\PersonInformation{
NameMeAName
}{
%
}[
%
]
然后,会有一些描述的段落NameMeAName
,之后我们希望有一个如下的 URL:
\href{http://a.beautiful.url/?searchid=NameMeAName}
当然,在 person 之后NameMeAName
,可能会有该命令的新用法,例如 for person NameMeBName
... 它将遵循类似的结构。
现在,我的问题是:
如何不总是手动填写人的姓名\href{http://a.beautiful.url/?searchid=
... }
,而是如何自动让URL出现,这将以某种方式需要“重用另一个命令最后一次出现的第 n 个参数?“(这是这篇原帖的标题)。
在此示例中
- 第 n 个参数 = 第一个且强制的参数
- 另一个命令 = “个人信息“ 命令
谢谢。
平均能量损失
\documentclass[a4paper]{article}
\usepackage{xparse, hyperref}
\DeclareDocumentCommand \PersonInformation { m } {%
\Large {\bfseries#1} \endgraf The person who didn't know why they were named ``#1''. \normalsize
}
\begin{document}
\PersonInformation{
NameMeAName
}
This person born and died in the same place, same year. \endgraf
On-line biography: \href{http://a.beautiful.url/?searchid=NameMeAName}{Click on me}
\PersonInformation{
NameMeBName
}
This person was born and did not die yet. \endgraf
On-line biography \href{http://a.beautiful.url/?searchid=NameMeBName}{Click on me}
\PersonInformation{
AndSoON
}
This person is not born yet. \endgraf
On-line biography: \href{http://a.beautiful.url/?searchid=AndSoON}{Click on me}
\end{document}
答案1
为什么不使用\NewDocumentEnvironment
而不是\NewDocumentCommand
?您还可以从代码中访问参数结束环境。下面是我所指的一个例子:
% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.
\usepackage[T1]{fontenc} % Not always necessary, but recommended.
% End of standard header. What follows pertains to the problem at hand.
\usepackage{xparse}
\usepackage{hyperref}
\NewDocumentEnvironment {PersonInformation} { m } {%
\begingroup
\Large \textbf{#1}\par
The person who didn't know why they were named ``#1".\par
\endgroup
}{%
\par
\noindent % say
On-line biography:
\href{http://a.beautiful.url/?searchid=#1}{Click on me}\par
}
\begin{document}
Outer text.
\begin{PersonInformation}{PoorBaby}
This person was born and died in the same place, on the same year.
(The same year? Oh, no!)
\end{PersonInformation}
Outer text.
\begin{PersonInformation}{NameMeAName}
This person was born and did not die yet.
\end{PersonInformation}
Outer text.
\end{document}