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 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?

share|improve this question
    
Welcome to TeX.SX! –  Papiro Jun 29 '13 at 23:13

1 Answer 1

up vote 7 down vote accepted

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

share|improve this answer
    
Thanks, I have to read the documentation more carefully. –  sgibb Jun 30 '13 at 7:36

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.