Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
3  
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
2  
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 '12 at 15:46
2  
@dochar: Your example won't compile without \protect as you said. BTW your acceptance rate is only 25% which may not match with the tradition here. Please consider visiting all your questions and if they were useful pl. consider upvoting and accepting them. –  Harish Kumar Apr 27 '12 at 15:53

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
    
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.

share|improve this answer
    
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.