I would like to define a TeX 'macro-building' command (let us call it \buildmacro
) that takes as argument a string of 'normal' characters (e.g., Title
) and which satisfies the followings:
- the command
\buildmacro{Title}
defines the counter with the nameTitleXcounter
and initializes it to 0. when issuing at some point in the file the TeX command
\Title{<arg>}
, this will have the effect of increasing the counterTitleXcounter
and then putting the value<arg>
into a new TeX macro with the name\Title-<x>
, where<x>
is the current value value ofTitleXcounter
;here,
<arg>
is a piece of TeX code (that can contain text, maths, paragraphs of any other TeX commands).
Consider the following minimal example:
\documentclass{article}
\usepackage{ifthen}
\def\buildmacro#1{...}% the definition of \buildmacro
\begin{document}
\buildmacro{Title}
% 'Read' several titles
\Title{The first title}
\Title{The second title}
\Title{The third title}
\newcount\tempcount
\tempcount=1\relax
% 'List' all the Titles
\whiledo{\the\tempcount<\numexpr \thetitleXcounter+1\relax}
{%
\textsc{\csname title-\the\tempcount\endcsname}\par
\advance\tempcount by 1\relax
}
\end{document}
I 'played' with \csname
and \endcsname
, \expandafter
, \def
, \gdef
and the \long
versions, but with no 'luck'.
Edited (in order to answer some of the comments):
The purpose of this is to have a simple mechanism to create a set of indexed commands with the same root-name (similar to a list of commands). One can always use the \csname <...> \endcsname
construct to issue the commnds.
-
or a number. You need to stick to letters, basically. (@
if@
is currently a letter or whatever is also OK. But that means a catcode change to make it a letter. Same for expl3 syntax with_
and:
being allowed.) Moreover,\title
is a core LaTeX command.\newcommand
and friends in LaTeX, namely they check you aren't overwriting stuff which is already defined. If you want to overwrite stuff, use\renewcommand
or whatever.\newcommand\cmdmaker[2]{\expandafter\newcommand\csname #1\endcsname{\textbf{#2}}}
, where the usage would be like\cmdmaker{firstone}{Look at this!}
with you being able to use\firstone
in the body of your.tex
file. But I really don't get the point/goal of the counter stuff....\csname <...> \endcsname
construct. Of course,\title
was an unfortunate choice, but we can uppercase it to\TITLE
, so there is no conflict.1
etc. catcode 11 is easy, but I think,you get into troubles when defining some macro number argument, i.e.[1]
etc.