2

I would like to achieve this: Every time I write, say, \mypair, it should output, say, #Pair 1, then on the next use #Pair 2, etc.

(1) How do I do that? (2) How do I add the option to reset or resume the counter?

3

This defines commands \resetmycounter, \resumemycounter, and \mypair. When you use \resetmycounter, it remembers the value and starts over with a new counter. When you use \resumemycounter it forgets the current value and goes back to the previous value.

\documentclass{article}
\newcounter{mycountnum}
\newcounter{mycount}

\newcommand\resetmycounter{
    \stepcounter{mycountnum}
    \ifcsname themycount\roman{mycountnum}\endcsname \else
        \newcounter{mycount\roman{mycountnum}}
    \fi
}
\newcommand\resumemycounter{\addtocounter{mycountnum}{-1}}
\newcommand\mypair{%
    \stepcounter{mycount\roman{mycountnum}}
    \#Pair \csname themycount\roman{mycountnum}\endcsname
}


\begin{document}
\mypair

\mypair
\mypair
\mypair

\mypair

Reset:

\resetmycounter

\mypair
\mypair
\mypair

Reset:

\resetmycounter
\mypair

Resume:
\resumemycounter
\mypair

Resume:
\resumemycounter

\mypair
\mypair

Reset:
\resetmycounter
\mypair
\end{document} 

enter image description here

2

As you have guessed you need a counter. When you define a counter, say \newcounter{Test} there is also a command \theTest defined that will print the number. If you like you can redefine this to print it some other way, but without knowing a bit more of how it is supposed to be used, I would not recommend to put the #Pair here. Anyway you need to increment the counter before using it, which done using \stepcounter or \refstepcounter. So define a command that increments and prints the counter, like \myTest in the example below.

To reset the counter you can do \setcounter{Test}{0}, which is the manual form. You can also use the optional parameter to \newcounter to reset it with some other counter, e.g. section.

You can find more about counters on https://en.wikibooks.org/wiki/LaTeX/Counters

\documentclass{article}
\newcounter{Test}[section]
\newcommand\myTest{\stepcounter{Test}\#Pair \theTest}
\begin{document}
\section{First section}
\myTest\ \myTest
\setcounter{Test}{0}
\myTest\ \myTest

\section{New section}
\myTest\ \myTest\ \myTest

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.