Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Following up this question here, I am looking for a way to compute a simple maths calculation inside the document and let the result of this calculation be the value that appears as page number in the table of content. The following code snippet creates an entry in the table of contents:

\addtocontents{toc}{%
\protect\contentsline {section}{%
\protect\numberline {\thesection}#1}{xxxxxxxxxx}}}

Where you find xxxxxxxxxx (= the pagenumber) in this code snippet, there should be something like \thepage minus 1. This result will then be shown as page number in the table of contents.

What did not work was to insert \theprevpage as xxxxxxxxxx and to let

\newcounter{prevpage}
\setcounter{prevpage}{\value{page}}\addtocounter{prevpage}{-1}

precede the above snippet. (Usually this, of course, works and is a very fine solution. But In my case of using ledpar the way I do it doesn't work.) So it seems that in my particular case it is not possible to "outsource" the computation.

So my question now is: Can't I just somehow replace xxxxxxxxxx by something like \resultOf{\thepage\minus{1}} in order to compute the calculation directly inside the brackets of the \addtocontents-argument, so that the seulting number is written in the *.toc-file? (I am looking for the easiest possible way... or any other method the get the "pagenumber-minus-one" in the brackets without typing it manually.)

share|improve this question
add comment

1 Answer

The toughest part is to expand the number; this is a job for \number; in order to do an expandable computation we can use \numexpr

\addtocontents{toc}{%
  \protect\contentsline{section}
    {\protect\numberline{\thesection}#1}
    {\number\numexpr\value{page}-1\relax}}}

It might be done also without \number, but it would be unnecessarily complicated.

share|improve this answer
    
thanks for your solution. Sadly, it brings the same wrong result. Of course the maths is right, but it seems to make a difference whether (a) \thepage or (b) \number\value{page} is used. (Does this make any sense?) (a) results in 3 and (b) in 1. Taking 1 off means that (a') would be 2 (correct) and (b') is 0 (wrong). So, can I use \thepage in a computation somehow to get the correct result 2 from \thepage minus 1? –  ClintEastwood May 14 '12 at 18:12
    
My test gives the right result. –  egreg May 14 '12 at 19:46
    
of course it does - but not in my environment. This is why I am looking for a solution. Within my environment prompting \thepage results in the correct number x but \value{page} gives at some places x-1 and at other places x-2. With your solution in my environment I get for a computation of pagenumber minus 1 the follwoing results: page 11 minus 1 = 8 and later in the document page 21 minus 1 = 19 although the first should be 10 and the second 20. This seems to be due to the fact that at these places \value{page} is not the same as thepage. I know, it's weird. sorry. –  ClintEastwood May 14 '12 at 20:18
    
@ClintEastwood You should try and provide a minimal example. –  egreg May 14 '12 at 20:23
1  
@ClintEastwood The arguments to \addcontents are expanded during the output routine, when page has already been incremented. I understand that ledmac might do bad jokes with the OR, that's why an example is needed: too many factors may be involved. Sorry. –  egreg May 14 '12 at 20:35
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.