# Manipulate a LaTeX2e counter with LaTeX3

I have some expl3 code where I want to manipulate a counter which is defined by a package using LaTeX2e code.

If I want to manipulate the counter foo, I could either mixing up LaTeX2e code with LaTeX3 code:

% some expl3 code
% more expl3 code


or I could do it using only expl3 commands:

% some expl3 code
% more expl3 code


Which way of manipulating the counter is preferable? Or does it not matter which way I choose?

Edit: Here is a MWE, using the datenumber package:

\documentclass{article}
\usepackage{datenumber,xparse}

\makeatletter
\ExplSyntaxOn

\cs_new_protected:Npn \juhu_futuredate:n #1
{
\setdatetoday
\setdatebynumber{\thedatenumber}
In ~ #1 ~ days ~ is ~ \datedate.
}

\NewDocumentCommand{\futuredate}{m}
{
\juhu_futuredate:n { #1 }
}

\ExplSyntaxOff
\makeatother

\begin{document}

\futuredate{7}

\end{document}


The datenumber package defines the datenumber counter and assigns an integer to it with the \setdatetoday command. \datedate prints a date depending on the value given to \setdatebynumber. Of course, I could define an own integer and pass it to the \setdatebynumber command, but the integer given by \setdatetoday is nontrivial and I want to avoid to do the calculations on my own.

-
I think answers to this question will depend on how you're implementing this. A MWE might help. And then, depending on the context there will most likely be a wide variety of opinion. But without a MWE, you're only likely to get very general answers. – A.Ellett Mar 22 '14 at 17:10
My general advice would be to try to build an interface between the old and the new that will allow you to keep the two syntaxes separate. Then again, without a MWE it's hard to explain how to do this (or whether it's appropriate or feasible for what you have in mind). – A.Ellett Mar 22 '14 at 17:17
\addtocounter{foo}{123} just checks that the counter is defined and, in case it is, it globally adds the value, so there's not much difference with \int_gadd:cn {c@foo}{123}. There is a big difference with \stepcounter{foo}, because this doesn't simply step the counter, but also zeroes the counters associated to foo. – egreg Mar 22 '14 at 17:31
Hmmm. The question I'm left with is "why are you even bothering with expl3?" It seems like xparse would be all that you really need. You're hardly doing much of anything with expl3. Perhaps that's the nature of your MWE. But your MWE isn't really showing any need for expl3. – A.Ellett Mar 22 '14 at 18:59
@A.Ellett My MWE is very minimal. There is some more expl3 code in my actual LaTeX file. – JuHu Mar 22 '14 at 19:05

I guess I can provide a more-or-less 'official' answer: use the LaTeX2e commands. Mixing up the two interfaces is asking for trouble, and while we are working on several areas we've not got a 'user level' counter approach at yet. (Indeed, the entire question of how variables at the document-level should be handled is open.)

The reason I'd use \setcounter here is that \c@... is an internal for LaTeX2e, and the entire point of expl3 is to have clear interfaces and internals. There's no reason to abuse the interfaces here (no functionality gain), so stick with them.

-

First I would avoid using internal commands if at all possible: particularly when there is already an interface provided. So for example, I would use

\addtocounter{datenumber}{...}


\c@datenumber


Also, I would use \addtocounter with counters provided at the LaTeX user-interface level. I would use \int_gadd:Nn only for counter-like things defined with expl3.

Next, I would not mix @ notation with expl3 notation. If you must use both, I would clearly separate the two and create an interface if they must somehow share information. But if at all possible, I would try to avoid the mixing of the two, even with an interface.

Then I would create my own set of expl3 commands to interactly directly with those provided by any particular package. Within these commands, I would only use standard LaTeX macros/structures and any LaTeX macros/structures provided by packages for the user interface.

Finally, I would create the expl3 code that on the surface is purely expl3 code.

So, following the above recommendation, your MWE would become something like:

\documentclass{article}
\usepackage{datenumber,xparse}

\makeatletter
%% define/work with functions that use @
%% notation create an interface for these
%% functions if necessary.
\makeatother

\ExplSyntaxOn
%% User level interface
\NewDocumentCommand{\futuredate}{m}
{
\juhu_futuredate:n { #1 }
}

%% Internal expl3 code that on the surface remains purely expl3
%% Though these functions might "stand in" for non-expl3 code.
\cs_new_protected:Npn \juhu_futuredate:n #1
{
%% ... expl3 code ...
\_juhu_initialize_date:
%% ... expl3 code ...
%% ... expl3 code ...
\_juhu_reset_by_datenumber:
%% ... expl3 code ...
\_juhu_print_future_date:n {#1}
%% ... expl3 code ...
}

%% Interface with commands from datenumber package
\cs_new_protected:Nn  \_juhu_initialize_date:       {\setdatetoday}
\cs_new_protected:Nn  \_juhu_reset_by_datenumber:   {\setdatebynumber{\thedatenumber}}