![是否有任何命令允許存取另一個命令的三個參數?](https://rvso.com/image/405357/%E6%98%AF%E5%90%A6%E6%9C%89%E4%BB%BB%E4%BD%95%E5%91%BD%E4%BB%A4%E5%85%81%E8%A8%B1%E5%AD%98%E5%8F%96%E5%8F%A6%E4%B8%80%E5%80%8B%E5%91%BD%E4%BB%A4%E7%9A%84%E4%B8%89%E5%80%8B%E5%8F%83%E6%95%B8%EF%BC%9F.png)
假設我有這樣的命令:\command{<command>}{<content>}{<value>}
。有沒有辦法透過其中一個參數來存取其他參數?例如這樣的東西:
\getcontent{<value> or <command>}%----> output: <content>
\getcommand{<content> or <value>}%----> output: <command>
\getvalue{<content> or <command>}%----> output: <value>
我也不知道如何定義\command{<command>}{<content>}{<value>}
?
答案1
也許以下是一個選擇:
\documentclass{article}
% \command{<command>}{<content>}{<value>}
\newcommand{\command}[3]{%
\expandafter\def\csname #2@cmd\endcsname{#1}%
\expandafter\def\csname #3@cmd\endcsname{#1}%
\expandafter\def\csname #1@cnt\endcsname{#2}%
\expandafter\def\csname #3@cnt\endcsname{#2}%
\expandafter\def\csname #1@val\endcsname{#3}%
\expandafter\def\csname #2@val\endcsname{#3}%
}
\newcommand{\getcontent}[1]{%
\ifcsname #1@cnt\endcsname
\csname #1@cnt\endcsname
\else
No command/value associated with #1.
\fi
}
\newcommand{\getcommand}[1]{%
\ifcsname #1@cmd\endcsname
\csname #1@cmd\endcsname
\else
No content/value associated with #1.
\fi
}
\newcommand{\getvalue}[1]{%
\ifcsname #1@val\endcsname
\csname #1@val\endcsname
\else
No command/content associated with #1.
\fi
}
\begin{document}
\command{abc}{def}{ghi}
\getcommand{def} % abc
\getcommand{ghi} % abc
\getcontent{abc} % def
\getcontent{ghi} % def
\getvalue{abc} % ghi
\getvalue{def} % ghi
\getcommand{jkl}% No jkl found
\getcontent{jkl}% No jkl found
\getvalue{jkl}% No jkl found
\end{document}
您會注意到,為了方便起見,我將其保留<command>
為字串,而不是控制序列。由於沒有使用 的上下文<command>
,因此這可能不是問題。