Do you know a simple way to use the current counter value to call the argument in a macro?
In myPackage I have this
Note: I have the for loop defined as
% \forloop[step]{counter}{initial_value}{conditional}{code_block}
The problem is in the following code
\newcounter{foo}
\newenvironment{myEnvironment}[1]{
\newcommand{\myLocalMacro}[#1]{% number of arguments depends on the value inserted in myEnvironment (this works fine)
\forloop[1]{foo}{1}{\numexpr\value{foo}-1<#1}{
##\value{foo} % HERE I need to return ##1 if foo is 1, ##2 if foo is 2 etc. (this doesn't work)
- % some separator
}%end forloop
}%end myLocalMacro
}
{}%end myEnvironment
I tested the variable number of arguments in \myLocalMacro
and it works.
I also tested the nesting of arguments in \forloop
– writing ##1
, ##2
etc. inside the for loop gives 1st and 2nd value. Now I need it dependent on foo
counter.
The main document will look like this
\documentclass{article}
\usepackage{myPackage}
\begin{document}
\begin{myEnvironment}{7}
\myLocalMacro{F}{O}{O}{B}{A}{R}{1} % this separation into arguments will be written manually
\myLocalMacro{F}{O}{O}{B}{A}{R}{2}
\myLocalMacro{F}{O}{O}{B}{A}{R}{3}
\end{myEnvironment}
or
\begin{myEnvironment}{3}
\myLocalMacro{T}{e}{X}
\myLocalMacro{a}{b}{c}
\end{myEnvironment}
\end{document}
The expected output should look like
F – O – O – B – A – R – 1 –
F – O – O – B – A – R – 2 –
F – O – O – B – A – R – 3 –
or
T – e – X –
a – b – c –
Well, this is only an example, the real formating will be more complex, which is why I defined an environment for it and don't do it manually for every single character.
Thanks.
Best Answer
Your proposed definition can't work, because TeX makes no expansion to the tokens in the replacement text of a macro and
#1
must be literal: it can't be given as#\value{counter}
. There are some tricks for making this possible, but I don't think they would help in your case.Here's something that might be what you're looking for:
The argument to
myEnvironment
is the formatting macro which is applied to every token in the argument to\myLocalMacro
.A different implementation that doesn't require to define auxiliary macros:
The argument to
myEnvironment
is the formatting you want to apply to each token.If you want to preserve spaces, you need a more complex approach; here's a straightforward way with
expl3
:Here
\tl_map_inline:Nn
is the analog of\@tfor
; before applying it, we change all spaces in the argument with\c_space_tl
, whose expansion is a space.