1

This is a follow-up to this question: @nameuse as conditional where i asked about how to use \expandafter if i combine \@nameuse with \ifx. However, in my real-life example, i use \relax as a default value for dynamically assigned csnames. I want to know if these control sequences have a specific value, or if they are default. My naive approach would be something like:

\documentclass{standalone}

\makeatletter
\parindent\z@
\@namedef{blafasel}{\relax}


\begin{document}
\expandafter\expandafter\expandafter\ifx\@nameuse{blafasel}\relax
   true
\else
   false
\fi

\end{document}

but this doesn't work. In its simplest form it gives "false", but in more complex form (with xmltex, a macro definition, and another ifx-structure around) i get Extra \else errors. I'd like to understand why that is and what i need to do to make it work.

Bonus: I'd prefer a “plain” solution, i.e. no expl3-syntax or complex packages since i want to be as independent as possible from my code's user's configurations.

EDIT:

Here is the problem i'm facing: I want to store some information of the cells of a table. I'm using David Carlisle's xmltex mechanism to parse html tables. Elements are grabbed and transmitted directly into corresponding tex macros. The necessary counters are incremented at the beginning of every such macro.

In the cell definiton iterate over all cells and use a global macro

\gdef\SetCellProp#1#2{\expandafter\def\csname cell-\the\curtab-\the\currow-\the\curcel-#2\endcsname{#1}}

to store properties like the cells calculated width (content in \mybox; \@tempdima=\wd\mybox), its content, and its background-color. E.g., i am in the second table, third row, second column, then the width of that cell (e.g. "12mm") is stored with \SetCellProp{12mm}{width} which "creates" a control sequence named \cell-3-2-2-width that yields the value "12mm".

Likewise, i have want to store the biggest value for width so far using

\gdef\SetColProp#1#2{\expandafter\def\csname col-\the\curtab-\the\curcel-#2\endcsname{#1}}

The macro that returns that value is defined as

\gdef\GetCurColProp#1{\csname cell-\the\curtab-\the\curcel-#1\endcsname}

Note, that this definition doesn't care about the row i am in. Since i re-count cells each row, i need to decide whether to overwrite the maximum value, or not if it is present at all.

To do so, in each cell, we need to check if

    1. a maxwidth for the column the current cell is in, is already given
  • 2a. if no, make the current cell's width maxwidth for the whole column
  • 2b. if yes, look if the value for the current width is larger than maxwidth for the column
  • 3a if yes, make that current value the new maxwidth for the while column,
  • 3b if no, leave everything as it is.

The part of my cell definition so far looks like:

  %% check if maxwidth for the col is already given:
  \ifx\csname cell-\the\curtab-\the\curcel-maxidth\endcsname\relax
    %% if no, take current width (\@tempdima) as maxwidth
    \expandafter\SetColProp\expandafter{\the\@tempdima}{maxwidth}%
  \else
    %% else check if current width is larger than  maxwidth:
    \expandafter\@tempdimb\GetCurColProp{maxwidth}% 
    \ifdim\@tempdimb<\@tempdima
      %% if so, reassign maxwidth with curwidth:
      \expandafter\SetColProp\expandafter{\the\@tempdima}{maxwidth}%
    \fi
  \fi

where \curtab counts the tables, \currow counts the current row, and \curcel the number of the cell in the row (gets reset to 1 at the beginning of every row). The aim is to get the maximum width of any column my table has.

I hope that explains my problem a little bit.

  • > \blafasel=macro://->\relax .//l.9 \show\blafasel and \relax=\relax.//l.10 \show\relax show that \relax and \blafasel are different for \ifx. One is a macro expanding to \relax, the other one is \relax. – moewe Jan 24 at 12:17
  • Try \expandafter\expandafter\expandafter\ifx\@nameuse{blafasel}\testrelax where \testrelax is \def\testrelax{\relax}. Or \expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\expandafter\ifx\@nameuse{blafasel}\relax to test not \blafasel against \relax, but the expansion of \blafasel. – moewe Jan 24 at 12:18
  • 1
    Copying my comment from the other post: \ifx compares the meaning of two tokens without expanding them previously, so \relax is valid. Of course the comparison will only be true if both tokens expand to \relax, for instance this code first yields foo is NOT relax because, obviously, \foo is not \relax, it only contains \relax in its definition. The second comparison yields foo is relax because you make \foo an exact copy of \relax. – Phelype Oleinik Jan 24 at 12:22
  • Note that undefined \csnames are \relax by default, so if you had not initialised blafasel with \@namedef{blafasel}{\relax} then \expandafter\expandafter\expandafter\ifx\@nameuse{blafasel}\relax would have worked. – moewe Jan 24 at 12:23
  • @moewe the whole point of this exersice is to test whether my cs is initialised, or not. If not, then my next step is to initialise it with a pre-defined default value (other than \relax). – Lupino Jan 24 at 12:28
