Counters in LaTex

By thurnherr

The following is a summary of counter usage and manipulations available in LaTex.

The following commands have a counter associated with it:

  • part
  • chapter
  • section
  • subsection
  • subsubsection
  • paragraph
  • subparagraph
  • page
  • equation
  • figure
  • table
  • footnote
  • mpfootnote
  • enumi
  • enumii
  • enumiii
  • enumiv

You have several possibilities to manipulate counters in LaTex:

  • \addtocounter {counter} {value} increments the specified counter by the value.
  • \newcounter {newcounter} [oldcounter] defines a new counter. Option ‘oldcounter’ is to link ‘newcounter’ to ‘oldcounter’.
  • \setcounter {counter} {value} sets the counter to have the specified value.
  • \usecounter{counter} used as the second argument of the list environment to number list items (see list command).
  • \value {counter} produces the value of the counter and is useful for doing arithmetic with counters.
  • \renewcommand {cmd} [args] [opt] {def} general command to define/redefine new commands.

Counter values can be printed in different formats such as:

  • \alph or \Alph {counter} for letters (a…z or A…Z) .
  • \arabic {counter} for arabic numbers (1…9).
  • \fnsymbol {counter} printing various symbols: asterisk, dagger, double dagger, section mark, paragraph mark, double vertical lines, double asterisk, double daggers, double double daggers. The value of counters must be between 1 and 9.
  • \roman or \Roman {counter} for roman numerals (I,II,III,IV,V,…,X,etc.)

Tags: , , , , , , , , , , ,

