4

How I make the numbering in the following file start at 1.0 instead of 1.1? setcounter{dfnv}{-1} doesn't do anything.

\documentclass[12pt]{article}

\usepackage{amsthm}
\usepackage{amsmath}

\newcounter{dfnq}[section]
\newcounter{dfnv}

\makeatletter
\@addtoreset{dfnv}{dfnq}
\let\thedfnvsaved\thedfnv
\renewcommand{\thedfnv}{\thedfnq.\thedfnvsaved}
\makeatother

%% \makeatletter
%% \@namedef{thedfnv}{\@nameuse{thedfnq}.\arabic{dfnv}}
%% \makeatother

\newtheorem{dfn}[dfnv]{Definition}

\begin{document}

\section{First section}


\stepcounter{dfnq}
% Should display as Definition 1.0
% Actually displays as Definition 1.1
\begin{dfn}
  Version 0 of the definition of foo.
\end{dfn}

% Should display as Definition 1.1
% Actually displays as Definition 1.2
\begin{dfn}
  Version 1 of the definition of foo.
\end{dfn}
\stepcounter{dfnq}

% Should display as Definition 2.0
% Actually displays as Definition 2.1
\begin{dfn}
  Version 0 of the definition of bar.
\end{dfn}

% Should display as Definition 2.1
% Actually displays as Definition 2.2
\begin{dfn}
  Version 1 of the definition of bar.
\end{dfn}
\stepcounter{dfnq}

\end{document}

Full disclosure: this is a continuation of my previous question.

3 Answers3

2

All you need is to subtract 1 to the value of your counter when you use it (in \thedfnv). Here it is done using a temporary counter.

\documentclass[12pt]{article}

\usepackage{amsthm}
\usepackage{amsmath}

\newcounter{dfnq}[section]
\newcounter{dfnv}[dfnq]
\newcounter{tmpcnt}

\renewcommand{\thedfnv}{
\setcounter{tmpcnt}{\value{dfnv}}\addtocounter{tmpcnt}{-1}\thedfnq.\thetmpcnt}


\newtheorem{dfn}[dfnv]{Definition}

\begin{document}

\section{First section}


\stepcounter{dfnq}
% Should display as Definition 1.0
% Actually displays as Definition 1.1
\begin{dfn}
  Version 0 of the definition of foo.
\end{dfn}

% Should display as Definition 1.1
% Actually displays as Definition 1.2
\begin{dfn}
  Version 1 of the definition of foo.
\end{dfn}
\stepcounter{dfnq}

% Should display as Definition 2.0
% Actually displays as Definition 2.1
\begin{dfn}
  Version 0 of the definition of bar.
\end{dfn}

% Should display as Definition 2.1
% Actually displays as Definition 2.2
\begin{dfn}
  Version 1 of the definition of bar.
\end{dfn}
\stepcounter{dfnq}

\end{document}

EDIT: a way to get labels to work correctly

  • decouple dfnv form dfnq (not strictly necessary but we'll handle the dependency manually)
  • initialize dfnv to -1
  • define a command \nextdef that you will call instead of \stepcounter{dfnq} :

    \newcommand{\nextdef}{\stepcounter{dfnq}\setcounter{dfnv}{-1}}

  • redefine \thedfnv the good old way:

    \renewcommand{\thedfnv}{\thedfnq.\arabic{dfnv}}

And voilà, should work correctly.

\documentclass[12pt]{article}

\usepackage{amsthm}
\usepackage{amsmath}

\newcounter{dfnq}[section]
\newcounter{dfnv}
\setcounter{dfnv}{-1}

\renewcommand{\thedfnv}{\thedfnq.\arabic{dfnv}}

\def\nextdef{\stepcounter{dfnq}\setcounter{dfnv}{-1}}

\newtheorem{dfn}[dfnv]{Definition}

\begin{document}

\section{First section}


\stepcounter{dfnq}
% Should display as Definition 1.0
% Actually displays as Definition 1.1
\begin{dfn}\label{defa}
  Version 0 of the definition of foo.
\end{dfn}

% Should display as Definition 1.1
% Actually displays as Definition 1.2
\begin{dfn}\label{defb}
  Version 1 of the definition of foo.
\end{dfn}
\nextdef

% Should display as Definition 2.0
% Actually displays as Definition 2.1
\begin{dfn}\label{defc}
  Version 0 of the definition of bar.
\end{dfn}

% Should display as Definition 2.1
% Actually displays as Definition 2.2
\begin{dfn}\label{defd}
  Version 1 of the definition of bar.
\end{dfn}
\nextdef

\ref{defa}

\ref{defb}

\ref{defc}

\ref{defd}

\end{document}
0

Put \setcounter{dfnv}{\numexpr\value{dfnv}-1\relax} after every occurence of \stepcounter{dfnq}.

\documentclass[12pt]{article}

\usepackage{amsthm}
\usepackage{amsmath}

\newcounter{dfnq}[section]
\newcounter{dfnv}

\makeatletter
\@addtoreset{dfnv}{dfnq}
\let\thedfnvsaved\thedfnv
\renewcommand{\thedfnv}{\thedfnq.\thedfnvsaved}
\makeatother

%% \makeatletter
%% \@namedef{thedfnv}{\@nameuse{thedfnq}.\arabic{dfnv}}
%% \makeatother

\newtheorem{dfn}[dfnv]{Definition}

\begin{document}

\section{First section}


\stepcounter{dfnq}
\setcounter{dfnv}{\numexpr\value{dfnv}-1\relax}
% Should display as Definition 1.0
% Actually displays as Definition 1.1
\begin{dfn}
  Version 0 of the definition of foo.
\end{dfn}

% Should display as Definition 1.1
% Actually displays as Definition 1.2
\begin{dfn}
  Version 1 of the definition of foo.
\end{dfn}
\stepcounter{dfnq}
\setcounter{dfnv}{\numexpr\value{dfnv}-1\relax}
% Should display as Definition 2.0
% Actually displays as Definition 2.1
\begin{dfn}
  Version 0 of the definition of bar.
\end{dfn}

% Should display as Definition 2.1
% Actually displays as Definition 2.2
\begin{dfn}
  Version 1 of the definition of bar.
\end{dfn}
\stepcounter{dfnq}

\end{document}

enter image description here

Sebastiano
  • 54,118
Biki Teron
  • 3,275
-1

\setcounter{dfnq}{-1} and follows...

MadyYuvi
  • 13,693
  • This doesn't do the job, the dfnq counter has the appropriate values, it is the way the dfnv counter increments is reset when the dfnq counter increments that is the problem as detailed in existing answers. – Dai Bowen May 26 '17 at 13:18