2

General overview

The goal is to make a list of photo credits at the end of a book.

MWE

\documentclass{article}

\newcommand\credittable{} % This command contain the final rendering. 

\newcommand{\addcredit}[1] % Prepare each entry rendering for \credittable and append it.
{
  \let\tempcredittable\credittable
  \renewcommand{\credittable}{\expandafter\tempcredittable #1. }
}

\begin{document}


\addcredit{Author0} 
\addcredit{Author1}

{\Large Credits}
\credittable % Make the final list of credits
\end{document}

The problem

This is the output of xelatex:

! TeX capacity exceeded, sorry [input stack size=5000].
\tempcredittable ->\expandafter \tempcredittable A
                                                  uthor0.
l.20 \credittable

No pages of output.
Transcript written on test.log.

But, I noticed this code works when their is only one entry. Witch isn’t helpful.

The question

So, how to create a command who append another existing command?

3
  • Do you want to process each element of the final list, or just set them in the text as-is?
    – Werner
    Sep 6, 2023 at 18:46
  • Related: Recursive use of \renewcommand: How to make it work?
    – Werner
    Sep 6, 2023 at 18:48
  • For the benefit of others, who find this question for more general purposes than the specific question asked: The etoolbox package has \apptocmd and \pretocmd macros. See its documentation. Particularly note that the macros have extra mandatory arguments (which you may leave empty) that can trap whether the macro succeeded or failed.
    – rallg
    Sep 6, 2023 at 22:05

3 Answers 3

3

I find this sollution with etoolbox:

\documentclass{article}
\usepackage{etoolbox}

\newcommand\credittable{} % This command contain the final rendering. 

\newcommand{\addcredit}[1]{ % Append the new content to \credittable
  \gappto\credittable{#1. }
}

\begin{document}

\addcredit{Author0} 
\addcredit{Author1}

{\Large Credits}
\credittable % Make the final list of credits
\end{document}
1
  • Are the additional spaces deliberate?
    – cfr
    Sep 6, 2023 at 23:18
3

You need to do the one-level expansion at definition time:

\newcommand{\addcredit}[1]
{% <--- don't forget
  \expandafter\gdef\expandafter\credittable\expandafter{\credittable #1. }% <--- don't forget
}

In this case \def is simpler to use; better yet, \gdef so grouping will not affect the redefinition.

There are better ways.

\documentclass{article}
\usepackage{booktabs} % for the last example

\ExplSyntaxOn

\NewDocumentCommand{\addcredit}{m}
 {
  \fauve_credit_add:n { #1 }
 }

\NewDocumentCommand{\credittable}{sO{.~}}
 {
  \fauve_credit_print:n { #2 }
  \IfBooleanF{#1}{#2}
 }

\seq_new:N \g_fauve_credit_list_seq

\cs_new_protected:Nn \fauve_credit_add:n
 {
  \seq_gput_right:Nn \g_fauve_credit_list_seq { #1 }
 }

\cs_new_protected:Nn \fauve_credit_print:n
 {
  \seq_use:Nn \g_fauve_credit_list_seq { #1 }
 }

\ExplSyntaxOff

\begin{document}


\addcredit{A.~Uthor (London)} 
\addcredit{R.~Eporter (New York)}

\section*{Credits}

\credittable

\section*{Credits again}

\credittable*[ -- ]

\section*{Credits in table}

\begin{tabular}{@{}l@{}}
\toprule
\multicolumn{1}{@{}c@{}}{Credits} \\
\midrule
\credittable[\\]
\bottomrule
\end{tabular}

\end{document}

With \credittable* the separator is not repeated at the end. The default value of the separator is “period space”.

Quite flexible, isn't it?

enter image description here

1

Is this what you want? :

\documentclass{article}

\newcommand\credittable{}%

\newcommand{\addcredit}[1]{%
  \expandafter\renewcommand
  \expandafter{%
  \expandafter\credittable
  \expandafter}%
  \expandafter{%
  \credittable
  \formatcreditentry{#1}}%
}

\newcommand\formatcreditentry[1]{#1. }%

\begin{document}

\addcredit{Author0} 
\addcredit{Author1}

{\Large Credits}
\credittable % Make the final list of credits
\end{document}

You might be interestred in

  • how \expandafter actually works,
  • \g@addto@macro,
  • the \@starttoc-\addtocontents-mechanism of the LaTeX 2ε-kernel.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .