GeneralDefinition of a Macro involving Counters

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
leo simon
Posts: 17
Joined: Wed Aug 19, 2009 5:41 pm

Definition of a Macro involving Counters

Post by leo simon »

Hi,

I've defined the macro \linkedLabels below to give me linked equation numbers

What I've done works ok, but there are two problems with it
1) I'd like to define \newcounter inside the macro, rather than separately in the preamble, but it appears that newcounter has to be in the preamble
2) It's silly to have arguments #2 and #3 be separate since obviously #3 is determined by #2. However, the command \tag{\the#2$\empty_\text{#4}$} doesn't work. Nor does \tag{\value{#2} etc}

So to summarize, my questions are:
1) is there away to define counters in the body of the document
2) is there a way in a macro to reference the value of a counter whose name is passed to the macro as an argument?

Thanks very much for any help!!

The code below includes the macro and illustrates the way I want to use it.

Code: Select all

\documentclass{amsart}%
\usepackage{ifthen}
\def\linkedLabels#1#2#3#4{\ifthenelse{#1=1}
    {\addtocounter{equation}{1}
     \setcounter{#2}{\theequation}}{}
     \label{eq:#2-#4}
     \tag{#3$\empty_\text{#4}$}}
\newcounter{tmpCounter}
\begin{document}
\begin{align}
a = b
\end{align}
\begin{align}
\linkedLabels{1}{tmpCounter}{\thetmpCounter}{sym}
a = b
\end{align}
\begin{align}
a = b
\end{align}
\begin{align}
\linkedLabels{2}{tmpCounter}{\thetmpCounter}{sb}
a = b
\end{align}
Referring to equations \eqref{eq:tmpCounter-sym} and \eqref{eq:tmpCounter-sb}, we find
\end{document}

Recommended reading 2024:

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

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

kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Definition of a Macro involving Counters

Post by kaiserkarl13 »

Perhaps I'm not answering your question, but does this do what you desire?

Code: Select all

\documentclass{amsart}%
\begin{document}
\begin{align}
  a = b
\end{align}
\refstepcounter{equation}%
\label{eq:tmpCounter}%
\begin{align}
  a = b \tag{\ref{eq:tmpCounter}$_\text{sym}$}
  \label{eq:tmpCounter-sym}
\end{align}
\begin{align}
  a = b
\end{align}
\begin{align}
  \stepcounter{equation}
  a = b \tag{\ref{eq:tmpCounter}$_\text{sb}$}
  \label{eq:tmpCounter-sb}
\end{align}
Referring to equations \eqref{eq:tmpCounter-sym} and \eqref{eq:tmpCounter-sb}, we find
\end{document}
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Definition of a Macro involving Counters

Post by cgnieder »

I'm not sure I understand exactly what you want the command to do. I assume the following:
  1. a switch which lets the command either use the next equation number or the one when the command was last used
  2. make a label with a custom name
  3. add a subscript to the equation number
Maybe you can clarify that?

If my assumption is right I would probably do something like this:

Code: Select all

\documentclass{amsart}

\makeatletter
\newif\if@nextequation
\@nextequationfalse

\newcounter{mylabel}

\newcommand*\mylabel{%
  \@ifstar
   {\@nextequationfalse\my@label@i}
   {\@nextequationtrue\my@label@i}}

\newcommand*\my@label@i[2]{%
  \label{eq:#1-#2}%
  \if@nextequation
   \stepcounter{equation}%
   \setcounter{mylabel}{\value{equation}}%
  \fi
  \tag{\arabic{mylabel}$_\text{#2}$}}
\makeatother

\begin{document}
\begin{align}
 a = b
\end{align}
\begin{align}
 a = b \mylabel{name}{sym}
\end{align}
\begin{align}
 a = b
\end{align}
\begin{align}
 a = b \mylabel*{name}{sb}
\end{align}
\begin{align}
a = b
\end{align}
Referring to equations \eqref{eq:name-sym} and \eqref{eq:name-sb}, we find
\end{document}
To answer your questions:
  1. counters can be defined in the document body without problems

    Code: Select all

    \documentclass{article}
    \begin{document}
    \newcounter{example}
    \setcounter{example}{3}
    \theexample
    \end{document}
  2. yes, there is:

    Code: Select all

    \documentclass{article}
    \newcounter{example}
    \setcounter{example}{3}
    \makeatletter
    \newcommand\showcounter[1]{%
      the counter \texttt{#1} has the value \@nameuse{the#1}}
    \makeatother
    \newcommand\ShowCounter[1]{%
      the counter \texttt{#1} has the value \arabic{#1}}
    \begin{document}
    \showcounter{example}; \ShowCounter{example}
    
    \setcounter{example}{7}
    \showcounter{example}; \ShowCounter{example}
    \end{document}
Regards
site moderator & package author
leo simon
Posts: 17
Joined: Wed Aug 19, 2009 5:41 pm

Definition of a Macro involving Counters

Post by leo simon »

Thanks to both of you for suggestions.
- \refstepcounter will be very useful, I've always wanted to be able to define a label outside of an environment. It would be nice if there were a command that did nothing but redefine the default counter used by \label. I.e., does exactly what \refstepcounter does but doesn't increment the argument of refstepcounter

- \@nameuse{the#2} solves the bigger of my problems!

- It turns out that what I need to do is to define a counter inside of a macro, as in

Code: Select all

\def\tmpCounter#1#2{
       \newcounter{#1}
       \setcounter{#1}{#2}
        etc
But this doesn't seem to work
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Definition of a Macro involving Counters

Post by cgnieder »

Although it's generally not a good idea to define a new counter inside a command it is possible.

And one could define a \refcounter which doesn't increment the counter but makes it available for \label.

Code: Select all

\documentclass{article}
\makeatletter
\newcommand*\refcounter[1]{%
  \protected@edef\@currentlabel{\csname p@#1\endcsname\csname the#1\endcsname}}
\makeatother
\begin{document}
\makeatletter
\newcommand*\tmpCounter[2]{%
  \@ifundefined{c@#1}
    {\newcounter{#1}%
     \setcounter{#1}{#2}}
    {\setcounter{#1}{#2}}}
\makeatother

\tmpCounter{test}{3}
\thetest % 3

\tmpCounter{test}{5}
\thetest % 5

\refcounter{test}\label{show_me}
\ref{show_me} % 5
\end{document}
site moderator & package author
Post Reply