多部分变量定义

多部分变量定义

是否可以定义一个多部分变量,然后检查其定义并在以后使用它的多个部分?我的意思是:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{etoolbox}

\begin{document}

    %Define macro
    \def\myMacro{

        %Check for multipart variable definition
        \ifdef{\VariableOne}{
            ##1

            ##2

            ##3 
        }{}

        #1
    }

    %Define multipart variable
    \def\VariableOne#1#2#3{
        {1}
        {2}
        {3}
    }
    \myMacro{4}
\end{document}

我知道这个任务可以通过这种方式完成:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{etoolbox}


\begin{document}
    \def\myMacro#1{
        \ifdef{\VariableOne}{   
            \foreach \x/\y/\z in \VariableOne{
                \x

                \y

                \z  
            }
        }{}

        #1
    }

    \def\VariableOne{1/2/3}

    \myMacro{4}
\end{document}

我也知道您可以将所有变量传递给\myMacro,但我很好奇使用第一个代码块中的方法是否可行。

答案1

也许这段代码可以实现你想要的功能:

\documentclass{article}
%\usepackage{pgfplots}
\usepackage{etoolbox}

% \myMacro{<Tokens to spit out in case macro \VariableOne is undefined>}%
%         {<Tokens forming whatsoever "value">}%
%
% In case the macro \VariableOne is undefined, 
% <Tokens to spit out in case macro \VariableOne is undefined>
% trailed by <Tokens forming whatsoever "value">
% will be "spit out".
%
% Otherwise the expansion of \VariableOne which delivers 
% \VariableOne's three components will be delivered to the
% macro \ProcessVariableOnesThreeComponentsAndWhatsoeverValue
% which in turn will process these three components and the
% <Tokens forming whatsoever "value">.
\newcommand\myMacro[2]{%
  % Check whether macro holding vaule(s) of "multi-component variable"
  % is defined:
  \ifdef{\VariableOne}%
        {\expandafter\ProcessVariableOnesThreeComponentsAndWhatsoeverValue\VariableOne{#2}}%
        {#1#2}%
}%

\newcommand\ProcessVariableOnesThreeComponentsAndWhatsoeverValue[4]{%

  This is \texttt{\string\VariableOne}'s first component: 
  \texttt{#1}

  This is \texttt{\string\VariableOne}'s second component:
  \texttt{#2}

  This is \texttt{\string\VariableOne}'s third component:
  \texttt{#3}

  This is whatsoever value:
  \texttt{#4}

}%


\begin{document}

% To make \VariableOne undefined, \let it equal to an undefined token.
% \let\VariableOne=\UndFInEdVeRYBiZarReConTRoLWORd
At this stage, the "multi-component variable" is undefined:

\myMacro{The macro \texttt{\string\VariableOne} is undefined!}{Whatsoever Value}%


\hrulefill


% Now define/initialize "multi-component variable":
\newcommand\VariableOne{%
  {Component 1}%
  {Component 2}%
  {Component 3}%
}%

At this stage, the "multi-component variable" is defined:

\myMacro{The macro \texttt{\string\VariableOne} is undefined!}{Whatsoever Value}%

\end{document}

在此处输入图片描述

答案2

具有灵活的语法,允许以不同的方式显示值。在可选参数中,\usevariable您可以使用#1来表示当前值;默认值为#1\par

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\setvariable}{mm}
 {% #1 = variable name
  % #2 = comma separated list of values
  \seq_clear_new:c { l_kevin_multivar_#1_seq }
  \seq_set_from_clist:cn { l_kevin_multivar_#1_seq } { #2 }
 }

\NewDocumentCommand{\usevariable}{+omm}
 {% #1 = optional way to use the values (+ means that \par is allowed)
  % #2 = variable to use
  % #3 = tokens to be displayed after the values
  \seq_if_exist:cTF { l_kevin_multivar_#2_seq }
   {
    \group_begin:
    \IfValueT { #1 }
     {
      \cs_set:Nn \__kevin_multivar_use:n { #1 }
     }
    \seq_map_function:cN { l_kevin_multivar_#2_seq } \__kevin_multivar_use:n
    \group_end:
   }
   { !!~Variable~`#2'~not~defined~yet~!! }
   #3
 }

\cs_new:Nn \__kevin_multivar_use:n { #1 \par } % default action

\ExplSyntaxOff

\begin{document}

\usevariable{VariableOne}{Something}

\setvariable{VariableOne}{A,B,C}

\usevariable{VariableOne}{Something}

\usevariable[#1--]{VariableOne}{Something}

\end{document}

在此处输入图片描述

相关内容