Text FormattingOutput of a counter BEFORE counting

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
nikolapoljak
Posts: 9
Joined: Sun Feb 26, 2012 3:03 pm

Output of a counter BEFORE counting

Post by nikolapoljak »

Hi,

perhaps this is simple, but I just can't get my head around it... is there a way to output a final counter value before the counter actually starts? For example, an output like:

Item count:2

1. first item
2. second item

I am using MacTex with the XeLaTex processor on TexWorks.

Please help, it is sort of urgent. Thanks

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

nikolapoljak
Posts: 9
Joined: Sun Feb 26, 2012 3:03 pm

Re: Output of a counter BEFORE counting

Post by nikolapoljak »

Solved, thanks.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Re: Output of a counter BEFORE counting

Post by Stefan Kottwitz »

Hi,

welcome to the board!

If fellow readers would solve your problem, they would post the solution. Generally it would be good if you would post your solution also if you found it yourself. Imagine, google users would come here searching for their problems and find it discussed, but only a "Solved" without any solution... would be frustrating.

So if you would find the time later, it would be nice if you would add a solution. I guess it could be done by storing the final counter value in the .aux file.

Stefan
LaTeX.org admin
nikolapoljak
Posts: 9
Joined: Sun Feb 26, 2012 3:03 pm

Output of a counter BEFORE counting

Post by nikolapoljak »

So, I didn't solve it after all... I thought the package totcount would help me, but not really. Here is the problem:

If I write something like:

Code: Select all

\vskip 5mm
\newtotcounter{Nature}
\begin{enumerate}
\usecounter{Nature}
\item{Blablabla}
\end{enumerate}
then I can use \total{Nature} anywhere in the text and it turns out fine. BUT, all the items in the list start with a zero.

On the other hand, if I use

Code: Select all

\vskip 5mm
\begin{enumerate}
\newtotcounter{Nature}
\item{Blablabla}
\end{enumerate}
than the items are enumerated correctly (1,2,3...), but I can't seem to use \total{Nature} anywhere.

Any suggestions?

Btw. I am not using \usepackage{enumerate}, it is just the usual enumerate environment.
nikolapoljak
Posts: 9
Joined: Sun Feb 26, 2012 3:03 pm

Output of a counter BEFORE counting

Post by nikolapoljak »

OK, so here is the simple workaround:

use package totcount and do:

Code: Select all

\newtotcounter{Nature}
\begin{enumerate}
\addtocounter{Nature}{1}
\item{Blablabla}
\addtocounter{Nature}{1}
\item{Second item}
\end{enumerate}
and then you can use \total{Nature} anywhere in the text and still your enumeration will look fine. Hopefully it helps someone.
Last edited by Stefan Kottwitz on Sun Feb 26, 2012 8:25 pm, edited 1 time in total.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Output of a counter BEFORE counting

Post by Stefan Kottwitz »

Hi,

\stepcounter{Nature} (or \refstepcounter{Nature}) would be a bit easier to write.
Thanks for sharing your solution!

Stefan
LaTeX.org admin
hugovdberg
Posts: 133
Joined: Sat Feb 25, 2012 6:12 pm

Output of a counter BEFORE counting

Post by hugovdberg »

Wouldn't it be even nicer to instead of manually adding \addtocounter every item declare a macro, for example:

Code: Select all

\documentclass{amsart}

\usepackage{totcount}

\newcommand{\Item}[1]{\stepcounter{#1}
\item}
\begin{document}
\total{Nature}
\newtotcounter{Nature}
\begin{enumerate}
\Item{Nature}Blablabla
\Item{Nature}Second item
\end{enumerate}

\end{document}
Makes it a lot easier. If you use only one counter you could include the counter name in the macro of course, but then I'd rename the new command a bit to distinguish it better from the default \item.
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Output of a counter BEFORE counting

Post by Stefan Kottwitz »

You could set the total counter to be the most recent value of the enumerate counter enumi:

Code: Select all

\documentclass{article}
\usepackage{totcount}
\newtotcounter{Nature}
\begin{document}
The total number will be \total{Nature}.
\begin{enumerate}
  \item{First item}
  \item{Second item}
  \item{Third item}
  \item{Fourth item}
\end{enumerate}
\setcounter{Nature}{\value{enumi}}
\end{document}
total.png
total.png (7.35 KiB) Viewed 6102 times
Stefan
LaTeX.org admin
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Output of a counter BEFORE counting

Post by Stefan Kottwitz »

hugovdberg wrote:Wouldn't it be even nicer to instead of manually adding \addtocounter every item declare a macro

Good idea, which is flexible.

If each item of an environment would be counted, it would be good to define a new enumerate environment (with a different name) which works like enumerate but updates the total counter in the environment's end definition, as in my previous post.

Stefan
LaTeX.org admin
hugovdberg
Posts: 133
Joined: Sat Feb 25, 2012 6:12 pm

Output of a counter BEFORE counting

Post by hugovdberg »

Stefan_K wrote:
hugovdberg wrote:Wouldn't it be even nicer to instead of manually adding \addtocounter every item declare a macro

Good idea, which is flexible.

If each item of an environment would be counted, it would be good to define a new enumerate environment (with a different name) which works like enumerate but updates the total counter in the environment's end definition, as in my previous post.

Stefan
When defining a new environment you should probably use \addtocounter and not \setcounter in case you want to use two lists which total to the same counter. When you have only one list this makes no difference as the initial value is zero anyway, am I wrong?
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit
Post Reply