On 2018-01-31 01:04, Wolfgang Schuster wrote:

Rik Kabel
31. Januar 2018 um 03:10

Listers,

I have a problem, and a question on ConTeXt programming efficiency.

In the example below, I have a set of variables. When these are reference directly via \getvariable, everything works as expected in simple text and in TABLEs. When I \define a macro to the \getvariable, that works in simple text, but only the value of the last iteration appears in the TABLE. The macro definition is saved and when it is used, that is the value that it has.

So, how can I \define (or \def, ...) a macro to the expanded value to avoid this? That is the problem.

The question is, Is there is any advantage to be had in doing this? Assume that the value is referenced many (tens of) times. There seems to be an aesthetic value of factoring out the multiple identical instances of the \getvariable syntax and assigning a more semantically informative name, but beyond that, is there any other value?


Natural tables collect the content of all cells to perform the width and height calculations
and you have to expand the content of the cells to get the current value of \getvariable.

\starttexdefinition unexpanded doTableRow #SET
    \bTR
      \expanded{\bTC\getvariable{#SET}{a}\eTC}
      \expanded{\bTC\getvariable{#SET}{b}\eTC}
    \eTR
\stoptexdefinition


BTW: You can use the \processcommacommand command when you save your lists in a macro (no need for \expandafter).

\defineexpandable\Sets{one,two}

\processcommacommand[\Sets]\doInlineTextExp


Wolfgang

Thank you for the help and the processcommacommand bit. However, the problem is still there (and I was just wrong in my own follow-up).

Below is an updated example with and without the \expanded{\bTC...

It does not seem to make a difference here, but perhaps it is needed in some situations.

My question is about factoring the \getvariable out of the \bTC rows (in this example) and \defining a simpler macro to hold the value. Should it be done, and if so, how?

% macros=mkvi engine=luajittex

\setvariables      [one]
                   [a=1aaaaaaaa,b=1b,c=1c]
\setvariables      [two]
                   [a=2a,b=2bbbbbbbbbbbbbbbb,c=2c]

\defineexpandable  \Sets
                   {one,two}

\starttexdefinition unexpanded doInlineText #SET
    Direct: \getvariable{#SET}{a} \getvariable{#SET}{b}\
            \getvariable{#SET}{c}\par
\stoptexdefinition

\starttexdefinition unexpanded doTableRow #SET
    \bTR
      \bTC\getvariable{#SET}{a}\eTC
      \bTC\getvariable{#SET}{b}\eTC
      \bTC\getvariable{#SET}{c}\eTC
    \eTR
\stoptexdefinition

\starttexdefinition doInlineTextExpA #SET
    \define\A{\getvariable{#SET}{a}}
    \define\B{\getvariable{#SET}{b}}
    \define\C{\getvariable{#SET}{c}}
    Factored A: \A\ \B\ \C\par
\stoptexdefinition

\starttexdefinition doTableRowExpA #SET
    \define\A{\getvariable{#SET}{a}}
    \define\B{\getvariable{#SET}{b}}
    \define\C{\getvariable{#SET}{c}}
    \bTR
      \bTC\A\eTC
      \bTC\B\eTC
      \bTC\C\eTC
    \eTR
\stoptexdefinition

\starttexdefinition doInlineTextExpB #SET
    \define\A{\getvariable{#SET}{a}}
    \define\B{\getvariable{#SET}{b}}
    \define\C{\getvariable{#SET}{c}}
    Factored B: \A\ \B\ \C\par
\stoptexdefinition

\starttexdefinition doTableRowExpB #SET
    \define\A{\getvariable{#SET}{a}}
    \define\B{\getvariable{#SET}{b}}
    \define\C{\getvariable{#SET}{c}}
    \bTR
      \expanded{\bTC\A\eTC}
      \expanded{\bTC\B\eTC}
      \expanded{\bTC\C\eTC}
    \eTR
\stoptexdefinition

\starttext

    \processcommacommand[\Sets]\doInlineText

    \bTABLE[frame=off]
      \bTABLEhead
        \bTR[nc=2]
          \bTH Direct\eTH
        \eTR
      \eTABLEhead
      \bTABLEbody
          \processcommacommand[\Sets]\doTableRow
      \eTABLEbody
    \eTABLE

    \processcommacommand[\Sets]\doInlineTextExpA

    \bTABLE[frame=off]
      \bTABLEhead
        \bTR[nc=2]
          \bTH Factored A\eTH
        \eTR
      \eTABLEhead
      \bTABLEbody
        \processcommacommand[\Sets]\doTableRowExpA
      \eTABLEbody
    \eTABLE

    \processcommacommand[\Sets]\doInlineTextExpB

    \bTABLE[frame=off]
      \bTABLEhead
        \bTR[nc=2]
          \bTH Factored B\eTH
        \eTR
      \eTABLEhead
      \bTABLEbody
        \processcommacommand[\Sets]\doTableRowExpB
      \eTABLEbody
    \eTABLE

\stoptext


--
RIk