Counting backwards

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

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


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

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
• You write -1, but in order to start from some number, you need to give n. – Alenanno May 12 '15 at 9:44
• 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

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}}
\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:

The next run shows the correct result:

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

\documentclass[12pt]{article}

\usepackage{totcount}

\newtotcounter{myCounter}
\newcounter{myDcounter}

\begin{document}

\setcounter{myDcounter}{\totvalue{myCounter}}

Up \themyCounter\ and down
\themyDcounter

Up \themyCounter\ and down
\themyDcounter

Up \themyCounter\ and down
\themyDcounter

Up \themyCounter\ and down
\themyDcounter

Up \themyCounter\ and down
\themyDcounter

Up \themyCounter\ and down
\themyDcounter

Up \themyCounter\ and down
\themyDcounter

\end{document}


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.

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