Document ClassesCreating custom counters by example

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
untexnixal
Posts: 3
Joined: Sun Feb 01, 2009 2:12 am

Creating custom counters by example

Post by untexnixal »

Since I don't have any access to good documentation, I'm trying to learn how to write a LaTeX class by googling for class and style files. However, I can't seem to produce automatic numbers using any combination of \newcounter \setcounter or \stepcounter.

So could somebody provide me with the shortest code fragment that I could paste in my class file (which patches the article class via LoadClass) to produce the following output:

Let's count 1 and 2 and 3!

(with the numbers 1, 2 and 3 being produced via the repeated invocation of a counter command called \foo).

The best I output I could come up with is:

Let's count 1 and 1 and 1!

(with the number 1 being produced by \setcounter or \stepcounter).

Thanks!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Keta
Posts: 63
Joined: Tue Nov 25, 2008 1:00 pm

Creating custom counters by example

Post by Keta »

First, you have to define a new counter, say, bar. Then, define a new command \foo which i) adds one to the counter, via \stepcounter, ii) prints the counter. To print the counter, you have to remember that each counter has an associated \thecounter which prints the value.

Here's a simple code doing that:

Code: Select all

\documentclass{article}

\newcounter{bar}
\newcommand{\foo}{%
	\stepcounter{bar}%
	\thebar}

\begin{document}

Let's count \foo\ and \foo\ and \foo\

\end{document}
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Creating custom counters by example

Post by localghost »

untexnixal wrote:Since I don't have any access to good documentation, I'm trying to learn how to write a LaTeX class by googling for class and style files. […]
For the present you can start with lshort as a general beginners guide and the clsguide as a manual for writing classes.


Best regards and welcome to the board
Thorsten¹
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
untexnixal
Posts: 3
Joined: Sun Feb 01, 2009 2:12 am

Re: Creating custom counters by example

Post by untexnixal »

@keta Thanks. I was a bit confused about the "the"(foo) commands.

@localhost Thanks. The references as you said seem quite basic, though they clarify a lot of general points such as the meaning of all those @'s. I had been hoping to find something that would walk me thru such details as changing character mapping.
Post Reply