Text FormattingNew Command with Counter

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
JonTsu
Posts: 9
Joined: Sun Jan 08, 2012 3:54 pm

New Command with Counter

Post by JonTsu »

Hi LaTeXers,

I was wondering if anyone could help me with the following:

I'm typing up lecture notes and I like to be able to tell where lectures begin. I've been typing \marginpar{\fbox{Lecture 1}}, \marginpar{\fbox{Lecture 2}}, etc.. where I've wanted to show the start of a new lecture. But what I'd really like is to set up a new command with a counter so that I can just type \lect throughout my .tex file and have the lecture # increased by one each time.

Any help would be greatly appreciated.

Thanks,
Jon :)
Last edited by cgnieder on Sat Oct 13, 2012 6:26 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

New Command with Counter

Post by cgnieder »

This is relatively easy: you need \newcounter{countername} to define a new counter. Then you need \stepcounter{countername} to increase it by one. At last you need \newcommand to define the new command. So if you add the following to your preamble \lect should do what you want:

Code: Select all

\newcounter{lecture}
\newcommand*\lect{%
  \stepcounter{lecture}%
  \marginpar{\fbox{Lecture \thelecture}}}
Regards
site moderator & package author
JonTsu
Posts: 9
Joined: Sun Jan 08, 2012 3:54 pm

Re: New Command with Counter

Post by JonTsu »

Thank you, cgnieder, that's perfect! :D
Jon
Post Reply