I'm trying to increase my LaTeX3-fu, but I'm getting an error about encountering printed output before the document begins.
I'm trying to generalize How to get the section title by section number?.
I also understand that LaTeX3's big punch is to (mostly) do away with \expandafter, but I don't know the equivalent syntax if there is a one-to-one relationship. (I'm really just trying to learn, here :-).)
Here is my code:
\documentclass{article}
\usepackage{xparse}
% Macro to insert labels at the end of other macros
\ExplSyntaxOn
\NewDocumentCommand{\labelize}{mm}{
% Automatically inserts a label of the form \label{#2:\the#1}for
% referencing after every #1
% For example:
% \labelize{section}{auto-section}
% ...
% \begin{document}
% \section{something}
% -->\label{auto-section:1}
% \section{something else}
% -->\label{auto-section:2}
% \end{document}
% #1 : Macro to labelize
% #2 : Prefix for label
% Store the old macro
\let\expandafter\csname old#1\endcsname\expandafter\csname #1\endcsname
% Adapted from https://tex.stackexchange.com/questions/76078/
% how-to-get-the-section-title-by-section-number
\expandafter\renewcommand\csname #1\endcsname[2]{
\ifx##1\relax
\csname old#1\endcsname{##2}
\else
\csname old#1\endcsname[##1]{##2}
\fi
\label{#2:\csname the#1\endcsname}%
}
}
\ExplSyntaxOff
\labelize{section}{auto@section}
\begin{document}
Hi
\end{document}
\sectiontakes no argument. It's not clear what the\labelizecommand should do. And, most importantly, you're trying to redefine\expandafter; at least switch the positions of\renewcommandand\expandafter;\expandafter\csnameis a (minor) mistake, too. – egreg Jul 06 '13 at 20:28section{argument here?}? How then does the more explicit case (linked) work? I tried to model it exactly. – Sean Allred Jul 06 '13 at 20:31\labelizeis supposed to do? – egreg Jul 06 '13 at 20:33\sectiontakes no arguments"... it at least seems to as an end-user. – Sean Allred Jul 06 '13 at 20:38\expandafterto hop over\letuntil the control sequences\csname ... \endcsnameresolve? – Sean Allred Jul 06 '13 at 23:42\let\expandafter\csname...\endcsnamedefines\expandafterto mean\csname, which is not what you want. Presumably,\expandafter\let\csname...\endcsnameis wanted. – Bruno Le Floch Jul 07 '13 at 02:36