4

I'm trying to write simple macros, which functional is to print every n = n + k-th letter from the alphabet, where k and n are counters. So I need a \loop-\repeat construction. I use it, but it works incorrectly unfortunately.

The main error is "missing number, treated as zero" in row with a \repeat command written. I also tried without using the \value{n} function - the same result.

Any ideas how to fix it to work fine?

Here is my code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english, russian]{babel}
\usepackage{amsmath}
\begin{document}
    \newcounter{n}
    \newcounter{k}
    \setcounter{k}{1}
    \setcounter{n}{0}
    \loop
    \alph{ \value{n} }
    \addtocounter{n}{k}
    \ifnum \value{n} < 26 \repeat
\end{document}
3

You use \value at the wrong place. You need to use it in the second argument of \addtocounter rather than in \alph. (I commented out the packages that are not directly related to the problem.)

\documentclass{article}
% \usepackage[utf8]{inputenc}
% \usepackage[english, russian]{babel}
% \usepackage{amsmath}
\begin{document}
    \newcounter{n}
    \newcounter{k}
    \setcounter{k}{1}
    \setcounter{n}{0}
    \loop
    \alph{n}
    \addtocounter{n}{\number\value{k}}
    \ifnum \value{n} < 26 \repeat

    Or without spurious spaces:
    \setcounter{n}{0}%
    \loop%
    \alph{n}%
    \addtocounter{n}{\number\value{k}}%
    \ifnum\value{n}<26\repeat
\end{document}

enter image description here

3

The command \alph takes as argument a counter's name, so \alph{\value{n}} is incorrect and should be \alph{n}.

The command \addtocounter takes two arguments; the first argument is the name of the counter LaTeX should act on, the second argument should be an integer (in any possible denotation); if you want to increment counter n by 31, you can do

\addtocounter{n}{31}
\addtocounter{n}{"1F} % hexadecimal
\addtocounter{n}{'37} % octal
\addtocounter{n}{\value{mycounter}}

provided mycounter currently stores 31. The second argument cannot contain just a counter's name.

So you have two calls wrong: \alph{\value{n}} (watch out for spaces) should be \alph{n}, whereas \addtocounter{n}{k} should be \addtocounter{n}{\value{k}}.

You also have spurious spaces and the order wrong (you won't get z):

\setcounter{k}{1}
\setcounter{n}{0}

\loop
\ifnum\value{n} < 26
\addtocounter{n}{\value{k}}% <--- needed
\alph{n}% <--- needed
\repeat

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.