0

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.

\documentclass[a4paper]{article}
\usepackage{
    etoolbox,
    xstring,
}
\newbool{hasReadPeriod}
\newbool{openDataFile}
\newcounter{foo}


\newcommand\TwoDigits[1]{\ifnum#1<10 0#1\else#1\fi}
\newcounter{year}\setcounter{year}{2016}
\newcounter{month}[year]\setcounter{month}{9}
\newcounter{day}[month]\setcounter{day}{15}
\begin{document}
\newread\fileA
\openin\fileA=periods.dat%

\immediate\newwrite\fileB

\def\Date{\theyear-\TwoDigits{\themonth}-\TwoDigits{\theday}}

\def\XXXX{
    \ifeof\fileA
      % EOF\par
    \else
        \ifbool{hasReadPeriod}%
            {%
                %
            }%
            {%  
                \read\fileA to \aaaaa%
                \StrGobbleRight{\aaaaa}{1}[\aaaaa]%
                \read\fileA to \bbbbb%
                \StrGobbleRight{\bbbbb}{1}[\bbbbb]%
                \global\booltrue{hasReadPeriod}%
            }%
        \ifbool{openDataFile}%
            {%
                \ifx\Date\bbbbb%
                    % OPENDATAFILE
                    \global\boolfalse{openDataFile}%
                    \global\boolfalse{hasReadPeriod}%
                    \immediate\closeout\fileB%
                \else
                    \immediate\write\fileB{\Date{} newstuffhere}%
                \fi
            }%
            {% is closed, should open?
                \ifx\Date\aaaaa %
                    \global\booltrue{openDataFile}%
                    \stepcounter{foo}% `
                    \immediate\openout\fileB=\thefoo.dat%
                    \immediate\write\fileB{\Date newstuffhere}%
                \else
                    \Date|\par%
                    \aaaaa|\par%
                    \hrule\vspace*{0.5cm}%
                \fi

            %   \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}}
            }%
    \fi
    \stepcounter{day}
}
INCREMENT DATE\par
\XXXX{}% 
\XXXX{}%
\XXXX{}%
\XXXX{}%
\XXXX{}%
\XXXX{}%
\XXXX{}%
\XXXX{}%
dasdsad
\def\Date{2016-09-16 }
Date \Date{}\ and \aaaaa{}.
\ifx\Date\aaaaa
    T
\else
    F
\fi

\end{document}
%==========================================
2016-09-16
2016-09-21

sample contents of periods.dat

2016-09-16
2016-09-21
2016-09-28
2016-09-30
6
  • \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
2
\ifx\Date\aaaaa

compares \aaaa which is defined as if by

\def\aaaa{2016-09-15}

with \Date which is defined by

\def\Date{\theyear-\TwoDigits{\themonth}-\TwoDigits{\theday}}

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

 \edef\Date{\theyear-\TwoDigits{\themonth}-\TwoDigits{\theday}}

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

\def\date{2016-09-15}

and would then have been \ifx equal to \aaaa

2
  • 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.