2

I have a problem with creating a lookup table. I have two table that the out put of first one is the input of next and here is the problem. is there any body could help me.

\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}
4
  • 1
    Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. A MWE should compile and be as small as possible to demonstrate your problem. it's much easier to help you if we have full working code to start from. Also, from your description it is far from clear what you are trying to do. Can you add a schematic of what you want?
    – user30471
    Mar 1, 2020 at 10:30
  • tanx for your comment, post is modified. Mar 1, 2020 at 11:01
  • @HadiEntezari Can you please also add what's the expected output?
    – egreg
    Mar 1, 2020 at 11:55
  • @ egreg I wanna use the outputs in my text and clculation for example \Conductor will shows the cable spec and \RL will used in sum calculations. Mar 2, 2020 at 5:37

2 Answers 2

1

Here are some macros for describing a conductor using previously set data. Doing it with xstring is really difficult, so I chose 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}

The \defineRLtype command is cumulative, so you add one for each type of conductor.

The output can be changed.

enter image description here

1

Here is one way using expl3 property lists (also known as prop variables):

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

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .