2

My code:

\documentclass[a4paper,12pt]{scrartcl}
\begin{document}

\newcount\tmpc
\def\modul#1#2{\tmpc=#1 \divide\tmpc by #2 \multiply \tmpc by #2 \multiply \tmpc by -1 \advance \tmpc by #1\relax \the\tmpc}
\def\addpages{%
\newcount\tmpb%
\tmpb=\modul{\thepage}{4}%
\multiply\tmpb by -1%
\advance\tmpb by 4%
\loop\ifnum\tmpb>0\advance\tmpb by-1 \vspace*{-5cm}\strut\newpage\repeat%
}
hello
\section[123\hbox to \textwidth{} {\rm\small 456}]{test}
\tableofcontents
\addpages
buggy
\addpages
test
\end{document}

I see "=1 1" on first page. Why?

1 Answers1

3

The explanation is simple. Let's examine the call

\tmpb=\modul{\thepage}{4}

After =, TeX performs expansion in order to find a <number>, so it replaces \modul with its replacement text and we get

\tmpb=\tmpc=\thepage \divide\tmpc by 4 \multiply \tmpc by 4 \multiply \tmpc by -1 \advance \tmpc by \thepage\relax \the\tmpc

Can you see the problem? The <number> is there, but it's \tmpc. So TeX assigns the current value of \tmpc to \tmpb and moves on. Now it finds =\thepage and it typesets them, as it's not doing an assignment any more. Only after this it performs the following assignments.

You probably want to look at Is a page modulo 4? in order to solve your problem.

Unrelated, but important: never do \newcount in the replacement text of a macro (unless it's a macro specifically designed to allocate a new counter, but to be used just once) or a new counter register will be wasted at each call of the macro.

egreg
  • 1,121,712
  • It's difficult to understand for me. I'm newbe in TeX. Thank you for a link. – Tarwirdur Turon Jan 28 '15 at 17:08
  • @TarwirdurTuron The main thing is that TeX doesn't feed the result of the computation with \modul{\thepage}{4} to \tmpb, because \modul doesn't simply expand to a number, but it is the set of instructions for producing one. – egreg Jan 28 '15 at 17:14
  • And how to expand it to number? – Tarwirdur Turon Jan 28 '15 at 17:31
  • might not there also be a problem with the % immediately after the 1 and 4 in the code? – barbara beeton Jan 28 '15 at 17:50
  • @barbarabeeton Not in this case, although the 4 will trigger the expansion of \loop that starts with \def, so the assignment is performed correctly. But yes, it's better not having % after constants – egreg Jan 28 '15 at 18:19
  • @TarwirdurTuron The answer to the linked question should be what you need. Please tell if you're not able to adapt it to your needs. – egreg Jan 28 '15 at 18:20