[Tex/LaTex] Using \total of totcount package in section title

counterssectioning

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}

Best Answer

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}

enter image description here

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.

Related Question