Using \csname in \renewcommand to change command definitions
First attempt
\renewcommand\csname the#1\endcsname{#2}
You're trying to redefine \csname
to be t
Second attempt
\renewcommand \the#1{#2}
You're trying to redefine \the
to expand to the first token in #1
Third attempt
\def\headername{\csuse{the#1}}
\renewcommand \headername {#2} % compiles but has no effect
You're redefining \headername
Fourth attempt
\renewcommand \csuse{the#1} {#2}
You're redefining \csuse
Fifth attempt
\makeatletter
\expandafter\renewcommand \@nameuse{the#1} {#2}
\makeatother
You're trying to define \spacefactor
, because you're expanding \@
. Note that \makeatletter
and \makeatother
should surround the outer \newcommand
, not be inside the replacement text. But this wouldn't fix the code, because
\makeatletter
\newcommand\changeprefix[2]{
\expandafter\renewcommand \@nameuse{the#1} {#2}
}
\makeatother
would try and redefine \csname
, which is the first token in the expansion of \@nameuse
.
Correct version
\makeatletter
\newcommand{\changeprefix}[2]{%
\@namedef{the#1}{#2}%
}
\makeatother
or
\newcommand{\changeprefix}[2]{%
\expandafter\renewcommand\csname the#1\endcsname{#2}%
}
Since etoolbox
is loaded already, there is a quicker way with \csdef
or \csgdef
, depending on the desired 'sustainability' in a group:
No 'weird' \makeatletter...\expandafter...\makeatother
constructs to be used here:
\documentclass{report}
\usepackage{etoolbox}
\newcommand{\changeprefix}[2]{%
\csdef{the#1}{#2}%
}
\begin{document}
\changeprefix{section}{test prefix - }
\section{Test section}
Some text...
\end{document}