8

The following produces a counter that counts in increasing arabic numerals.

\documentclass[12pt]{article}
\pagestyle{empty}
\begin{document}
  \newcounter{myCounter}
  \addtocounter{myCounter}{1}
  \themyCounter
  \addtocounter{myCounter}{1}
  \themyCounter
  \addtocounter{myCounter}{1}
  \themyCounter
\end{document}

enter image description here

As noted in the comments, one can have LaTeX count backwards with \addtocounter{myCounter}{-1}

enter image description here

but this has the disadvantage that you need to manually specify the starting value, n. That seems like a nuisance because if you want to add some additional items to the list then you also have to remember to update n.

Is there a way to have a custom latex counter count backwards without having to manually specify the value of n?

  • \addtocounter{myCounter}{-1}? Wait, i don't understand the question. You have to give LaTeX some point to start with. – Johannes_B May 12 '15 at 9:42
  • 1
    You write -1, but in order to start from some number, you need to give n. – Alenanno May 12 '15 at 9:44
  • 1
    Do you know the etaremune package? Maybe it alread solves your problem. There are several threads on the site about it. – egreg May 12 '15 at 10:39
7

The following example lets increment the counter myCounter the usual way starting with 0, 1, 2. The latest value of the counter is stored in a zref label LastPage after the last page is shipped out. Then the appearance of the counter, \themyCounter is redefined to show the desired value by subtracting the actual value from the latest value:

\documentclass{article}
\pagestyle{empty}
\usepackage{zref-lastpage}
\newcounter{myCounter}

\makeatletter
\zref@newprop{myCounter}{\the\value{myCounter}}
\zref@addprop{LastPage}{myCounter}
\newcommand*{\myCounterLast}{%
  \zref@extractdefault{LastPage}{myCounter}{0}%
}
\renewcommand*{\themyCounter}{%
  \@arabic{\numexpr\myCounterLast+1-\value{myCounter}\relax}%
}
\makeatother

\begin{document}
\stepcounter{myCounter}
\themyCounter
\stepcounter{myCounter}
\themyCounter
\stepcounter{myCounter}
\themyCounter 
\end{document}

In the first run, the label is written but not yet known, the latest value is assumed to be zero:

First run

The next run shows the correct result:

Second run

5

You can use the totcount package to grab the final value of a counter and use that to set your starting value.

Sample output

\documentclass[12pt]{article}

\usepackage{totcount}

\newtotcounter{myCounter}
\newcounter{myDcounter}

\begin{document}

\setcounter{myDcounter}{\totvalue{myCounter}}
\addtocounter{myDcounter}{1}

\addtocounter{myCounter}{1}
\addtocounter{myDcounter}{-1}
Up \themyCounter\ and down
\themyDcounter

\addtocounter{myCounter}{1}
\addtocounter{myDcounter}{-1}
Up \themyCounter\ and down
\themyDcounter

\addtocounter{myCounter}{1}
\addtocounter{myDcounter}{-1}
Up \themyCounter\ and down
\themyDcounter

\addtocounter{myCounter}{1}
\addtocounter{myDcounter}{-1}
Up \themyCounter\ and down
\themyDcounter

\addtocounter{myCounter}{1}
\addtocounter{myDcounter}{-1}
Up \themyCounter\ and down
\themyDcounter

\addtocounter{myCounter}{1}
\addtocounter{myDcounter}{-1}
Up \themyCounter\ and down
\themyDcounter

\addtocounter{myCounter}{1}
\addtocounter{myDcounter}{-1}
Up \themyCounter\ and down
\themyDcounter

\end{document}
1

I'm afraid I don't understand the stipulation that one doesn't know the starting value of the counter. At any rate, here's a LuaLaTeX-based solution that counts down from n (where n is set to 10) to 1 in steps of -1.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\begin{document}
\directlua{ for i = 10, 1, -1 do tex.sprint(i.." ") end }
\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.