Sharing parameters between a command and an environment

(This question is related to Using \total of totcount package in section title)

I'd like to keep track of the papers I review, and I have written a code (MWE shown below). The code keeps the count of papers from different journals and then also calculates total and pending journal and conference papers, as shown in the screenshot after the code.

\documentclass[11pt,landscape]{article}

\usepackage[margin=.5in,top=0.75in]{geometry}
\usepackage{booktabs}
\usepackage[table,dvipsnames]{xcolor}
\definecolor{AleeRed}{rgb}{0.5,0,0}
pdfborder={0 0 0}]{hyperref}
\usepackage{etoolbox}

\usepackage{totcount}
\newtotcounter{TPWRD}\newtotcounter{TPWRDpending}
\newtotcounter{TPWRS}\newtotcounter{TPWRSpending}
\newtotcounter{APEC}\newtotcounter{APECpending}

\newcommand{\duedate}[2]{\stepcounter{#2}\color{magenta} Due: #1}

\let\oldsubsection\subsection
\renewcommand{\subsection}[3]{%
\oldsubsection{#1%
~\texorpdfstring{(\protect\total{#2})}{}%
\ifnumcomp{\totvalue{#3}}{=}{0}%
{}%
{~\texorpdfstring{\fbox{Pending:~\protect\total{#3}}}{}}}%
}

\newcounter{reversedummy}
\newcounter{reversecounter}
\newenvironment{articlelist}[1]{%  Don't show counter for the first row, which is the title row
\setcounter{reversedummy}{0}
\begin{center}\small
\rowcolors{2}{white}{gray!35}
\begin{tabular}{%
c<{\ifnumcomp{\value{reversedummy}}{=}{0}%          Show decreasing counter
{}%                                             Don't show counter for the title row
%                                               Calculate the reverse counter as total-i+1
{\defcounter{reversecounter}{\totvalue{#1}-\value{reversedummy}+1}\arabic{reversecounter}}%
\stepcounter{#1}\stepcounter{reversedummy}}%    Increment total papers, i
p{3.8cm} p{12cm} m{3.5cm} p{3.4cm}}
\toprule
No & ID & Title & My Decision (Date) & Decision (Date) \\
\midrule
}
{   \bottomrule
\end{tabular}
\end{center}
}

\begin{document}

% Prepare counters
\newcounter{JournalsTotal}\defcounter{JournalsTotal}{\totvalue{TPWRD} + \totvalue{TPWRS}}
\newcounter{JPendingsTotal}\defcounter{JPendingsTotal}{\totvalue{TPWRDpending} + \totvalue{TPWRSpending}}
\newcounter{ConferencesTotal}\defcounter{ConferencesTotal}{\totvalue{APEC}}
\newcounter{CPendingsTotal}\defcounter{CPendingsTotal}{\totvalue{APECpending}}
\newcounter{PapersTotal}\defcounter{PapersTotal}{\value{JournalsTotal} + \value{ConferencesTotal}}
\newcounter{PendingsTotal}\defcounter{PendingsTotal}{\value{JPendingsTotal} + \value{CPendingsTotal}}

\begin{center}
\textbf{\Large Reviewed Papers}\\
Last Updated \today; Last Count \thePapersTotal; Pending \thePendingsTotal.
\end{center}

\tableofcontents

\section{Journals (\theJournalsTotal)}
\subsection{IEEE Transactions on Power Delivery}{TPWRD}{TPWRDpending}
\begin{articlelist}{TPWRD}
&
TPWRD-000-2011 &
Pizza delivery vs. power delivery: top 10 differences you always missed &
\duedate{3-May-2012}{TPWRDpending} &
\\
&
TPWRD-001-2011.R1 &
Recent advances in the IEEE website &
Reject (24-Mar-2012) &
Reject (22-Apr-2012)
\\
\end{articlelist}

\subsection{IEEE Transactions on Power Systems}{TPWRS}{TPWRSpending}
\begin{articlelist}{TPWRS}
&
TPWRS-001-2012 &
How to have your own DYI power system &
\duedate{18-May-2012}{TPWRSpending} &
\\
&
TPWRS-002-2011.R1 &
How power corrupts any system, including power system itself &
Revise (15-Mar-2012) &
Revise (12-Apr-2012)
\\
\end{articlelist}

\section{Conferences (\theConferencesTotal)}
\subsection{APEC 2012}{APEC}{APECpending}
\begin{articlelist}{APEC}
&
APEC-000 &
Divide and conquer: How to produce two conferences out of one &
Accept (23-Aug-2011) &
\\
\end{articlelist}

\end{document}


The code does what I need it to do, but there are two things I don't like about my code:

• I have to manually include the "pending" counter when I call \duedate.
• I have to manually decrement the counters at the very end of the code.

Perhaps I can use \NewDocumentEnvironment and include \addtocounter{#1}{-1} in the end part of environment definition to solve the second problem. But I don't know how I can solve the first. The "pending" counter information is already passed to \begin{articlelist}.

Is there any way to modify \duedate so it can use the parameters of \begin{articlelist}? I also welcome any suggestions for improving the code.

-

You can "save" the name of the counter in \subsection:

\newcommand\pendingcountername{}
\renewcommand{\subsection}[3]{%
\renewcommand\pendingcountername{#3}%
...


and then

\newcommand{\duedate}[1]{\stepcounter{\pendingcountername}\color{magenta} Due: #1}


Is that what you were looking for?

-
Yes, and then I also don't really need to include the counter name in \begin{articlelist}. But I think the first \newcommand won't compile without the braces. And #1 needs to be ##1 as \duedate has to be redefined inside \subsection. –  Ali Mehrizi Apr 29 '12 at 20:34
@dochar Right about the {}, sorry I didn't test this. Edited. But about the ##1: \duedate doesn't have to be redefined anywhere as it will just access whatever value the new macro \pendingcountername has when \duedate is called. This is different from Peters approach. You don't have to change anything apart from what I wrote. –  Stephan Lehmke Apr 29 '12 at 20:50
You're right. I also don't need a separate \pendingcountername, can simply do #2pending (as in @Peter Grill's approach). –  Ali Mehrizi Apr 29 '12 at 21:25
@dochar That's correct. I wasn't aware that the naming scheme is always like that. What is the third argument of \section for then ;-) –  Stephan Lehmke Apr 30 '12 at 2:51
@Stephen, for nothing now. I have removed that too! –  Ali Mehrizi Apr 30 '12 at 4:27

Yo could redefine \duedate within the articlelist environment.

\newenvironment{articlelist}[1]{%
\renewcommand{\duedate}[1]{\stepcounter{#1pending}\color{magenta} Due: ##1}
.... rest of environment ....


Now you call \duedate with just one parameter (but only within the articlelist environment), the appropriate counter will be incremented.

Notes

• As Stephan Lehmke pointed out, in the \renewcommand{\duedate} the first use of #1 refers to the parameter to the articlelist environment, but the second one needs to refer to the \duedate parameter, hence the need for the ##.
-
I think the second #1 should be ##1 in this case. –  Stephan Lehmke Apr 29 '12 at 20:03
Yep, was just testing to see that the output was the same in both cases... and with your fix it seems to be... –  Peter Grill Apr 29 '12 at 20:06
Thanks Peter. I would "accept" your answer if I could accept two. @Stephen Lehmke's answer has the bonus feature that I won't need to pass the counter name in \begin{articlelist}. –  Ali Mehrizi Apr 29 '12 at 21:27
Since the same counter is being passed to the modified \section command, you could use a similar technique and redefine the \duedate macro there: \renewcommand{\duedate}[1]{\stepcounter{#3}\color{magenta} Due: ##1}. In you case this would also necessitate another command as it appears that you do need that counter in the articlelist macro as well. It would be easier if you have composed a MWE for exactly these issues, and removed the unnecessary complications that are not related, as it makes it harder to focus on the actual problem. –  Peter Grill Apr 30 '12 at 1:17
@PeterGrill, I had a compromise to make. Make the example "minimum" enough, while still making it informative enough if somebody wants to use the code (or know what code exactly does). –  Ali Mehrizi Apr 30 '12 at 4:29