7

I want to count my section's, figure's etc. using the totcount package. To get a general useable template I want to show only counters which are larger than zero. But it seems that total{counter} would not evaluated.

I tried the following but none of the both hide calls generate any output:

\documentclass{article}

\usepackage{totcount}

\regtotcounter{section}

\newcommand{\hide}[2] {
  \ifnum0<0#1
    #2
  \fi
}

\begin{document}

\section{Section One}
\section{Section Two}
\section{Section Three}

% should print "Sections: 3" but doesn't show anything
\hide{\total{section}}{Sections: \total{section}}
% should print nothing
\hide{\total{figure}}{Figures: \total{figure}}

\end{document}

If I wrote the numbers manually it works like expected:

% prints "Sections: 3"
\hide{3}{Sections: \total{section}}
% prints nothing
\hide{0}{Figures: \total{figure}}

What am I doing wrong? Is there a better approach to get the expected result?

0

1 Answer 1

7

Macro \total of package is not expandable, it starts with a definition. Use \totvalue instead, it maps the name to the internal counter register that can be used with \ifnum. From totcount.sty:

%%   Returns the numeric total value of a registered total counter that is
%%   passed as argument. Note that if the counter's value is not yet computed
%%   (at the first time \LaTeX\ runs on the document) this macro returns |-1|.
\newcommand\totvalue[1]{\value{#1@totc}}

Example file (I have added the missing \regtotcounter{figure}):

\documentclass{article}

\usepackage{totcount}

\regtotcounter{section}
\regtotcounter{figure}

\newcommand{\hide}[2] {
  \ifnum\totvalue{#1}>0
    #2
  \fi
}

\begin{document}

\section{Section One}
\section{Section Two}
\section{Section Three}

% should print "Sections: 3"
\hide{section}{Sections: \total{section}}
% should print nothing
\hide{figure}{Figures: \total{figure}}

\end{document}

Result

1
  • Thanks, I have to read the documentation more carefully.
    – sgibb
    Commented Jun 30, 2013 at 7:36

You must log in to answer this question.

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