Number of equation with condition

Harald Hanche-Olsen harald.hanche-olsen at ntnu.no
Sun Mar 28 11:58:29 CEST 2021




Hello! I am working in a article documentclass. It have five section using the command \section. My intention is that each equation number showed a determined format depending on if the section number is odd or even.

I intended using the conditionals, before the \section, for example:
\ifodd \value{section} \renewcommand{\theequation}{\textbf{{\thesection}-\alph{equation}}} \else \renewcommand{\theequation}{\textit{{\thesection}-\arabic{equation}}}\fi

This might work. However there are two problems: (1) in the code you showed, you run this conditional only once, thus choosing the “even” branch of the conditional, since the section number is zero initially. This will then be in force in the entire document. You would have to run this code together with each \section command. (2) The \section command increments the counter, so if you run your code before \section, the sense of even vs odd will be flipped. Better run it after \section.

A better approach still would be to incorporate the test into \theeequation. Here is a sample document that hopefully does what you want:

\documentclass{article}

\renewcommand\theequation{%
  \ifodd\value{section}%
    \textbf{{\thesection}-\alph{equation}}%
  \else
    \textit{{\thesection}-\arabic{equation}}%
  \fi
}
\begin{document}
\begin{equation} \label{zero}
  a=b
\end{equation}
\section{one}
\begin{equation} \label{one}
  c=d
\end{equation}
\section{two}
\begin{equation} \label{two}
  e=f
\end{equation}
Reference to \ref{zero}, \ref{one}, \ref{two}.
\end{document}

– Harald

PS. When asking question like this, please do include a minimal, but complete file demonstrating your approach. By complete, I mean something that anybody can compile and which shows the problem you are trying to solve. You are much more likely to get help if you do.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://tug.org/pipermail/texhax/attachments/20210328/176ccea5/attachment.html>


More information about the texhax mailing list.