48 Responses to “Counters in LaTex”

  1. Affan Says:

    Do you know of a way of defining newcounter such that I can have a separate macro that prints out my own heading? eg.
    I want to use \creatTask{Develop a new Algorithm},
    to expand into \textbf{TASK 1 — Develop a new Algorithm}.

    Thank you.

  2. thurnherr Says:

    Hi!

    Try the code below, I think it solves your problem. I am using a newcommand “\createTask” which you call each time you want to create a new task. You can modify the new command to your needs, e.g. add the dash, etc.

    \documentclass{article}
    \newcounter{taskcounter}
    \begin{document}
    \newcommand{\createTask}[1]{
    \stepcounter{taskcounter}
    \bf{TASK \arabic{taskcounter} #1}
    }
    \section{My three tasks}
    \createTask{Your first task}
    \newline
    \createTask{Your second task}
    \newline
    \createTask{Your third task}
    \end{document}

  3. Felipe Says:

    I have a counter-related question. I’m writing the outline of a document and want the section headings to be called simply “Chapter 1″, “Chapter 2″, etc. However, I need to move things around (i.e. move entire sections up or down) which messes the numbering, and then I have to go a manually change all of the headings. Any suggestions on how can I reference the chapter number without having to use actual numbers?

  4. thurnherr Says:

    Hi Felipe,

    You can use labels to reference many things including all headings. Just place the label command (\label{label:name}) below the heading and use \ref{label:name} to access the number.

    Cheers,
    Tom

  5. David Says:

    Hi, first of all, great blog. Anyway, do you know if there is any command that gets the max value of a counter in a list? Thing is, I need to typeset a list where the label is normally:

    P 1 – This and that and that.
    P 2 – This and also another thing.

    However, if there is only one \item in the list, it should be typeset as:

    Paragraph – I’m alone in this list!!

    Therefore, when defining the \newenvironment, I will probably need to use, as a condition value for \ifthenelse, some command like \maxvalue{counter}, as in:

    \ifthenelse{\maxvalue{counter}=1}{Paragraph – }{P \arabic{counter} -}

    The question is, of course, is there such a command (maxvalue)? Is it something LaTeX can do?

    Thank you very much, I can send you the sources if you want.
    David

  6. thurnherr Says:

    Hi David,

    If I understood your problem correctly, I do not think you can do it the way you propose, as Latex runs through your code sequentially and therefore cannot know the final value of a counter beforehand. Hence there is nothing like \maxvalue{counter}.
    You would probably need to do it manually…

    Tom

  7. David Says:

    Thank you for your help, I was afraid of that, exactly. I guess I will need to figure out another automatic way of doing that (mixing latex/scripts probably).

  8. Ehsan Says:

    Hi,
    plz help me!!
    How can i link footnote counter to pabe counter?

    Thanks.

  9. thurnherr Says:

    Hi,

    You can use the Latex variable “\thepage” to get the current page as a number.
    Look at this post for how to customise header/footer:
    http://texblog.wordpress.com/2007/11/07/headerfooter-in-latex-with-fancyhdr/

    Hope this solves your problem. If not, let me know.

  10. Ehsan Says:

    Hi,
    Thanks,
    In the above lesson (maybe!) you write
    ————————————————————————–
    \newcounter {newcounter} [oldcounter] defines a new counter. Option ‘oldcounter’ is to link ‘newcounter’ to ‘oldcounter’.
    ————————————————————————-
    i want create such link (beetwen newcounter and oldcounter).
    It means that when the oldcounter is increased the newcounter is set to zero!!

    Thanks a lot.

  11. thurnherr Says:

    You can try to use \newcommand:
    \newcommand{\name}[3]{
    %arguments: newcounter, oldcounter, value
    \addtocounter{#1}{#3}
    \setcounter{#2}{0}
    }

    I am not sure whether this will work, you have to try it yourself.
    Good luck:).

  12. David Says:

    Hi again! I almost managed to do what I wanted, using \label and \ref to store a given value in the aux file. Here is what I did:

    %%%%%%%%%%%%%%%%%%%%%%%%
    \newcounter{listnum}

    \makeatletter
    \newcommand\firstitem{\@item[ImAlone-]}
    \makeatother

    \newenvironment{myenumerate}{
    \refstepcounter{listnum}
    \begin{enumerate}
    \ifthenelse{\equal{\ref{enum:\arabic{listnum}}}{1}}
    {
    \let\item\firstitem
    \refstepcounter{enumi}
    }{
    \refstepcounter{enumi}
    }
    }{
    \label{enum:\arabic{listnum}}
    \end{enumerate}
    }
    %%%%%%%%%%%%%%%%%%%%%

    This is: for each myenumerate env in the text, there will be a correspondent enum:(number) label, and at the end of the processing, those counters will hold, as value, the total number of items in the environment, plus 1.

    With 3 myenumerate ’s, each with 1,4 and 1 numberof \item ’s, respectively, I get a clean compilation, but:

    2.

    2.
    3.
    4.
    5.

    2.

    So, I’m very close, but since I’m a total newbie in LaTeX, there is something simple I can’t realize. Could you help me? Maybe try it?

    Thanks a lot.

  13. Alex Says:

    How to change numbering of figures in a standard Latex article style from Figure 1: to Figure 1.
    (i.e., colon to dot after numbers)

  14. thurnherr Says:

    Hi Alex,

    You can actually customise your caption completely.
    To answer your question, you need the package “caption2″:
    \usepackage{caption2}
    Then you renew the “caption label delimiter” through:
    \renewcommand{\captionlabeldelim}{.}
    If you want to have the colon back for whatever reason, just use the command again inside your document with a colon instead of a dot.

    This should solve your problem.

    Cheers,
    Tom

  15. Florian Says:

    Hi,
    I am writing a document with many constants that I want to write $C_i$ with $i=1,2,…$, and such that I can make a reference to a specific constant later. So I tried to define a counter \cte, a command \newcte{label} that prints $C_\thecte$ and labels it with some label, and then another function \usecte{label} that would print the corresponding constant.

    I tried:

    \newcounter{cte}
    \newcommand{\newcte}[1]{
    \refstepcounter{cte} \label{cte:#1} C_{\thecte}
    }
    \newcommand{\usecte}[1]{
    C_{\ref{cte:#1}}
    }

    But I don’t understand why this does not work. Moreover, I have an annoying problem: when I want to use this syntax in an align environment with a label, I get a multiple labels error…

    Thanks in advance if anyone knows how to make this work (the way I tried or any other way!).

  16. thurnherr Says:

    Hi Florian,

    What you did works perfectly, just that you forgot the dollar signs ($), even though you had them in the text. Here is the corrected code:
    \newcounter{cte}
    \newcommand{\newcte}[1]{
    \refstepcounter{cte} \label{cte:#1} $C_{\thecte}$
    }
    \newcommand{\usecte}[1]{
    $C_{\ref{cte:#1}}$
    }

    And now you can print what you wanted:
    \newcte{bla} -> C_1
    \ref{cte:bla} -> 1

  17. Troy C. Says:

    Hi,

    I’m using “book” as the document class. And I want to link chapters to parts, so whenever I create a new part, the chapter counter is reset to 0. The easiest way to do this would be to just add following code in the preamble:

    \newcounter{chapter}[part]

    Right? But LaTeX always complains that the counter “chapter” has already been defined.

    How do I do this efficiently, without having LaTeX complain? Please help. Thanks in advance.

  18. thurnherr Says:

    Hi Troy!

    The counter is indeed already defined. What you have to do is reset it to zero.
    The following line after each “part-command” will do the trick:

    \setcounter{chapter}{0}

    Cheers,
    Tom

  19. Troy C. Says:

    Hi Thurnherr,

    Thanks for your tip. I tried that, and at the first look, it’s the solution I was looking for. But only at the first look. See, I use PDFTeX. And setting the “chapter” counter to zero whenever I call “part” does the trick visually. This solution would work fine as a print. But my goal is PDF.

    The problem is, let’s say I have two parts in my book, with various chapters in each part. Now, in the table of contents in the PDF, if I click on the second chapter of part II, the link doesn’t take me to the second chapter of part II. Instead, it takes me to the second chapter of part I. So internally, setting the “chapter” counter to zero every time part is called up messes with PDFTeX’s linking system.

    Is there any way to avoid that? There has to be an elegant solution to this problem without having to program new \chapter or \part, because I want to be able to use my .tex files in other projects as well without having to change anything.

    Thanks again. Cheers,
    Troy

  20. thurnherr Says:

    Hi Troy,

    I see your point. The books I have seen usually have consecutive chapter numbering even over different parts. I couldn’t find a solution to your problem, sorry!

    Cheers,
    Tom.

  21. Florian Says:

    Hi,
    thank you for your answer. unfortunately, what does not work is that I cannot use this code inside an environment \begin{align} with a label, because then there is a conflict on the labels…
    i have found a trick by using the package smartref, but if anybody has a better solution, I am interested!

  22. venus Says:

    hello

    I want for my math formula number, first have equation number then have chapter number, e.g.(34-2)
    34 is equation number and 2 is chapter number

    how can I do this?please guide me

  23. thurnherr Says:

    @venus

    In Latex, you can always access the counter of a specific environment using \theenvironment, e.g. the equation-environment counter is accessed through “\theequation”.
    Your problem can be solved by redefining the equation counter as follows:

    \renewcommand\theequation{\arabic{equation}-\thechapter}

    Cheers,
    Tom

  24. Erick Says:

    Hi all,

    I have the appropriate “example” environment, and its respective ExampleCounter.

    I want to be able to continue an “example”, as in:

    Example a: xyz
    Example b: xyz
    Example c: xyz
    Example b (continued): xyz
    Example d: xyz

    My question is:
    How do you:
    (1) set ExampleCounter = value of ExampleCounter of the (say the) second example (ie. Example b:)
    (2) Use the counter to create the example: “Example b (continued):” (i.e. \begin{example}[continued] xyz \end{example}
    (3) set ExampleCounter = value it had before step (1) —so that I can continue with the correct example labels…

    Thanks in advance,
    Erick

  25. thurnherr Says:

    Hi Erick,

    I couldn’t find a nice answer to your questions (1) and (3).
    Concerning question 2, a possible way to do it is the following:

    \newcounter{examplecounter}
    \newenvironment{example}[1]{
    \addtocounter{examplecounter}{1}
    Example \theexamplecounter#1:}{\\}

    and then

    \begin{example}{}
    xyz
    \end{example}
    \begin{example}{ (continued)}
    xyz
    \end{example}

    The disadvantage is, you always have to have the optional argument brackets when using the environment.

    For (1) & (3), you can simply add and subtract from your counter:

    \begin{example}{}xyz\end{example}
    \begin{example}{}xyz\end{example}
    \begin{example}{}xyz\end{example}
    \addtocounter{examplecounter}{-2}
    \begin{example}{ (continued)}xyz\end{example}
    \addtocounter{examplecounter}{1}
    \begin{example}{}xyz\end{example}

    This is obviously not dynamic, but I couldn’t think of a better solution for now, sorry.

    Cheers,
    Tom

  26. venus Says:

    hello thnks for your worth help

  27. Erick Says:

    First of all I wanted to thank Tom for his response. I also wanted to post a possible solution to my questions 1 and 3 that is somewhat more dynamic (although definitively not elegant).

    %First we need to save counter before the example that we wish to %continue later (say save its value in the newcounter ‘ex’)
    \newcounter{ex} \setcounter{ex}{\value{examplecounter}}

    %When where we want to continue the example we can load the
    %saved counter (we need to save the current counter value in the %newcounter temp —so that we can restore it later)
    \newcounter{temp}
    \setcounter{temp}{\value{examplecounter}}
    \setcounter{examplecounter}{\value{ex}}
    %We continue the example (as Tom showed)
    \begin{example}{ (continued)}xyz\end{example}
    %Finally we restore the counter value
    \setcounter{examplecounter}{\value{temp}}

    Erick

  28. pauld Says:

    hi all

    I’m looking to define a new sort of section to my document where i store all my figures. It should be something like when you do \appendices your chapter titles change from Chapter 1,2,3 to Appendix A,B,C
    I want this new section at the very end (after appendices, bibliography, etc), and when i type \figures for instance, chapter titles should change to Figures Chapter 1,2,3
    What do you think is the best solution for this? And do the figurecounters automatically change too? (so they look like Figure 1.12, 2.34, etc)

  29. dcheslow Says:

    I need to print a document in batches of pages. I’d LIKE to do this by creating separate documents for each batch of pages (the batches have different layouts and are printed on different paper so it makes some sense to separate them).

    In order to do this, I need to set the starting page of each document. I haven’t had much luck modifying \thepage… it seems to be defined as a macro command somewhere deep in the bowels of TeX.

    Any/all help and suggestions are welcome,

    thanks.

  30. dcheslow Says:

    Sorry… my bad… I found the (very simple) answer moments after posting:

    \setcounter{page}{9}

    Thanks anyway,

  31. venus Says:

    i have a question. how can i start footnote number, from 1 in each page, as u know its continuous for each chapter

  32. venus Says:

    hello
    please help me who knows it, as you familiar with latex footnotes numbers

    starts from 1 at the beginning of each chapter, but I want for each page

    footnotes number starts from 1, how do it?

    thank you

  33. venus Says:

    hi
    how can I clear the page number of the first page of each chapter, and second page of chapter start from 2 and continue?

    thanx

  34. Victor Says:

    I am writing a book where I would like to number the examples, and have them appear in a “listofexamples”. I would like the counter to be chpater name dash example number, also, I would like to be able to reference an example via \label \ref. How can I achieve this.

    Thanks

  35. thurnherr Says:

    Hi Victor,

    Thanks for your very interesting question. As this might be interesting to others, I decided to write a new post on that topic.

    http://houseofinteraction.com/texblog/?p=56

    I hope this is more or less what you were looking for. If you have any further question, don’t hesitate to contact me.

    Best regards,
    Tom

  36. Victor Says:

    Great, I used:
    \newcommand{\listexamples}{List of Examples}
    \newlistof{examples}{exp}{\listofexamples}
    \newcommand{\example}[1]{%
    \refstepcounter{examples}
    \vspace*{0.2in}\par\noindent\sqrblt\textbf{Example \theexamples. #1}
    \addcontentsline{exp}{examples}
    {\protect\numberline{\theexamples}#1}\par}

    however…
    1. If I set example numbers to include chapter, than in the table of contents the example number overlaps with the text (i.e. the numwidth is not properly set).

    2. Also, I would like to have an extra space in between the examples from various chapter.

    Thanks

  37. application Says:

    Hi! thanks for the great blog! I have a question. I have document with Chapters 1, 2, 3, and sub sections 1.1 1.2…. 2.1, 2.2 … so on.. How do I set a counter for a figure X that belongs to Chapter Y and subsection Z, so that its number will appear as Figure Y.Z.X ?

  38. application Says:

    … and basically same question for tables…

  39. thurnherr Says:

    Hi!

    What you would need to do is the following.
    Define a new counter:
    \newcouter{myfigure}
    and tell Latex how you want your figure counter to look like:
    \renewcommand{\thefigure}{\addtocounter{myfigure}{1}\thesection.\themyfigure}%

    If you want Latex to reset the counter everytime you start a new chapter you need to add this piece of code:
    \makeatletter
    \@addtoreset{myfigure}{chapter}
    \makeatother

    Hope this is what you were looking for,
    Tom

  40. application Says:

    Thanks a lot!

  41. 1sy8 Says:

    Tom,

    Your last tip can solve Troy C.’ s problem :

    To change chapter number each time changing part, add in your preamble
    \makeatletter
    \@addtoreset{chapter}{part}
    \makeatother

  42. chstef Says:

    Hi,

    I am writing a book that contains a certain number of tables and figures.

    I would like to include the total number of figures and the total number of tables in the front page.

    I was wondering if there is way to do so automatically.

    Thanks in advance.

  43. thurnherr Says:

    Hi,

    Thanks for your question, which was rather tricky to answer :-) . The problem is, that you first have to run through the document in order to know the number of figures.
    I propose you write a macro with the code given below, so you don’t mess up your tex-file:

    1. Open a new text-file and call numberfigure.sty.
    2. Copy-paste the code below into the file and save it.
    3. Add the numberfigure package to your Latex-document.
    4. Use \pageref{NumberFigures} to print the number of figures.

    I have to admit, the solution is not very nice and I would be happy if someone who is familiar with macros would post a better solution.

    Here is the macro code for numberfigure.sty:

    \newcounter{myfigure}
    \setcounter{myfigure}{0}
    \newcommand{\mycaption}[1]{
    \stepcounter{myfigure}
    \caption{#1}}

    \def\numberfigures@putlabel{\addtocounter{myfigure}{0}%
    \immediate\write\@auxout{\string
    \newlabel{NumberFigures}{{}{\themyfigure}}}%
    \addtocounter{myfigure}{1}}
    \AtEndDocument{%
    \clearpage\numberfigures@putlabel}%
    \endinput

    Cheers,
    Tom

  44. chstef Says:

    It worked!!! Not only for the tables and figures, but also for a user-defined floating environment as well. Thanks a lot for your prompt help.

  45. Tip: Showing page 4 of 65 « Blog on Latex Matters Says:

    [...] I recently posted a comment on how to count the number of figures within your document. View [...]

  46. emurray Says:

    Hi,

    I’m a little stuck with list numbering. I’m creating a list of assumptions in a paper. However, I would like the assumption counter to reset ONLY when the chapter number changes. In other words, I’d like the text to read:

    Chapter 1, Section 1
    ~
    Assumption 1.1
    Assumption 1.2
    ~
    Chapter 1, Section 2
    ~
    Assumption 1.3
    Assumption 1.4
    ~
    Chapter 2, Section 1
    ~
    Assumption 2.1
    ~

    Currently though, the assumption counter resets every time I call the assumption environment. Here’s my code for the environment:

    \newcounter{assump}
    \newenvironment{assumption}
    {\begin{list}{Assumption \arabic{chapter}.\arabic{assump}:}
    {\usecounter{assump}}}
    {\end{list}}

    I’ve tried adding “[chapter]” to the end of the counter definition, but it still doesn’t ensure that the counter is reset only when the chapter changes. Any ideas? Thanks!

  47. emurray Says:

    Ah, nevermind, I figured out a way around it. I added a new counter using

    \newcounter{assumpt}[chapter]

    which keeps the total of the assumptions in a chapter. Then I changed the last two lines of the above code to:

    {\usecounter{assump}}\addtocounter{assump}{\value{assumpt}}}
    {\end{list}\setcounter{assumpt}{\value{assump}}}

    Thanks though!

  48. chstef Says:

    Hi again,

    I have finished the book I was writing (Programming in MATLAB). The book is written in greek. Now, I am trying to generate a multilingual index (greek + english) using makeindex package without success. Can you give me any suggestion on which package do I have to use?

    Thanks in advance.

Leave a Reply