是否有任何命令允許存取另一個命令的三個參數?

是否有任何命令允許存取另一個命令的三個參數?

假設我有這樣的命令:\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>,因此這可能不是問題。

相關內容