3

I think the answer to my question is "no," but one could argue that it would be nice if there were. Here's what motivates the question. Define

\def\myloop#1#2{\count11=#1#2%
\advance\count11 by -1\ %
\ifnum\count11>0{\myloop{\count11}{#2}}\fi}

\myloop{4}{T} then produces T T T T (notice the extra space at the end). The undesired space at the end of the output is due to the space in the definition that is required to terminate the \advance command. The space has two effects: it acts as both a delimiter in the programming language, and it also appears on the page. What would be nice is a kind of "delimiting but invisible" space.

Of course, it's not clear how one would express such a thing, or how it would be better than

\def\myloop#1#2{\count11=#1#2%
\advance\count11 by -1\relax\ifnum\count11>0{\ }\fi 
\ifnum\count11>0{\myloop{\count11}{#2}}\fi}

But TeX has so many nooks and crannies that I may have overlooked something.

Apologies for the title, someone else suggested it, and I couldn't resist.

15
  • 1
    no, spaces used to terminate a number are absorbed and never produce output Feb 18 at 15:39
  • 2
    You're inserting a control space \ , that is different from a space.
    – Skillmon
    Feb 18 at 15:42
  • 6
    Also: Ground control to Major Tom!
    – Skillmon
    Feb 18 at 15:42
  • 2
    @RandallFairman but is wrong. Don't use \ to terminate number parsing. If you have to be somewhat explicit use \advance\mycount by -1\@sptoken.
    – Skillmon
    Feb 18 at 15:51
  • 2
    A space token, which \ isn't, will terminate the search for digits and will be ignored. Any other unexpandable token that's not a digit will do as well, but it won't be ignored. I explained it in my answer to your previous question.
    – egreg
    Feb 18 at 15:54

1 Answer 1

2

Quoting from my previous answer to Parsing oddity: when is a space not a space

Some more explanations

A space token in the above sense is an explicit character with category code 10. When TeX is looking for digits to perform an assignment, it will do macro expansion (including expandable primitives) and it will stop, as said, when, after expansion, a nondigit is found. If the stop is caused by a space token, it will be ignored; otherwise the token causing the stop is read again after the assignment has been completed.

It follows that \ is not a space token for the purpose of terminating numbers. It does stop the search for further digits, but it is an unexpandable primitive, so it will be reexamined after the number has been used, in this case to make an assignment to \count11. And therefore it will appear in print.

\newcount\loopcount
\def\myloop#1#2{\loopcount=#1\relax
  #2% print the second argument
  \advance\loopcount by -1 % decrease the counter
  \ifnum\loopcount>0
    \myloop{\loopcount}{#2}% repeat
  \fi
}

You need \relax in the first line, because #1 might be either an explicit number or a register. A space token will not be gobbled if the assignment uses a register's value. In the \advance line, -1 is properly terminated by a space (which won't appear in output). Similarly for the endline following 0 in the next line (it is converted to a space when TeX tokenizes the input).

Footnote

You shouldn't be using \count11, to begin with. It's an important register used by TeX to keep track of register allocations: line 194 in plain.tex says

\count11=9 % allocates \dimen registers 10, 11, ...

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.