[texhax] Counters for tables

James Smith james.smith at aleph-one.com
Thu Apr 24 20:16:16 CEST 2008


Hey,

I have used the table environment for both figures and pseudo-code
listings, so I've had to redefine the captions to read `Figure x.x:...'
and `Listing x.x:...' depending on the context. This I managed easily,
but of course the figures and listings were numbered, say...

Figure 1.1
Listing 1.2
Listing 1.3
Figure 1.4

...instead of:

Figure 1.1
Listing 1.1
Listing 1.2
Figure 1.2

I thought the smartref package would allow me to overcome this without
much programming, maybe I've got the wrong end of the stick, but I've
still had to define separate environments for the tables, incrementing
the counters ``manually'' myself:

+++

\newcounter{pseudoctr}
\newcounter{figurectr}

\addtoreflist{pseudoctr}
\addtoreflist{figurectr}

\newcommand{\resettablecounters}%
{%
    \setcounter{pseudoctr}{0}%
    \setcounter{figurectr}{0}%
}%

\newenvironment{mypseudotable}[1]%
{%
    \addtocounter{pseudoctr}{1}%
    \begin{table}[#1]%
}%
{%
    \end{table}%
    \par%
}%

\newenvironment{myfiguretable}[1]%
{%
    \addtocounter{figurectr}{1}%
    \begin{table}[#1]%
}%
{%
    \end{table}%
    \par%
}%

+++

I call the \resettablecounters command in my custom \chapter command to
reset the counters. I've then had to define dedicated ref commands...

+++

\newcommand{\mypseudoref}[1]{\chapterref{#1}.\pseudoctrref{#1}}
\newcommand{\myfigureref}[1]{\chapterref{#1}.\figurectrref{#1}}

+++

...and redefine the captions...

+++

\usepackage[labelformat=empty]{caption}

\newcommand{\mypseudocaption}[1]%
{%
    \vspace{-1.6em}%
    \caption{Listing \thechapter.\thepseudoctr: \it#1}%
}%

\newcommand{\myfigurecaption}[1]%
{%
    \vspace{-1.6em}%
    \caption{Figure \thechapter.\thefigurectr: \it#1}%
}%

+++

Have a made a meal of this? I accept that I can practically halve the
number of newly created commands and environments by passing a second
argument to differentiate between the pseudo-code listings and figures
but before I attempt this I want to know whether I'm barking up the
wrong tree...

Any help would be greatly appreciated.

Kind regards,

James



More information about the texhax mailing list