Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding a \ref to a counter?

300 views
Skip to first unread message

Jon Haugsand

unread,
Apr 7, 1998, 3:00:00 AM4/7/98
to

In a humble attempt to simulate a spreadsheet in LaTeX, I tried to add
a \ref to a counter like this:

\newcounter{bbb}\setcounter{bbb}{50}\addtocounter{bbb}{\ref{AAA}}
(\thebbb)
...
\newcounter{aaa}\setcounter{aaa}{100}\refstepcounter{aaa}\label{AAA}


This did not work out as \ref{AAA} is not a number. Is it possible to
coerce it into a number? Or maybe redefine \ref and \label to also
save numbers?

--
Jon Haugsand
Dept. of Informatics, Univ. of Oslo, Norway, mailto:jon...@ifi.uio.no
Just say NO to frames: http://www.ifi.uio.no/~jonhaug/say-no-to-frames.html


Boris Veytsman

unread,
Apr 7, 1998, 3:00:00 AM4/7/98
to Jon Haugsand

Jon Haugsand <haug...@regulus.nr.no> writes:

>
> \newcounter{bbb}\setcounter{bbb}{50}\addtocounter{bbb}{\ref{AAA}}
> (\thebbb)
> ...
> \newcounter{aaa}\setcounter{aaa}{100}\refstepcounter{aaa}\label{AAA}
>
>
> This did not work out as \ref{AAA} is not a number. Is it possible to
> coerce it into a number? Or maybe redefine \ref and \label to also
> save numbers?
>

It worked fine for me, giving a nice 151. What was in your log file?

--
Good luck

-Boris
http://www.plmsc.psu.edu/~boris/

Heiko Oberdiek

unread,
Apr 8, 1998, 3:00:00 AM4/8/98
to

Jon Haugsand <haug...@regulus.nr.no> wrote:

>In a humble attempt to simulate a spreadsheet in LaTeX, I tried to add
>a \ref to a counter like this:
>

>\newcounter{bbb}\setcounter{bbb}{50}\addtocounter{bbb}{\ref{AAA}}
>(\thebbb)
>...
>\newcounter{aaa}\setcounter{aaa}{100}\refstepcounter{aaa}\label{AAA}
>
>
>This did not work out as \ref{AAA} is not a number. Is it possible to
>coerce it into a number? Or maybe redefine \ref and \label to also
>save numbers?

In a thread in de.comp.text.tex I have written commands to convert
\ref into a number. Here the next version as a package file:

--- snip --- 'refcount.sty' --- snip --- begin ---
% refcount.sty
% (c) Copyright 1998 by Heiko Oberdiek.
%
% User interface
%
% eg: \newcounter{CNTR}
% \refstepcounter{CNTR}\label{REF}
%
% * \setcounterwithref{CounterTwo}{REF}
% * \addtocounterwithref{CounterTwo}{REF}
%
% Hints
%
% * The \label-command should store the reference REF of the counter
CNTR
% as an arabic number (\theCNTR ==> \arabic{CNTR}).
% * Zero is assumed for undefined references.
% * Tested with packages 'calc' and 'hyperref'.
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{refcount}
[1998/04/08 Use of \ref in counter commands]

\newcommand{\@FirstParam}{}
\def\@FirstParam#1#2\END{#1}

\newcommand{\@useref}[3]{%
\begingroup
\expandafter\let\expandafter\refcmd\csname r@#3\endcsname
\ifx\refcmd\relax
\PackageWarning{refcount}{Undefined reference '#3'}%
#1{#2}{0}%
\else
#1{#2}{\expandafter\@FirstParam\refcmd\END}%
\fi
\endgroup
}

\newcommand{\setcounterwithref}[2]{%
\@useref\setcounter{#1}{#2}%
}

\newcommand{\addtocounterwithref}[2]{%
\@useref\addtocounter{#1}{#2}%
}
\endinput
--- snip --- 'refcount.sty' --- snip --- end ---

Your example looks now:

--- snip --- 'test.tex' --- snip --- end ---
\documentclass{article}}
%\usepackage{calc}
%\usepackage{hyperref}
\usepackage{refcount}

\newcounter{aaa}
\newcounter{bbb}

\begin{document}
\setcounter{bbb}{50}\addtocounterwithref{bbb}{AAA}
(\thebbb)

\setcounter{aaa}{-100}\refstepcounter{aaa}\label{AAA}

\end{document}
--- snip --- 'test.tex' --- snip --- end ---

Yours sincerely
Heiko <ober...@ruf.uni-freiburg.de>

Jon Haugsand

unread,
Apr 8, 1998, 3:00:00 AM4/8/98
to

* Boris Veytsman

| Jon Haugsand <haug...@regulus.nr.no> writes:
|
| >
| > \newcounter{bbb}\setcounter{bbb}{50}\addtocounter{bbb}{\ref{AAA}}
| > (\thebbb)
| > ...
| > \newcounter{aaa}\setcounter{aaa}{100}\refstepcounter{aaa}\label{AAA}
| >
| >
| > This did not work out as \ref{AAA} is not a number. Is it possible to
| > coerce it into a number? Or maybe redefine \ref and \label to also
| > save numbers?
| >
|
| It worked fine for me, giving a nice 151. What was in your log file?

LaTeX Font Info: ... okay on input line 13.
\c@bbb=\count88
! Missing number, treated as zero.
<to be read again>
\protect
l.16 \addtocounter{bbb}{\ref{AAA}}

?

--
Jon Haugsand
Norsk Regnesentral, <mailto:haug...@nr.no>

Jon Haugsand

unread,
Apr 8, 1998, 3:00:00 AM4/8/98
to

* Heiko Oberdiek

| % * The \label-command should store the reference REF of the counter
| CNTR

I assume the above was a victim of an unforeseen word wrap.

However, still my LaTeX complains:

(/nr/user/haugsand/texmf/tex/latex/refcount.sty)
! Incomplete \iffalse; all text was ignored after line 22.
<inserted text>
\fi
l.13 \begin
{document}
?
)
! Incomplete \iffalse; all text was ignored after line 22.
<inserted text>
\fi
<*> t2

?

*
(Please type a command or say `\end')
*
! Emergency stop.
<*>

Boris Veytsman

unread,
Apr 8, 1998, 3:00:00 AM4/8/98
to Jon Haugsand

Jon Haugsand <haug...@castor.nr.no> writes:

>
> LaTeX Font Info: ... okay on input line 13.
> \c@bbb=\count88
> ! Missing number, treated as zero.
> <to be read again>
> \protect
> l.16 \addtocounter{bbb}{\ref{AAA}}
>
> ?
>


Stranger and stranger....


Ok, let's do it straight and isolate the error. Below is my tex file
and log file. The result is (151). Can you try EXACTLY same tex file
and send the log? Do you use the same version of LaTeX as I?

--
Good luck

-Boris
http://www.plmsc.psu.edu/~boris/

---------------------tmp.tex----------------------------------------
\documentclass[12pt]{article}

\begin{document}

\newcounter{bbb}\setcounter{bbb}{50}\addtocounter{bbb}{\ref{AAA}}
(\thebbb)

\newcounter{aaa}\setcounter{aaa}{100}\refstepcounter{aaa}\label{AAA}

\end{document}

---------------------------tmp.log-------------------------------------
This is TeX, Version 3.14159 (C version 6.1) (format=latex 98.1.21) 8 APR 1998 12:31
**tmp.tex
(tmp.tex
LaTeX2e <1997/12/01> patch level 1
Hyphenation patterns for english, russian, loaded.
(/usr/local/teTeX/texmf/tex/latex/base/article.cls
Document Class: article 1997/10/10 v1.3x Standard LaTeX document class
(/usr/local/teTeX/texmf/tex/latex/base/size12.clo
File: size12.clo 1997/10/10 v1.3x Standard LaTeX file (size option)
)
\c@part=\count79
\c@section=\count80
\c@subsection=\count81
\c@subsubsection=\count82
\c@paragraph=\count83
\c@subparagraph=\count84
\c@figure=\count85
\c@table=\count86
\abovecaptionskip=\skip41
\belowcaptionskip=\skip42
\bibindent=\dimen102
) (tmp.aux)
LaTeX Font Info: Checking defaults for OML/cmm/m/it on input line 3.
LaTeX Font Info: ... okay on input line 3.
LaTeX Font Info: Checking defaults for T1/cmr/m/n on input line 3.
LaTeX Font Info: ... okay on input line 3.
LaTeX Font Info: Checking defaults for OT1/cmr/m/n on input line 3.
LaTeX Font Info: ... okay on input line 3.
LaTeX Font Info: Checking defaults for OMS/cmsy/m/n on input line 3.
LaTeX Font Info: ... okay on input line 3.
LaTeX Font Info: Checking defaults for OMX/cmex/m/n on input line 3.
LaTeX Font Info: ... okay on input line 3.
LaTeX Font Info: Checking defaults for U/cmr/m/n on input line 3.
LaTeX Font Info: ... okay on input line 3.
\c@bbb=\count87
\c@aaa=\count88
[1

] (tmp.aux) )
Here is how much of TeX's memory you used:
218 strings out of 10901
1986 string characters out of 72478
44760 words of memory out of 262141
3173 multiletter control sequences out of 9500
3938 words of font info for 15 fonts, out of 150000 for 255
14 hyphenation exceptions out of 607
22i,4n,21p,109b,160s stack positions out of 300i,40n,60p,3000b,4000s

Output written on tmp.dvi (1 page, 220 bytes).
--------------------------------end----------------------------------------


%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


Heiko Oberdiek

unread,
Apr 8, 1998, 3:00:00 AM4/8/98
to

Jon Haugsand <haug...@castor.nr.no> wrote:

>* Heiko Oberdiek
>| % * The \label-command should store the reference REF of the counter
>| CNTR
>
>I assume the above was a victim of an unforeseen word wrap.

Yes.

>However, still my LaTeX complains:
>
>(/nr/user/haugsand/texmf/tex/latex/refcount.sty)
>! Incomplete \iffalse; all text was ignored after line 22.

>[...]

* Error in \refcount.sty (But my LaTeX2e <1997/12/01> patch level 1
didn't complain? Only it produces an wrong log file output):

\ProvidesPackage{refcount}
[1998/04/08 Use of \ref in counter commands]

==> \use of unprotected \ref is wrong
\ProvidesPackage{refcount}
[1998/04/08 Use of \string\ref\space in counter commands]

* Error in test.tex: Too many closing braces:
\documentclass{article}} ==> \documentclass{article}

Pardon
Heiko <ober...@ruf.uni-freiburg.de>

Donald Arseneau

unread,
Apr 8, 1998, 3:00:00 AM4/8/98
to

In article <u3d8es4...@hal.plmsc.psu.edu>, Boris Veytsman <bo...@plmsc.psu.edu> writes...

>Jon Haugsand <haug...@castor.nr.no> writes:
>> ! Missing number, treated as zero.
>> <to be read again>
>> \protect
>> l.16 \addtocounter{bbb}{\ref{AAA}}

It should only do this on the first run, when the cross reference is
undefined. The second run should work.

Note that this method is very vulnerable to formatting changes;
it only works with the default setup from \newcounter. Any changes
to \theaaa or \p@aaa are likely to make the reference not a legal
number.

Donald Arseneau as...@triumf.ca

Jon Haugsand

unread,
Apr 9, 1998, 3:00:00 AM4/9/98
to

* Heiko Oberdiek

|
| * Error in \refcount.sty (But my LaTeX2e <1997/12/01> patch level 1
| didn't complain? Only it produces an wrong log file output):

LaTeX2e <1996/12/01> patch level 1

--
Jon Haugsand
Dept. of Informatics, Univ. of Oslo, Norway, mailto:jon...@ifi.uio.no

Jon Haugsand

unread,
Apr 9, 1998, 3:00:00 AM4/9/98
to

* Donald Arseneau

I feel then it is better to write a macro that write directly to the
aux-file something like:

\setcounter{AAA}{151}

Is it possible? What is the definition of \label?

--
Jon Haugsand
Norsk Regnesentral, mailto:haug...@nr.no

Husk personvernet: http://www.ifi.uio.no/~jonhaug/security/personvern/

David Carlisle

unread,
Apr 9, 1998, 3:00:00 AM4/9/98
to

one of you (who gets \protect in the way) is using babel.

You could look at th eversion of \ref that hyperref package uses
for this that always gives a number. Even on the firstpass it gives 0
rather than generate a warning. That way you can use it in counter
arithmetic without error.

David

0 new messages