\foreach の最後の要素の特別なケース

\foreach の最後の要素の特別なケース

カンマで区切られた入力リストから方程式参照のリストを生成する関数を作成したい。例:

\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

拡張可能な方法が求められていた場合、これは他のユーティリティを使用して実行できます。xintツール

xintforlast

\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}

関連情報