One .dat file contains periods (beginning and ending dates on separate rows) which are to be read once then compared with the date, and if it matches it goes \immediate\write\fileB{\Date{} #2, #3} in the command. The MWE given works in the same.

\read<#> to \whatever gives ends the cs with a space, which can be gobbled with \StrGobbleRight{\whatever}{1}[\whatever] or the definition of \Date and be given an extra space at the end, both work outside the command. Inside, however, I can't get it to match and I don't know why, there's obviously something small I'm missing. Both expand to the same thing without any newline, space characters, the | in there is just for this. The one conditional is commented out, so I could test the given tex conditionals along with the ones in xstring, etoolbox has some conditionals, as well, but I've lost tracked of what I've tried already.


\newcommand\TwoDigits[1]{\ifnum#1<10 0#1\else#1\fi}



      % EOF\par
                \read\fileA to \aaaaa%
                \read\fileA to \bbbbb%
                    % OPENDATAFILE
                    \immediate\write\fileB{\Date{} newstuffhere}%
            {% is closed, should open?
                \ifx\Date\aaaaa %
                    \stepcounter{foo}% `
                    \immediate\write\fileB{\Date newstuffhere}%

            %   \IfStrEq{\Date{}}{\aaaaa}%
            %       {%
            %           % CLOSEDATAFILE\par%
            %           {\global\booltrue{openDataFile}}%
            %           \stepcounter{foo}%
            %           \immediate\openout\fileB=\thefoo.dat%
            %           \immediate\write\fileB{\Date{} newstuffhere}%
            %       }%
            %       {\Date{}|\par
            %       \aaaaa|\par
            %       \hrule\vspace*{0.5cm}}
\def\Date{2016-09-16 }
Date \Date{}\ and \aaaaa{}.


sample contents of periods.dat

  • \ifx\Date{ compares Date with { which will be false unless \Date was defined by \let\Date{ Oct 2 '18 at 17:34
  • either leave the space there and compare to \space or remove it and compare to \@empty Oct 2 '18 at 17:38
  • That doesn't explain why they work outside the command, and don't inside. I've tried without brackets, 1.dat is always empty. I don't understand why I'd compare it to \@empty or \space, I'm comparing dates in the form of YYYY-MM-DD?
    – vlg
    Oct 2 '18 at 18:41
  • what works outside the command? \ifx\Date{ compares \Date and { (so is false) wherever you put it?. I presumed the intention of \ifx\Date{} was to test if \Date was empty but that should be \ifx\Date\@empty Oct 2 '18 at 18:46
  • Added example of it at the end of the doc. Even if defined via the counters and TwoDigits, it works.
    – vlg
    Oct 2 '18 at 18:53

compares \aaaa which is defined as if by


with \Date which is defined by


as you can see, these are not equal, \ifx just compares the list of tokens in the replacement texts of the two macros. If you had defined \Date by


then it would have been defined by the expansion of those tokens at the point of definition so been equivalent to


and would then have been \ifx equal to \aaaa

  • This doesn't explain why the `\ifx\Date\aaaaa` works outside the command environment, as the example at the end shows a 'T'. Changing it to \edef also makes it so it's always the same, that is, you'd have to edef it every time, and even so, it still didn't create a 1.dat - it didn't match, at least on my side ---- "at the point of definition, so jsut stick it in the command, got it. Many thankarinos
    – vlg
    Oct 2 '18 at 19:33
  • @vlg yes it does because in the example at the end you have defined \Date by \def\Date{2016-09-16 } so it is \ifx equal to \aaa if you have defined it in terms of \theyear you would have had to use \edef as I explain above. Oct 2 '18 at 19:35

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.