0

As I understand it, a counter is supposed to increment automatically. However, when I try to use one all I get is zeros. Here is a MWE.

\documentclass[12pt]{article}
\usepackage{lipsum}
\newcounter{mcounter}


\begin{document}
\themcounter \lipsum[1]

\themcounter \lipsum[2]

\themcounter \lipsum[3]
\end{document}

Printed lipsum text with zeros preceding each paragraph.

Truthfully, I'm trying to create a counter for pages in the middle of a list of teaching plans. The list looks something like "Day 1: Worksheets #1, #2, and Notes. Day 2: Worksheets #3, Notes, and Worksheet #4". If I add a worksheet (or take one away) I don't want to renumber everything in the list for that day and any subsequent days.

Any suggestion as to what I'm doing wrong? I'm using LauLaTeX on Texmaker on a Win 10 PC.

  • 6
    To increase a counter by a value of 1 you can use \stepcounter (or \refstepcounter if you want to use the \label \ref mechanism) – leandriis Aug 10 at 13:36
  • Could you please add some more information on wha exactly you want to achieve with your counter? Your example document looks like it could easily be recreated with an enumerate environment instead of using a self defined counter. – leandriis Aug 10 at 13:37
  • 1
    a counter is supposed to increment automatically; only if requested to. Your MWE only created a new counter, which starts in 0, but no commands increasing it anywhere. – Sigur Aug 10 at 13:49
  • 1
    \stepcounter{mcounter} for a simple increment by 1 unit. \refstepcounter{mcounter} if you want the next \label{name-of-label} command to use this counter value (i.e., store it in the .aux file associated to the label name-of-label so that it can be retrieved with \ref{name-of-label}, and the corresponding page number with \pageref{name-of-label}). – frougon Aug 10 at 14:06
  • 4
    You want the computer to read your mind, maybe? If it's repetitive, put it inside a macro (cf. \newcommand). – frougon Aug 10 at 14:15
4

You wrote,

As I understand it, a counter is supposed to increment automatically.

That's not correct. If you create a counter but never do anything to or with it except show its value, that value will remain at the initial value (generally, 0) throughout the document.

In LaTeX, a counter's value can be modified with the commands \setcounter, \addtocounter, \stepcounter, and \refstepcounter. The latter two increment the counter's value by 1, and they take one argument -- the name of the counter whose value should be incremented. \setcounter and \addtocounter take two arguments: the name of the counter and a whole number.

If you want to create a macro which increments the counter named mycounter by 1 and displays the newly-incremented value of mycounter, you can do so in several ways. E.g., after creating the counter with

\newcounter{mycounter}

you could use one of the following three definitions of \showmycounter:

\newcommand\showmycounter{\addtocounter{mycounter}{1}\themycounter}
\newcommand\showmycounter{\stepcounter{mycounter}\themycounter}
\newcommand\showmycounter{\refstepcounter{mycounter}\themycounter}

By default, the directives \themycounter and \arabic{mycounter} produce the same output, i.e., the value of a counter is shown, by default, as an arabic numeral. If you wanted to show the counter's value as a uppercase-Roman numeral, say, you'd either have to redefine \themycounter (via \renewcommand\themycounter{\Roman{mycounter}}) or change the above \newcommand instructions, e.g.,

\newcommand\showmycounter{\stepcounter{mycounter}\Roman{mycounter}}

The value of a counter can be any whole number, including 0 and negative whole numbers. If the value of mycounter is negative, representing its value via \roman{mycounter} and \Alph{mycounter} will generate an error message.


An MWE (minimum working example) that builds on these ideas:

enter image description here

\documentclass{article}
\newcounter{mycounter} % create a new counter, called 'mycounter'
% default def'n of '\themycounter' is '\arabic{mycounter}'

%% command to increment 'mycounter' by 1 and to display its value:
\newcommand\showmycounter{\stepcounter{mycounter}\themycounter}

\usepackage{lipsum}
\newcommand\showlips{\stepcounter{mycounter}\lipsum[\value{mycounter}]}

\begin{document}
\showmycounter, \showmycounter, \showmycounter

\showlips

% verifying that the preceding command used '4':
\lipsum[4]
\end{document}

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.