我想创建一个函数,从逗号分隔的输入列表中生成方程式引用列表。例如
\erefs{eq:first, eq:second, eq:third}
\erefs{eq:first}
得到以下结果:
(\ref{eq:first}), (\ref{eq:second}), and (\ref{eq:third})
(\ref{eq:first})
我可以正确处理 1 项列表,但在为最后一个元素生成“and”时遇到了麻烦。关于如何将“and”添加到此代码中,您有什么想法吗?
\documentclass{article}
\usepackage{tikz, amsmath, hyperref}
\def\erefs#1{%
\gdef\firstelement{1}
\foreach \e [count=\ni] in {#1}{%
\ifnum\firstelement=0 , \fi %
(\ref{\e})%
\gdef\firstelement{0}%
}
}
\begin{document}
\begin{equation}\label{eq:first}\end{equation}
\begin{equation}\label{eq:second}\end{equation}
\begin{equation}\label{eq:third}\end{equation}
\erefs{eq:first, eq:second, eq:third}
\erefs{eq:first}
\end{document}
我需要同时使用 hyperref 和 amsmath 包
添加:
我经常使用\package{xr}
和\externaldocument{...}
来引用技术附录,并喜欢将其放在TA.
这些参考资料前面。如果可以为前缀传递可选参数就好了
我想创建一个函数,从逗号分隔的输入列表中生成方程式引用列表。例如
\erefs{eq:first, eq:second, eq:third}
\erefs{eq:first}
\erefs[TA]{eq:first, eq:second, eq:third}
\erefs[TA]{eq:first}
得到以下结果:
(\ref{eq:first}), (\ref{eq:second}), and (\ref{eq:third})
(\ref{eq:first})
(TA.\ref{eq:first}), (TA.\ref{eq:second}), and (TA.\ref{eq:third})
(TA.\ref{eq:first})
答案1
这是一种无需 的方法cleveref
,但需要 的最新版本expl3
:
\documentclass{article}
\usepackage{xparse,amsmath}
\ExplSyntaxOn
\NewDocumentCommand{\erefs}{sm}
{
\IfBooleanTF{#1}
{ \jlperla_erefs:Nn \ref { #2 } }
{ \jlperla_erefs:Nn \eqref { #2 } }
}
\seq_new:N \l_jlperla_input_seq
\seq_new:N \l_jlperla_output_seq
\cs_new_protected:Npn \jlperla_erefs:Nn #1 #2
{
\seq_set_split:Nnn \l_jlperla_input_seq { , } { #2 }
\seq_clear:N \l_jlperla_output_seq
\seq_map_inline:Nn \l_jlperla_input_seq
{
\seq_put_right:Nn \l_jlperla_output_seq { #1 { ##1 } }
}
\seq_use:Nnnn \l_jlperla_output_seq
{ ~ and ~ } % between two
{ , ~ } % between more than two
{ , ~ and ~ } % between last two
}
\ExplSyntaxOff
\begin{document}
Some text before
\begin{align}
0+0&=0\label{eq:first}\\
0+1&=1\label{eq:second}\\
1+1&=2\label{eq:third}
\end{align}
One: \erefs{eq:first}
Two: \erefs{eq:second, eq:third}
Three: \erefs{eq:first,eq:second,eq:third}
One: \erefs*{eq:first}
Two: \erefs*{eq:second, eq:third}
Three: \erefs*{eq:first,eq:second,eq:third}
\end{document}
* 版本使用\ref
,而普通版本使用\eqref
(更适合方程式)。
“输入”序列设置为标签,循环添加\ref
或\eqref
围绕它们。然后\seq_use:Nnnn
在所有情况下都执行正确的操作。
这是允许使用前缀的修改。我将其挂钩到\eqref
,定义一个类似的命令以便应用特殊格式。
\documentclass{article}
\usepackage{xparse,amsmath}
\makeatletter
\newcommand{\peqref}[2]{\textup{\tagform@{#1\ref{#2}}}}
\newcommand{\pref}[2]{#1\ref{#2}}
\makeatother
\ExplSyntaxOn
\NewDocumentCommand{\erefs}{ s o m }
{
\IfBooleanTF{#1}
{
\IfNoValueTF{#2}
{ \jlperla_erefs:Nnn \pref { } { #3 } }
{ \jlperla_erefs:Nnn \pref { #2. } { #3 } }
}
{
\IfNoValueTF{#2}
{ \jlperla_erefs:Nnn \peqref { } { #3 } }
{ \jlperla_erefs:Nnn \peqref { #2. } { #3 } }
}
}
\seq_new:N \l_jlperla_input_seq
\seq_new:N \l_jlperla_output_seq
\cs_new_protected:Npn \jlperla_erefs:Nnn #1 #2 #3
{
\seq_set_split:Nnn \l_jlperla_input_seq { , } { #3 }
\seq_clear:N \l_jlperla_output_seq
\seq_map_inline:Nn \l_jlperla_input_seq
{
\seq_put_right:Nn \l_jlperla_output_seq { #1 { #2 } { ##1 } }
}
\seq_use:Nnnn \l_jlperla_output_seq
{ ~ and ~ } % between two
{ , ~ } % between more than two
{ , ~ and ~ } % between last two
}
\ExplSyntaxOff
\begin{document}
Some text before
\begin{align}
0+0&=0\label{eq:first}\\
0+1&=1\label{eq:second}\\
1+1&=2\label{eq:third}
\end{align}
No prefix
One: \erefs{eq:first}
Two: \erefs{eq:second, eq:third}
Three: \erefs{eq:first,eq:second,eq:third}
One: \erefs*{eq:first}
Two: \erefs*{eq:second, eq:third}
Three: \erefs*{eq:first,eq:second,eq:third}
\bigskip
With prefix
One: \erefs[TA]{eq:first}
Two: \erefs[TA]{eq:second, eq:third}
Three: \erefs[TA]{eq:first,eq:second,eq:third}
One: \erefs*[TA]{eq:first}
Two: \erefs*[TA]{eq:second, eq:third}
Three: \erefs*[TA]{eq:first,eq:second,eq:third}
\end{document}
答案2
您实际上不需要任何额外的包,您只需使用 LaTeX 中已有的循环即可:
\documentclass{article}
\makeatletter
\def\erefs#1{%
\count@\z@
\@for\tmp:=#1\do{\advance\count@\@ne}%
\edef\xtmp{\ifcase\count@\or\or\ and\ \else, and\ \fi}%
\@for\tmp:=#1\do{%
\advance\count@\m@ne
\edef\tmp{%
\noexpand\ref{\expandafter\zap@space\tmp\@gobble{} \@empty}}%
\tmp
\ifnum\count@>\@ne, %
\else
\ifnum\count@=\@ne\xtmp
\fi\fi}}
\makeatother
\begin{document}
\begin{equation}\label{eq:first}\end{equation}
\begin{equation}\label{eq:second}\end{equation}
\begin{equation}\label{eq:third}\end{equation}
A \erefs{eq:first, eq:second, eq:third}
B \erefs{eq:first, eq:second}
C \erefs{eq:first}
\end{document}
答案3
这是一个通用的使用方法,使用etoolbox
:
\documentclass{article}
\usepackage{etoolbox}
% set up defaults so we don't get an error
% when we try to redefine these commands
\newcommand*{\elementsep}{}%
\newcommand*{\lastelement}{}%
\newcommand*{\prelastelement}{}%
% define the handler macro:
\newcommand*{\dodisplayelement}[1]{%
\elementsep
\lastelement
\renewcommand{\lastelement}{%
\renewcommand{\elementsep}{, }%
\renewcommand{\prelastelement}{ and }%
#1%
}}%
% define the new command to process a list of elements:
\newcommand*{\displayelements}[1]{%
% initialise:
\renewcommand*{\elementsep}{}%
\renewcommand*{\lastelement}{}%
\renewcommand*{\prelastelement}{}%
% Iterate through list
\forcsvlist{\dodisplayelement}{#1}%
% Finish off:
\prelastelement \lastelement
}
\begin{document}
\displayelements{first,second,third,fourth,fifth}
\end{document}
每个项目都显示为\lastelement
,如果该项目代表一个标签,则可以使用:
% define the handler macro:
\newcommand*{\dodisplayelement}[1]{%
\elementsep
\lastelement
\renewcommand{\lastelement}{%
\renewcommand{\elementsep}{, }%
\renewcommand{\prelastelement}{ and }%
\ref{#1}%
}}%
答案4
和\xintFor
。可选参数可以是任意值,也可以不是TA
。
如果要求采用可扩展的方式,则可以使用来自新工具。
\documentclass{article}
\usepackage{amsmath, hyperref}
\usepackage{xinttools}
\makeatletter
% with optional parameter.
\def\erefs {\@ifnextchar[\@erefsTA\@erefs }
\def\@erefs #1{%
\xintFor ##1 in {#1}\do
{\xintifForFirst{}{, \xintifForLast{and }{}}(\ref{##1})}%
}
\def\@erefsTA [#1]#2{%
\xintFor ##1 in {#2}\do
{\xintifForFirst{}{, \xintifForLast{and }{}}(#1, \ref{##1})}%
}
\makeatother
\begin{document}\thispagestyle{empty}
\begin{equation}E=mc^2\label{eq:first}\end{equation}
\begin{equation}E=h\nu\label{eq:second}\end{equation}
\begin{equation}\zeta(s)=0\label{eq:third}\end{equation}
\erefs{eq:first, eq:second, eq:third}
\erefs{eq:first}
\erefs[TA]{eq:first, eq:second, eq:third}
\erefs[TA]{eq:first}
\end{document}