7

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}
6
  • 3
    A wild guess... have you tried putting \protect\total{TPWRD}?
    – user11232
    Apr 27, 2012 at 7:51
  • @HarishKumar \protect does the trick. You should make an answer. Apr 27, 2012 at 8:43
  • 2
  • @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. Apr 27, 2012 at 14:58
  • 1
    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, 2012 at 15:46

3 Answers 3

4

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.

1
  • Thanks. As @egreg mentioned above, this solution does not show the "total" value in the bookmarks, but it does what I was looking for. Apr 28, 2012 at 3:06
9

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.

3
  • 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? Apr 27, 2012 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.
    – user11232
    Apr 27, 2012 at 15:08
  • Harish, it doesn't even get past the first run because of the error. Apr 27, 2012 at 15:09
6

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .