Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Adding a \ref to a counter?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Heiko Oberdiek  
View profile  
 More options Apr 8 1998, 3:00 am
Newsgroups: comp.text.tex
From: oberd...@ruf.uni-freiburg.de (Heiko Oberdiek)
Date: 1998/04/08
Subject: Re: Adding a \ref to a counter?

Jon Haugsand <haugs...@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 <oberd...@ruf.uni-freiburg.de>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google