# Difficulty with datatool

I want to use the command \DTLstoreinitials from package \usepackage{datatool} in the following way :

\newcommand{\extractInitials}[1]{%
\DTLstoreinitials{#1}{\userInitials}%
\DTLsubstituteall{\userInitials}{.}{}%remove points between the initials
\DTLsubstituteall{\userInitials}{ }{}%remove spaces
\userInitials%
}

If I call the command directly, it works :

\extractInitials{My Name}

gives MN. But if I use an auxiliary command: \newcommand{\nom}{My Name} and try to call

\extractInitials{\nom}

then the compilation fails with error

! Paragraph ended before \@dtl@initials was complete.

How can I fix this?

Here is a minimal example 1 :

\documentclass[12pt]{article}
\usepackage{trackchanges}
\usepackage{datatool}

\newcommand{\extractInitials}[1]{%
%\expandafter\DTLstoreinitials\expandafter{#1}{\userInitials}%
\DTLstoreinitials{#1}{\userInitials}%
\DTLsubstituteall{\userInitials}{.}{}%remove points
\DTLsubstituteall{\userInitials}{ }{}%remove spaces
}
\begin{document}
----------- \\% to see the result of the addeditor commands
\makeatletter
\TC@editorOne\\
\TC@editorTwo\\
\TC@editorThree\\
\makeatother
-----------
\end{document}

The output is CD n/n n/n To compare : example 2

----------- \\% to see the result of the addeditor commands
\makeatletter
\TC@editorOne\\
\TC@editorTwo\\
\TC@editorThree\\
\makeatother
-----------

The output is AB CD n/n

-
You don't have to sign with your name since it automatically appears in the lower right corner of your post. I've edited your post accordingly. –  Werner Dec 21 '11 at 23:13
Welcome to TeX.sx! Note that you can edit your posts to add more information. Answer posts are for solutions to the question. –  Martin Scharrer Dec 22 '11 at 22:09
I've added note 3. –  egreg Dec 22 '11 at 22:34
thank you, it solved my problem. –  nicolas roy Dec 22 '11 at 23:42

You have two choices.

## First

\newcommand{\extractInitials}[1]{%
\expandafter\DTLstoreinitials\expandafter{#1}{\userInitials}%
\DTLsubstituteall{\userInitials}{.}{}%remove points between the initials
\DTLsubstituteall{\userInitials}{ }{}%remove spaces
\userInitials
}

This can be used if \nom expands immediately to the name and no more processing is needed.

The trick is to access at #1 before processing by \DTLstoreinitials begins. The first \expandafter acts on the second, which in turn expands the first token after the brace: in your case it can be \nom; if you give directly a name nothing happens (but see the second note).

## Second

\makeatletter
\newcommand{\extractInitials}[1]{%
\begingroup\protected@edef\x{%
\endgroup\noexpand\DTLstoreinitials{#1}{\noexpand\userInitials}}\x%
\DTLsubstituteall{\userInitials}{.}{}%remove points between the initials
\DTLsubstituteall{\userInitials}{ }{}%remove spaces
\userInitials
}
\makeatother

This is to be preferred if the contents of \nom is more complicated (say \nom becomes \Prenom\space\Nom. Here we expand until the bottom, but "robusted" macros such as those for accented letters remain untouched.

## Note 1

Using \edef in this context directly is bound to break if the argument of \extractInitials contains accented characters (even when a control sequence is used).

## Note 2

In both cases, results are unpredictable if the first initial is an accented letter. Put it as {É}lie Cartan to get a correct result. However, none of the method works reliably in this case if the input encoding is UTF-8 (for limitations of datatool). Write always a first accented initial in braces.

## Note 3

In case this macro must be fed to another command, the result will depend on what this macro does. For example it will work with \textbf{\extractInitials{My Name}}, since \textbf wants only to print its argument. In other cases a different approach must be taken; for example, in your test document one has to take an indirect path:

\newcommand{\extractInitials}[1]{%
\expandafter\DTLstoreinitials\expandafter{#1}{\userInitials}%
\DTLsubstituteall{\userInitials}{.}{}%remove points between the initials
\DTLsubstituteall{\userInitials}{ }{}%remove spaces
}