1

I have reformed a command from this answer. It auto-generates numbers for specific lines. Please see this code.

\documentclass{article}
\usepackage{lipsum}
\newcounter{mynumber}
\newcommand{\myno}[1][1]{\hfill\refstepcounter{mynumber}(\arabic{mynumber})\label{#1}}
\def\NewLLLLLast{\texttt{[\the\numexpr\value{mynumber}-4\relax]}\space}
\def\NewLLLLast{\texttt{[\the\numexpr\value{mynumber}-3\relax]}\space}
\def\NewLLLast{\texttt{[\the\numexpr\value{mynumber}-2\relax]}\space}
\def\NewLLast{\texttt{[\the\numexpr\value{mynumber}-1\relax]}\space}
\def\NewLast{\texttt{[\the\value{mynumber}]}\space}

\begin{document}
    \lipsum[1] \myno\\
    \lipsum[1] \myno[second]\\
    \lipsum[1] \myno\\
    \lipsum[1] \myno\\

    \NewLLLLast is the first paragraph.\\
    \NewLLLast is the second paragraph.\\
    \NewLLast is the third paragraph.\\
    \NewLast is the fourth paragraph.\\

    In \ref{second} paragraph bla bla bla.
\end{document}

Now I want a command which restarts this numbering from 1. Let's say the command is \restartnum. Whenever I put it, the next \myno should generate (1).

Niranjan
  • 3,435

1 Answers1

2

Use \setcounter{mynumber}{0}.

On the other hand, there are many improvements you can apply.

First, the code for appending the number at the right margin is not going to work if the text ends up near the margin. I added a working code (see https://tex.stackexchange.com/a/91564/4427): in case the last line is almost full, the number would be moved to the next line, still aligned to the right margin; with your code it would appear at the left margin.

Second, instead of defining a bunch of commands and counting L's, just use an argument.

\documentclass{article}
\usepackage{lipsum}

\newcounter{mynumber}
\newcommand{\myno}[1][]{%
  \begingroup
  \nobreak\hfill\penalty50\hskip1em\null\nobreak\hfill
  \refstepcounter{mynumber}(\arabic{mynumber})%
  \if\relax\detokenize{#1}\relax\else
    \label{#1}%
  \fi
  \parfillskip=0pt \finalhyphendemerits=0 \par
  \endgroup
}
\newcommand{\NewLast}[1]{%
  \texttt{[\the\numexpr\value{mynumber}-#1+1\relax]}%
}
\newcommand{\resetmyno}{\setcounter{mynumber}{0}}

\begin{document}

\lipsum[1][1-3] \myno

\lipsum[1][1-3] \myno[second]

\lipsum[1][1-3] \myno

\lipsum[1][1-3] \myno

\NewLast{4} is the first paragraph.

\NewLast{3} is the second paragraph.

\NewLast{2} is the third paragraph.

\NewLast{1} is the fourth paragraph.

In \ref{second} paragraph bla bla bla.

\resetmyno

\lipsum[1][1-3] \myno

\lipsum[1][1-3] \myno

\end{document}

enter image description here

egreg
  • 1,121,712