# Using \total of totcount package in section title

I am keeping track of all the journal papers I review, and of course I'm using LaTeX.

There are different journals I review for and I have different tables and counters keeping track of papers. Each journal has it's own section---something similar to this:

IEEE Transactions on Power Systems (2)

1. Title of the first paper, my decision, editor's decision.
2. Title of the second paper, my decision, editor's decision.

IEEE Transactions on Power Delivery (1)

1. Title of the first paper, my decision, editor's decision.

I have defined environments and commands to automate things as much as possible. I want to be able to show the number of papers at the beginning of my document; therefore, I'm using totcount package. Then simply writing something like (after defining TPWRD as a "total" counter)

Total number of TPWRD papers: \total{TPWRD}


does the trick. But I also want to show this number in the section title, as shown above in the parenthesis, and in the table of contents. My problem is that using \total as part of a section/subsection title gives me the error "Undefined control sequence." This happens without any parametrization. That is, even

\section{Simple Section \total{TPWRD}}


results in an error. Using

\section[Simple Section]{Simple Section \total{TPWRD}}


avoids the problem by showing the counter value only in the section title and not in the table of contents. How can I have both?

I have also tried \protect\total{TPWRD}, but it results in another error: Missing number, treated as zero.

Here's a MWE:

\documentclass{article}

\usepackage{hyperref}

\usepackage{totcount}
\newtotcounter{TPWRD}

\begin{document}

\tableofcontents

\section{Journals \total{TPWRD}}
\stepcounter{TPWRD}

\end{document}

-
A wild guess... have you tried putting \protect\total{TPWRD}? –  Harish Kumar Apr 27 '12 at 7:51
@HarishKumar \protect does the trick. You should make an answer. –  Stephan Lehmke Apr 27 '12 at 8:43
–  Marco Daniel Apr 27 '12 at 9:38
@HarishKumar, Yes, I did try \protect. I should've mentioned this in my question. \protect\total{TPWRD} gives me another error: Missing number, treated as zero. –  Ali Mehrizi Apr 27 '12 at 14:58
The problem with hyperref is that the computation for \total cannot be performed when preparing the bookmark text. So a non perfect remedy is to say \section{Journals \texorpdfstring{\protect\total{TPWRD}}{}}. It's not perfect because the number won't appear in the bookmark. –  egreg Apr 27 '12 at 15:46

Your MWE will not work without \protect. Earlier I had no idea that you are using hyperref. Your MWE works in the following form:

\documentclass{article}

\usepackage{hyperref}   % It does work even with hyperref.

\usepackage{totcount}
\newtotcounter{TPWRD}

\begin{document}

\tableofcontents

\section{Journals \texorpdfstring{\protect\total{TPWRD}}{}}  % Works only with  \protect and also with  hyperref
\stepcounter{TPWRD}

\stepcounter{TPWRD}

\stepcounter{TPWRD}

\end{document}


PS: By adding a full MWE with your question, you will be helping us to avoid wasting time and the probability that you will get an accurate answer, also increases.

-
Thanks. As @egreg mentioned above, this solution does not show the "total" value in the bookmarks, but it does what I was looking for. –  Ali Mehrizi Apr 28 '12 at 3:06

The argument of the \section command may be used for more than just producing a section title such as toc entries, running headings etc. When we use a fragile command inside a \section command, it will get broken while being moved around between different files/places (like .toc etc). The command \total is not a robust command (ie., it is fragile). Naturally, when it is used in a moving argument like \section, it will get broken and one will get an error. To prevent this we have to \protect the fragile commands like

\protect\total{TPWRD}


When we use \section[Simple Section]{Simple Section \total{TPWRD}}, the short title will be moving around in toc etc and the main argument will be used only for producing the section headings. Hence errors will not be seen. But the toc entries will not carry the total counts of whatever you intend.

-
as I also mentioned above, \protect won't work with this. I should've mentioned this in my question (will update). Perhaps it's the way totcount is implemented and perhaps doesn't define certain commands until certain points? –  Ali Mehrizi Apr 27 '12 at 15:04
@dochar: Also, it will be better if you add a MWE showing the problem. Otherwise we will be left making wild guess all through. –  Harish Kumar Apr 27 '12 at 15:08
Harish, it doesn't even get past the first run because of the error. –  Ali Mehrizi Apr 27 '12 at 15:09

The definition of \total in totcount.sty is far from perfect, but works well outside of moving arguments such as section titles.

I propose you to use, in those situations, a different command:

\makeatletter
\newcommand\stotal[1]{%
\ifnum\value{#1@totc}=\m@ne
??%
\else
\number\value{#1@totc}%
\fi
}
\makeatother


so that

\section{Journal \stotal{TPWRD}}


will work with or without hyperref. It won't give warnings if the number is still not defined, though.

-