2

I do my homework in LaTeX, and I encountered a new problem. I usually don't number my equations. In this assignment, I would like to number at least one equation of mine that I will reference later. Unfortunately, I always copy the assignment problem statement at the top, and in this assignment, there were numbered equations. Rather than manually numbering equations (mine or the assignment's), I would like to define a new counter and \label - \ref setup to make two types of equation references and counters. Here's what I came up with, which gave me errors:

\newcounter{MyEqNum}
\newcommand{\MyNum}{\refstepcounter{MyEqNum}\rm{\Roman{MyEqNum}}}
%\newcommand{\RefMyEqn}{(\Roman{MyEqNum})}
%\renewcommand{\theequation}{\Roman{MyEqNum}}
\renewcommand{\theequation}{\refstepcounter{MyEqNum}\rm{\Roman{MyEqNum}}}
\begin{equation}
    \frac{\partial^4 U}{\partial x^4} = U^{(4)}(x) = \frac{\rho \omega_n^2}{Y I} U(x)
    \label{eq:My1}
\end{equation}

\begin{equation}
    test
    \label{eq:My2}
\end{equation}

The above result, \eqref{eq:My1} will be used later. Now using modal expansion, and writing the fourth spatial derivative as $\frac{\partial^4 U(x)}{\partial x^4} = U_n^{(4)}(x)$:

enter image description here

The errors I get are "No counter", "Extra \endcsname", "Missing \endcsname inserted", and a few undefined control sequences.

Note. that the original assignment used Arabic numbering, which is why I tried to make the second label counter numbering use Roman. I am sorta resigned to write \renewcommand{\theequation}{} every time I switch back and forth between numbering schemes, but if there is a better way, I would love to know. I would prefer not to define a new equation environment, but I figured that might be the right way to do this (I have no experience with defining new environments and am a bit afraid of that prospect).

-------------------------------------------------------------------------------

Edit: After implementing @Donald Arseneau's method, I made a slight change to get Roman numerals to work. It took me some trial and error, so I'm posting it here.

\makeatletter
\newcounter{MyEqNum}
\newenvironment{mynumbering}{%
  % replace equation numbering with my own counter and style
  %\let\theequation\theMyEqNum
  \renewcommand{\theequation}{\Roman{MyEqNum}}
  \let\c@equation\c@MyEqNum 
  \let\p@equation\p@MyEqNum
}{}
2
  • 1
    Surly you have to leave the regular equation numbering for the assignment's numbered equations. So you should define an alternate environment that uses "MyEqNum". You must not put "processing" like \refstepconter into \theequation. Apr 26, 2020 at 4:22
  • Yes, the setting \let\theequation\theMyEqNum presumes you have defined \theMyEqNum to be what you want (\Roman{MyEqNum}). Apr 28, 2020 at 8:33

2 Answers 2

2

Here is a document that defines a "mynumbering" environment to use around answers you have written with numbering separate from the assignment questions. It is feasible to make the counter swap in an alternative "equation" environment, but that would have to be repeated for all the numbered math environments.

The appearance of the numbering should be distinct, which should be done by defining \theequation and \theMyEqNum. I've shown defining \theequation to produce numbers like "Q1".

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\newcounter{MyEqNum}
\newenvironment{mynumbering}{%
  % replace equation numbering with my own counter and style
  \let\theequation\theMyEqNum
  \let\c@equation\c@MyEqNum 
  \let\p@equation\p@MyEqNum
}{}

% Number assignment equations with Q
\renewcommand\theequation{Q\arabic{equation}}

\begin{document}

The question is
\begin{equation}
  2b \lor \neg\, 2b \label{eq:ass1}
\end{equation}
\begin{mynumbering}
\begin{equation}
    \frac{\partial^4 U}{\partial x^4} = U^{(4)}(x) = \frac{\rho \omega_n^2}{Y I} U(x)
    \label{eq:My1}
\end{equation}

\begin{equation}
    test
    \label{eq:My2}
\end{equation}
\end{mynumbering}

The above result, \eqref{eq:My1} will be used later but not the test \eqref{eq:My2}, because the question \eqref{eq:ass1} is proven false.

\end{document}

In the mynumbering definition, \theequation and \theMyEqNum are cmacros that format the counter value; \c@equation / \c@MyEqNumare the actual counter (count register); and \p@equation / \p@MyEqNum are textual prefixes that would be used in cross referencing, but are normally blank.

Unfortunately, there is no encapsulated command for hyperref that can be switched, so this solution does not work with hyperref.

2
  • I edited my response with a variation on your solution that solved my problem. Thanks! Question: can you explain what c@equation and p@equation are, and the newenvironment code lines are doing? Apr 26, 2020 at 15:06
  • I've added some text at end of answer. Apr 28, 2020 at 8:47
1

You can define several styles and keep the numbering across them.

You can also restart a style with newnumbering* in case of need.

\documentclass{article}
\usepackage{amsmath}

\makeatletter % better use internal names

\newenvironment{newnumbering}[1][Roman]
 {
  \def\jr@counter{#1}%
  \jr@setup@numbering{#1}{1}%
 }
 {%
  \setcounter{jr@\jr@counter @equation}{\value{equation}}%
  \setcounter{equation}{\value{jr@equation@save}}%
  \ignorespacesafterend
 }
\newenvironment{newnumbering*}[1][Roman]
 {%
  \def\jr@counter{#1}%
  \jr@setup@numbering{#1}{0}%
 }
 {%
  \setcounter{jr@\jr@counter @equation}{\value{equation}}%
  \setcounter{equation}{\value{jr@equation@save}}%
  \ignorespacesafterend
 }

\newcounter{jr@equation@save}
\newcommand{\jr@setup@numbering}[2]{%
  % define a new counter if not yet done
  \@ifundefined{c@jr@#1@equation}{\newcounter{jr@#1@equation}}{}%
  % save the current equation number
  \setcounter{jr@equation@save}{\value{equation}}%
  \setcounter{equation}{%
    \ifnum#2>0
      \value{jr@#1@equation}%
    \else
      0%
    \fi
  }%
  \renewcommand{\theequation}{\csname#1\endcsname{equation}}%
}
\makeatother

\begin{document}

First a normal numbered equation
\begin{equation}\label{a}
0=0
\end{equation}
then another one
\begin{equation}\label{b}
0=0
\end{equation}

\begin{newnumbering}
An equation in Roman numbers
\begin{equation}\label{c}
1=1
\end{equation}
and another
\begin{equation}\label{d}
1=1
\end{equation}
\end{newnumbering}

Another normal equation
\begin{equation}\label{e}
2=2
\end{equation}

\begin{newnumbering}
Another with Roman numbers
\begin{equation}\label{f}
3=3
\end{equation}
\end{newnumbering}

\begin{newnumbering}[Alph]
Now we try a different one, alphabetic
\begin{equation}\label{g}
4=4
\end{equation}
\end{newnumbering}

\begin{newnumbering*}
This should restart the Roman numbering
\begin{equation}\label{h}
5=5
\end{equation}
\end{newnumbering*}

\eqref{a},
\eqref{b},
\eqref{c},
\eqref{d},
\eqref{e},
\eqref{f},
\eqref{g},
\eqref{h}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .