创建查找表

创建查找表

我在创建查找表时遇到问题。我有两个表,第一个表的输出是下一个表的输入,这就是问题所在。有没有人能帮助我。

\documentclass{article} 
\usepackage{xstring}
\newcommand{\CableType}[1]{\renewcommand{\CableType}{#1}}
\newcommand{\csa}[1]{\renewcommand{\csa}{#1}}
\begin{document}
\CableType{N2XSY}
\csa{240}
\newcommand{\Conductor}{
                        \IfEqCase{\CableType}{
                        {N2XSY}{stranded copper conductors}
                        {N2XSYRY}{stranded copper conductors}
                        {N2XSYBY}{stranded copper conductors}
                        {NA2XSY}{stranded aluminium conductors}
                        {NA2XSYRY}{stranded aluminium conductors}
                        {NA2XSYBY}{stranded aluminium conductors}
                                              }
                        }

\newcommand{\RL}[1]{
                        \IfEqCase{\Conductor}{
                        {stranded copper conductors}{
                                        \IfEqCase{#1} {
                                        {150}{34.5}
                                        {185}{36.5}
                                        {240}{39}
                                        {300}{41.5}
                                                      }
                                                     }
                                              }
                        \IfEqCase{\Conductor}{
                        {stranded aluminium conductors}{
                                        \IfEqCase{#1}{
                                        {150}{39}
                                        {185}{39.5}
                                        {240}{41}
                                        {300}{41.5}
                                                      }
                                                       }

                                              }

                    }   

\Conductor \\
\csa\\
\RL{\csa}

\end{document}

答案1

以下是使用先前设置的数据描述导体的一些宏。使用 做这件事xstring确实很困难,所以我选择了expl3

\documentclass{article} 
\usepackage{xparse}

\ExplSyntaxOn

\tl_new:N \l_entezari_cabletype_tl
\tl_new:N \l_entezari_conductor_tl
\tl_new:N \l_entezari_csa_tl
\tl_new:N \g_entezari_types_tl
\tl_new:N \g_entezari_rltypes_tl
\cs_generate_variant:Nn \str_case:nn { VV }

\NewDocumentCommand{\DescribeConductor}{mm}
 {% #1 = cable type, #2 = csa
  \tl_set:Nn \l_entezari_cabletype_tl { #1 }
  \tl_set:Nn \l_entezari_csa_tl { #2 }
  \tl_set:Nx \l_entezari_conductor_tl
   {
    \str_case:VV \l_entezari_cabletype_tl \g_entezari_types_tl
   }
  \noindent
  Conductor:~\l_entezari_cabletype_tl\c_space_tl(\l_entezari_conductor_tl)\\
  csa:~\l_entezari_csa_tl \\
  RL:~\str_case:VV \l_entezari_conductor_tl \g_entezari_rltypes_tl
}

\NewDocumentCommand{\defineconductors}{m}
 {
  \tl_gset:Nn \g_entezari_types_tl { #1 }
 }
\NewDocumentCommand{\defineRLtype}{mm}
 {
  \tl_gput_right:Nn \g_entezari_rltypes_tl
   {
    {#1}{\str_case:Vn \l_entezari_csa_tl { #2 } }
   }
 }

\ExplSyntaxOff


\defineconductors{
  {N2XSY}{stranded copper conductors}
  {N2XSYRY}{stranded copper conductors}
  {N2XSYBY}{stranded copper conductors}
  {NA2XSY}{stranded aluminium conductors}
  {NA2XSYRY}{stranded aluminium conductors}
  {NA2XSYBY}{stranded aluminium conductors}
}
\defineRLtype{stranded copper conductors}{
  {150}{34.5}
  {185}{36.5}
  {240}{39}
  {300}{41.5}
}
\defineRLtype{stranded aluminium conductors}{
  {150}{39}
  {185}{39.5}
  {240}{41}
  {300}{41.5}
}

\begin{document}

\DescribeConductor{N2XSY}{240}

\end{document}

\defineRLtype命令是累积的,因此您可以为每种类型的导体添加一个。

输出可以改变。

在此处输入图片描述

答案2

以下是使用expl3属性列表(也称为prop变量)的一种方法:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

% Define error messages to print when looking up a value using a key that
% doesn't belong to the corresponding mapping.
\msg_new:nnn { hadi } { unknown-cable-type }
  { Unknown~cable~type:~'\exp_not:n {#1}'. }

\msg_new:nnn { hadi } { unknown-conductor-type }
  { Unknown~conductor~type:~'\exp_not:n {#1}'. }

\msg_new:nnn { hadi } { unknown-whatever } % whatever = diameter?
  { Unknown~whatever:~'\exp_not:n {#1}'. }

\prop_const_from_keyval:Nn \c_hadi_conductor_for_cable_type_prop
  {
    N2XSY = stranded~copper~conductors,
    N2XSYRY = stranded~copper~conductors,
    N2XSYBY = stranded~copper~conductors,
    NA2XSY = stranded~aluminium~conductors,
    NA2XSYRY = stranded~aluminium~conductors,
    NA2XSYBY = stranded~aluminium~conductors,
  }

\prop_const_from_keyval:Nn \c_hadi_rl_for_stranded_copper_conductor_prop
  {
    150 = 34.5,
    185 = 36.5,
    240 = 39,
    300 = 41.5,
  }

\prop_const_from_keyval:Nn \c_hadi_rl_for_aluminium_copper_conductor_prop
  {
    150 = 39,
    185 = 39.5,
    240 = 41,
    300 = 41.5,
  }

% Note the omission of the initial backslash in each value
\prop_const_from_keyval:Nn \c_hadi_rl_conductor_type_mapping_prop
  {
    stranded~copper~conductors = c_hadi_rl_for_stranded_copper_conductor_prop,
    stranded~aluminium~conductors = c_hadi_rl_for_aluminium_copper_conductor_prop,
  }

\cs_new_protected:Npn \hadi_conductor_type_set_for_cable:Nn #1#2
  {
    \prop_get:NnNF \c_hadi_conductor_for_cable_type_prop {#2} #1
      { \msg_error:nnn { hadi } { unknown-cable-type } {#2} }
  }

\cs_generate_variant:Nn \hadi_conductor_type_set_for_cable:Nn { Nx }

% Fully expand #2, then store the conductor type in #1.
\NewDocumentCommand \setConductorTypeforCable { m m }
  {
    \hadi_conductor_type_set_for_cable:Nx #1 {#2}
  }

% Similar to \setConductorTypeforCable, except it leaves the result in the
% input stream instead of storing it in a macro.
\NewDocumentCommand \conductorTypeForCable { m }
  {
    % Fully expand the argument, then store the conductor type in \l_tmpa_tl.
    \hadi_conductor_type_set_for_cable:Nx \l_tmpa_tl {#1}
    % Leave the conductor type in the input stream (\tl_use:N may be omitted).
    \tl_use:N \l_tmpa_tl
  }

\tl_new:N \l__hadi_conductor_type_tl

% #1: macro where the result will be stored
% #2: cable type
% #3: the other parameter (some diameter, maybe?)
\cs_new_protected:Npn \hadi_rl_set:Nnn #1#2#3
  {
    \hadi_conductor_type_set_for_cable:Nn \l__hadi_conductor_type_tl {#2}

    % Get the name of the second mapping to use in \l_tmpa_tl
    \prop_get:NVNF \c_hadi_rl_conductor_type_mapping_prop
      \l__hadi_conductor_type_tl \l_tmpa_tl
      {
        \msg_error:nnV { hadi } { unknown-conductor-type }
          \l__hadi_conductor_type_tl
      }

    % Get value from the second mapping, store result in #1 (if found).
    \prop_get:cnNF { \l_tmpa_tl } {#3} #1
      { \msg_error:nnn { hadi } { unknown-whatever } {#3} }
  }

\cs_generate_variant:Nn \hadi_rl_set:Nnn { Nxx }

% Set macro #1 to the “rl” for conductor type #2 and <whatever> #3
\NewDocumentCommand \setToRl { m m m }
  {
     % Fully expand the second and third arguments, then get and store the
     % result in #1.
    \hadi_rl_set:Nxx #1 {#2} {#3}
  }

% Similar to \setToRl, except it leaves the result in the input stream instead
% of storing it in a macro.
\NewDocumentCommand \Rl { m m }
  {
    \hadi_rl_set:Nxx \l_tmpa_tl {#1} {#2}
    % Leave the result in the input stream (\tl_use:N may be omitted).
    \tl_use:N \l_tmpa_tl
  }

\ExplSyntaxOff

\begin{document}

% Direct lookup
\noindent For cable type~N2XSYBY and other parameter~240:\par
\conductorTypeForCable{N2XSYBY},
\Rl{N2XSYBY}{240}

% In case you want to use macros to define a “current value” for each
% parameter:
\newcommand*{\cableType}{NA2XSYRY}%
\newcommand*{\otherParameter}{185}%
%
\bigskip\noindent
For cable type~\cableType\ and other parameter~\otherParameter:\par
\setConductorTypeforCable{\conductorType}{\cableType}%
\setToRl{\myResult}{\cableType}{\otherParameter}%
\conductorType, \myResult

% Ditto, but without explicitly storing the results in macros
\conductorTypeForCable{\cableType},
\Rl{\cableType}{\otherParameter}

\end{document}

在此处输入图片描述

相关内容