4

The problem you will confront will be that, unless you know in advance the types of arguments that may be passed to this test, they each may require differing numbers of expansions to finally reach the \relax or not-\relax condition.

Perhaps you need to fully expand your argument for the test:

\documentclass{standalone}

\makeatletter
\parindent\z@
\@namedef{blafasel}{\relax}


\begin{document}
\edef\tmp{\@nameuse{blafasel}}% MUST, HOWEVER, BE FULLY EXPANDABLE
\expandafter\ifx\tmp\relax
   true
\else
   false
\fi

\end{document}

enter image description here

Of course, this approach only works if the test argument is fully expandable.


Alternately, if it will always be a \@nameuse that is part of the test, then the use of 3x\expandafter only produces 2 expansions. You need 7x\expandafter to get 3 expansions, resulting in a true test.

Rule: to get n expansions, you need 2^n - 1 instances of \expandafter.

\documentclass{standalone}

\makeatletter
\parindent\z@
\@namedef{blafasel}{\relax}


\begin{document}
\expandafter\expandafter\expandafter\expandafter\expandafter%
  \expandafter\expandafter\ifx\@nameuse{blafasel}\relax
   true
\else
   false
\fi

\end{document}

enter image description here

  • Thanks. I fear, i dont understand "fully expandable". my actual exampe is \@namedef{blafasel\c@mycounter}. The "value" of \blasasel<x> can be a string, a dimension, a length, a number, a cs, or \relax... Say, i know that \blafasel1 is a string (or relax), \blafasel2 is a dimension (or relax), does that help me? – Lupino Jan 24 at 12:25
  • What do you mean, you don't know how many expansions are necessary? \@nameuse{foo} will always take exactly 3 expansions to arrive at the expansion of \foo, won't it? – schtandard Jan 24 at 13:00
  • Imagine a table and for each cell of any that table, i want to store width, height, content. So my version of @namedef is \def\@mynamedef#1#2{\expandafter\gdef\csname cell-\the\table-\the\c@row-\the\c@cell-#2\endcsname{#1}} with #2 being the propety (content, width, height), and #1 the value. I sorta switched #1 and #2 so i can expand length registers prior to assignment into my property matrix using \expandafter\@mynamedef\expandafter{\the\@tempdima}{width} which stores (e.g.) 12mm as value of \csname cell-1-2-2-width\endcsname. – Lupino Jan 24 at 13:12
  • @Lupino I will try to understand what you are asking here, but it would be useful for you to edit your question and provide additional code on what does and doesn't work for you. – Steven B. Segletes Jan 24 at 13:25
  • @Lupino See my edit. The use of 3x\expandafter in your MWE only produces 2 expansions. You would need 7x\expandafter to get the triple expansion you seek. – Steven B. Segletes Jan 24 at 13:38
1

You just need to give no initial definition to \blafasel. If you want to test whether it has received a definition, just do

\expandafter\ifx\csname blafasel\endcsname\relax
   <still not defined>%
\else
   <is defined>%
\fi

This exploits the fact that a token constructed with \csname is made equivalent to \relax if it has no previous definition.

1

\@nameuse{blafasel} expands to \csname blafasel\endcsname, which you expand once again yielding \blafasel. You then compare this to \relax, which is different. You have three possibilities of solving your problem:

  1. You can expand \blafasel once more:

    \@namedef{blafasel}{\relax}%
    \expandafter\expandafter\expandafter\expandafter
    \expandafter\expandafter\expandafter\ifx\@nameuse{blafasel}\relax
        true
    \else
        false
    \fi
    

    Note that this \ifx will always follow the true branch when the expansion \blafasel starts with two equal tokens and is thus not a good test. Try \@namedef{blafasel}{OOH, LOOK, AN ERROR!}, for example.

  2. You can define a different macro to compare it to:

    \@namedef{blafasel}{\relax}%
    \def\@test@macro{\relax}%
    \expandafter\expandafter\expandafter\ifx\@nameuse{blafasel}\@test@macro
        true
    \else
        false
    \fi
    
  3. You can initialize your macros to be the same as \relax instead of a macro expanding to \relax:

    \def\@namelet#1#2{\expandafter\let\csname #1\endcsname#2}%
    \@namelet{blafasel}\relax
    \expandafter\expandafter\expandafter\ifx\@nameuse{blafasel}\relax
        true
    \else
        false
    \fi
    

    As egreg points out, you could just as well leave \blafasel undefined (i.e. omit the first two lines of this code block), since \csname will default to \relax, when the macro is not defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.