0

I am dynamically creating commands and would like to define them based on the current value in an iteration list. The problem is that the current value appears as the last value of the iterated variable.

Here's a MWE. This example is obviously meaningless, but an application I can think of is remembering the last value in the loop. I've tried many variations of \expandafter, \edef, and can't figure it out. I've been reading about expansion. I would like to define some intermediate variable and push it to the commands I generate so that it should display "1:1, 2:2".

enter image description here

\documentclass[11pt,a4paper]{article}
\usepackage{etoolbox}
\begin{document}%
    \noindent%
    \newcounter{n}%
    \setcounter{n}{0}%
    \renewcommand{\do}[1]{%
        \let\Num#1%
        \setcounter{n}{#1}%
        \expandafter\newcommand\csname C\alph{n}\endcsname{\textbf{#1}: \Num}% create a command
    }%
    \docsvlist{1,2,3,4}%
\Ca\\
\Cb\\
\end{document}

EDIT:

Here is a more illustrative example. I would like to define a command that displays the current number, and previous number in the loop, but the previous number is always the last in the list.

enter image description here

\documentclass[11pt,a4paper]{article}
\usepackage{etoolbox}
\usepackage{xintexpr}
\begin{document}%
    \noindent%
    \newcounter{n}%
    \setcounter{n}{0}%
    \renewcommand{\do}[1]{%
        \edef\evenodd{\xintthe\xintiexpr\value{n}/:2\relax}% get the column num
        \ifnum\evenodd=0%
            \let\PrevNum#1%
        \fi
        \ifnum\evenodd=1%
            \expandafter\newcommand\csname C\alph{n}\endcsname{prev: \PrevNum, current: #1}% create a command
        \fi
        \setcounter{n}{#1}%
    }%
    \docsvlist{1,2,3,4,5,6}%
\Ca\\
\Cc\\
\Ce\\
\end{document}
4
  • 1
    What's wrong with \textbf{#1}: #1? Maybe you want to show a more meaningful example.
    – egreg
    Commented Sep 8, 2020 at 15:32
  • @egreg I'm deliberately avoiding that for conceptual purposes. My full working example is more complicated and I wanted to strip it down. Commented Sep 8, 2020 at 15:45
  • Sorry, but I can't understand what you mean by “previous element in a loop” and “remembering the last value in the loop”.
    – egreg
    Commented Sep 8, 2020 at 15:52
  • @egreg Thanks for trying--I've included a more illustrative example in an edit that will hopefully explain. Commented Sep 8, 2020 at 15:56

1 Answer 1

1

It's not really clear what the final aim is. However, \let is not the right tool and you should use \def. Also, you need to access the expansion of \PrevNum, rather than the control sequence.

\documentclass[11pt,a4paper]{article}
\usepackage{etoolbox}
\usepackage{xintexpr}

\newcounter{n}
%\setcounter{n}{0}%
\renewcommand{\do}[1]{%
  \edef\evenodd{\xintthe\xintiexpr\value{n}/:2\relax}% get the column num
  \ifnum\evenodd=0
    \def\PrevNum{#1}%
  \fi
  \ifnum\evenodd=1
    \definetablecommand{C\alph{n}}{#1}{\PrevNum}%
  \fi
\setcounter{n}{#1}%
}
\newcommand{\definetablecommand}[3]{%
  \expandafter\definetablecommandaux\expandafter{#3}{#1}{#2}%
}
\newcommand{\definetablecommandaux}[3]{%
  \expandafter\newcommand\csname #2\endcsname{prev: #1, current #3}%
}

\docsvlist{1,2,3,4,5,6}

\begin{document}

\Ca

\Cc

\Ce

\end{document}

Note that % after constants is wrong and could lead to unexpected results.

Doing such things inside document is also disputable.

enter image description here

With a different programming style.

\documentclass[11pt,a4paper]{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\definetablecommands}{m}
 {
  \ilikethevegetable_dtc_main:n { #1 }
 }

\seq_new:N \l__ilikethevegetables_dtc_data_seq
\tl_new:N \l__ilikethevegetables_dtc_item_tl

\cs_new_protected:Nn \ilikethevegetable_dtc_main:n
 {
  \seq_set_from_clist:Nn \l__ilikethevegetables_dtc_data_seq { #1 }
  \seq_map_indexed_inline:Nn \l__ilikethevegetables_dtc_data_seq
   {% ##1 is the current item index, ##2 is the current item
    \int_if_odd:nTF { ##1 }
     {% remember the item
      \tl_set:Nn \l__ilikethevegetables_dtc_item_tl { ##2 }
     }
     {% define the control sequence
      \cs_new:cpx { C \int_to_alph:n { ##1 - 1 } }
       {
        \exp_not:n { prev:~} \exp_not:V \l__ilikethevegetables_dtc_item_tl ,~
        \exp_not:n { current:~ ##2 }
       }
     }
   }
 }

\ExplSyntaxOff

\definetablecommands{1,2,3,4,5,6}

\begin{document}

\Ca

\Cc

\Ce

\end{document}

You must log in to answer this question.

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