GeneralTotcount counter problem?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
NChairFix
Posts: 7
Joined: Thu May 21, 2015 9:08 am

Totcount counter problem?

Post by NChairFix »

Hi,

I have started using Latex to do our company manuals, everything seemed to be going well, apart from the odd hiccup.
Part of these manuals have tables of information, one of the rows needs to include the number of rows in the particular table.

This all seemed to work well using the totcount package. However after more work was done placing the tables in a macro, the whole table and totcount affair fell apart.

After investigating it further I have come up with a MWE that replicates the basic idea/problem, but simplified a lot.

Code: Select all

\documentclass[11pt,portrait]{article}
\usepackage{totcount}

\newcommand\currentProtocol{p1}
\newcommand{\createCounter}[1]
{
	\newtotcounter{mbc#1 Ac}
	\setcounter{mbc#1 Ac}{0}
	creating counter:'mbc#1 Ac'

}

\newcommand{\addToCounter}[2]
{
	\addtocounter{mbc#1 Ac}{#2}
	incrementing counter:'mbc#1 Ac' by #2

}

\newcommand{\printCounter}[1]
{
	total of the counter = \total{mbc#1 Ac}
}

\begin{document}

\renewcommand\currentProtocol{p1}
protocol: \currentProtocol

\createCounter{\currentProtocol}
\addToCounter{\currentProtocol}{1}
\addToCounter{\currentProtocol}{10}
\printCounter{\currentProtocol}

\rule{\linewidth}{1pt}

\renewcommand\currentProtocol{p4}
protocol: \currentProtocol

\createCounter{\currentProtocol}
\addToCounter{\currentProtocol}{4}
\addToCounter{\currentProtocol}{40}
\printCounter{\currentProtocol}

\rule{\linewidth}{1pt}
\newtotcounter{mbcNEWONE Ac}
\end{document}
Running this MWE twice, as required, I would have expected there to be two counter values. Except the first counter generated doesn't seem to be found anywhere, while the last one seems correct. The first counter 'mbcp1 Ac' should be 11.

I believe the problems lies in the fact that as there could be lots of tables in my document I am having to create new total counters on the fly with a name given by the value of the macro \currentProtocol. It is as if the counters are only generated using the final value of the macro \currentProtocol.

Looking at the .aux file this does appear to be the case as I am guessing there should be a line in that file for each counter:

Code: Select all

\relax 
\expandafter\ifx\csname c@mbcp4 Ac@totc\endcsname\relax\newcounter{mbcp4 Ac@totc}\fi\setcounter{mbcp4 Ac@totc}{44}
\expandafter\ifx\csname c@mbcp4 Ac@totc\endcsname\relax\newcounter{mbcp4 Ac@totc}\fi\setcounter{mbcp4 Ac@totc}{44}
\expandafter\ifx\csname c@mbcNEWONE Ac@totc\endcsname\relax\newcounter{mbcNEWONE Ac@totc}\fi\setcounter{mbcNEWONE Ac@totc}{0}
But the counter 'mpcp1 Ac' is missing.


Does anyone have any ideas on how to resolve or get round this?
I did think that maybe I could do it with luatex but unfortunately I am using a landscape page with hyperlinks on nodes which appears to be a known bug, so that is out of the question.

Many thanks in advance.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

BlackForestrian

Totcount counter problem?

Post by BlackForestrian »

Apparently, \newtotcounter does not expand its argument directly.
This means that at the time of writing the counter value to the .aux - file, \currentProtocol contains the p4 value and not p1. This must be prevented by using an \edef pre-expansion, feeding this definition to \newtotcounter to any of the counter commands defined in the OP's example.

However, it's not a good idea to define counters on the fly!

Code: Select all

\documentclass[11pt,portrait]{article}
\usepackage{totcount}

\newcommand{\namescheme}[1]{%
  mbc#1 Ac%
}

\makeatletter
\newcommand\currentProtocol{p1}
\newcommand{\createCounter}[1]
{%
  \begingroup%
  \edef\tempcounter@@name{\namescheme{#1}}%
  \expandafter\newtotcounter\expandafter{\tempcounter@@name}%
  \expandafter\setcounter\expandafter{\tempcounter@@name}{0}%
  \typeout{creating counter:'\namescheme{#1}'}
 \endgroup

}


\newcommand{\addToCounter}[2]
{
  \begingroup
  \edef\tempcounter@@name{\namescheme{#1}}%
  \expandafter\addtocounter\expandafter{\tempcounter@@name}{#2}
  incrementing counter:'\namescheme{#1}' by #2
  \endgroup

}

\newcommand{\printCounter}[1]
{
  total of the counter = \total{\namescheme{#1}}%

}
\makeatother




\begin{document}

protocol: \currentProtocol
\renewcommand\currentProtocol{p1}
\createCounter{\currentProtocol}
\addToCounter{\currentProtocol}{1}
\addToCounter{\currentProtocol}{10}
\printCounter{\currentProtocol}

\rule{\linewidth}{1pt}

\renewcommand\currentProtocol{p4}
protocol: \currentProtocol

\createCounter{\currentProtocol}
\addToCounter{\currentProtocol}{4}
\addToCounter{\currentProtocol}{40}
\printCounter{\currentProtocol}

\rule{\linewidth}{1pt}
\end{document}
NChairFix
Posts: 7
Joined: Thu May 21, 2015 9:08 am

Re: Totcount counter problem?

Post by NChairFix »

Thankyou for the information and code.

It will take me a while to digest all that code as I am fairly new to latex and certainly new to that level of coding.

You say that defining counters on the fly isn't a good idea, how would you recommend me assigning a counter to my own table environment then please?
I was trying to avoid doing things my hand and having the macros seemed like a good idea but coming from a programming background everything looks like a programming problem to me!
Many thanks
BlackForestrian

Totcount counter problem?

Post by BlackForestrian »

Some comments to your reply
  1. Counters are managed using registers, provided by the TeX core. It's better to define them in the preamble first, then use them in the document body (i.e. in \begin{document}...\end{document}

    If one does not have too sophisticated things in mind, it might be alright to use \newcounter
  • I think I can realize your reasonings concerning the programming: I am a programmer too -- TeX is a lot different compared to C/C++/Java. The expansion (i.e. the replacement of macros basically on the fly poses new problems to traditional programmers. It takes a while to get accustomed to it. Give it a try ;)
NChairFix
Posts: 7
Joined: Thu May 21, 2015 9:08 am

Re: Totcount counter problem?

Post by NChairFix »

Thank you again for the reply.

As latex is pretty new to me I am trying to avoid doing non-recommend things but I am going to have to see how the on the fly counters workout for me. Admittedly maybe I am using the macros too much but we will see.....

I must admit I am not looking forward to explaining to a load of none programmers how to use this new manual creation system (latex) when I finally get it all up and running :?

Thank you for your help
BlackForestrian

Re: Totcount counter problem?

Post by BlackForestrian »

I assume that your non-programmer users will use LaTeX to generate the manuals?

My suggestion is to write a package with an easy interface for the non-programmer users. You have to take care that some standard cases are provided without much ado (whatever a standard case for the manuals you mentioned might be...)

The problem is, that easy interfaces often require a strong background in LaTeX/TeX programming

BF (BlackForester -- which is the correct name for me, but can't change my user name here ;-))
NChairFix
Posts: 7
Joined: Thu May 21, 2015 9:08 am

Re: Totcount counter problem?

Post by NChairFix »

Yes non programmers will be doing manuals, at least I hope they will! Though Im sure I will face some opposition from some of them as we all dislike writing manuals with a passion! Though I must admit working though the manuals this way is proving more fun/interesting than normal :D .

I hadn't thought about doing a package, though I have been doing some new environments and lots of commands for tasks. Hopefully these will cover the standard cases for connector pinouts, remote control messages, LCD menus etc. Its where one manual may require a slightly non standard way of doing things that I can see may cause problems, something that I guess will only be seen when others are let loose on the system.

All the consolidating of the varied old manual styles isn't a quick process but hopefully this process + latex, in the long term should make the manuals more consistant, more useful, easier to read and write.
Post Reply