# [Tex/LaTex] increment a counter

counters

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}


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.

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 display its value (say, via \themcounter), the counter's value will remain at its initial value (generally, 0) throughout the document.

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

If you want to create a LaTeX macro which (a) increments the counter named mycounter by 1 and (b) 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., arabic numerals are used by default to show the value of a counter. If you wanted to show the counter's value as, say, an uppercase-Roman numeral, 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. Unsurprisingly, if the value of mycounter is non-positive, an attempt to represent its value as an alphabetic character or as a Roman numeral will generate an error message.

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

\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}
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.6389096975326538, "perplexity": 2433.2221563408234}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2023-06/segments/1674764500758.20/warc/CC-MAIN-20230208092053-20230208122053-00657.warc.gz"}