0

I need a \def whose name involves a number, with that number being value of a counter.

\documentclass[varwidth,border=5mm]{standalone}

\begin{document}

% COUNTER
\newcounter{myC}
\addtocounter{myC}{1}

% DOESN'T WORK
% \def\csname\myM\value{myC}\endcsname{...}

\end{document}
David Carlisle
  • 757,742
bp2017
  • 3,756
  • 1
  • 13
  • 33
  • 2
    \expandafter\def\csname\the\value{myC}\endcsname{...} or \@namedef{\the\value{myC}}{...} – Phelype Oleinik Jul 31 '19 at 21:22
  • 2
    You may think to \value{myC} as the abstract counter's value, independent of its representation (it's not the whole truth, though). The number two is a different thing from 2 (its decimal representation). – egreg Jul 31 '19 at 21:45

1 Answers1

6
 \def\csname\myM\value{myC}

would redefine \csname to be a macro that had to be followed by the tokens \myM\value and which expanded to myC. That isn't what you want, closer would be

\expandafter\def\csname...

but \value{myC} is the internal count register not the decimal expansion so you want

\expandafter\def\csname myM\arabic{myC}\endcsname{...}
David Carlisle
  • 757,742
  • @bp2017 just the same \csname myM32\endcsname will use the 32nd one which is fairly inconvenient which is why most people use \roman{..} rather than arabic then you could access it as \myMxxxii (see any aux file of a document using longtable) – David Carlisle Jul 31 '19 at 21:47
  • possible duplicate https://tex.stackexchange.com/questions/66666/command-macro-name-cannot-include-numbers-and-symbols/66695#66695 – David Carlisle Jul 31 '19 at 21:47
  • @bp2017 I'm not sure that I understood that comment but I think that is the wrong way to think about it, \csname abc\endcsname constructs the token \abc it isn't a different kind of name that requires csname to access it. – David Carlisle Jul 31 '19 at 21:51
  • 1
    @bp2017 not necessarily: if you really want digits rather than roman numbers and really want a backslash then you just need to locally \catcode\1=\catcode`athen you can use\myM1` – David Carlisle Jul 31 '19 at 21:57
  • @bp2017 you can of course use backslashes to access commands inside \csname so long as those commands expand to character tokens not to unexpandable command tokens, hence the use of \arabic in the example in the answer. – David Carlisle Jul 31 '19 at 22:00
  • @bp2017 I know, but \arabic is latex syntax – David Carlisle Jul 31 '19 at 22:02