Consider the following MWE1:
\documentclass{article}
\newcommand*{\myOwnCommand}{\c{C}engel}
\begin{document}
This is my surname: \myOwnCommand.
\end{document}
MWE1 works just fine. The cedilla is produced under C.
Now, consider the following MWE2.
\documentclass{article}
\xdef\myOwnCommand{\c{C}engel}
\begin{document}
This is my surname: \myOwnCommand.
\end{document}
This time, I have defined \myOwnCommand globally using the TEX primitive \xdef, and I wanted \myOwnCommand also to be expanded at once. I'm going to use it inside loops like \DTLforeach in the datatool package.
When I compile MWE2, I get the following error:
! Illegal parameter number in definition of \myOwnCommand.
<to be read again>
\crcr
l.15 \xdef\myOwnCommand{\c{C}
engel}
?
I ask myself where my error is.
\xdefand\edefperform exhaustive expansion in the text of the definition, and some things don't work by expansion (the same reason as in your previous question aboutfilecontents).\newcommand(which uses\definternally) doesn't try to expand, so it works. If you want\xdef, then use\protected@xdefinstead – Phelype Oleinik Apr 01 '20 at 01:16\protected@xdef, only commands that are designed to work there, namely robust commands (accents like\c, commands declared with\DeclareRobustCommand, or with\newrobustcmdfrometoolbox...) – Phelype Oleinik Apr 01 '20 at 01:25i got the following error ! You can't use a prefix with `the character @'.
\makeatletterbefore using commands with@in their name:\makeatletter \protected@xdef.... See: https://tex.stackexchange.com/q/8351/134574 – Phelype Oleinik Apr 01 '20 at 01:28\v“works by accident”. Though try\show\myOwnCommandafter the definition :-) Glad it worked. I'll mark a duplicate (asked by me: I had this same problem some time ago :-) – Phelype Oleinik Apr 01 '20 at 01:31thank you so much @PhelypeOleinik
– beethovengg14 Apr 01 '20 at 01:39\makeatletter\xdef\myOwnCommand{\v{C}engel}\makeatotheryou don't need\makeatletter...\makeatother, as there is no command with@in the name. 2) Try\xdef\myOwnCommand{\v{C}engel}\show\myOwnCommandand see the terminal output. 3) Yes, if the contents of the definition are “text” (accents and such)\protected@xdefis safer. 4) If you have an updated distribution you may also try\ExplSyntaxOn\cs_new_eq:NN\TextExpand\text_expand:n\ExplSyntaxOff\xdef\myOwnCommand{\TextExpand{\c{Ç}engel}}(\text_expand:ndoes the “protection” here). – Phelype Oleinik Apr 01 '20 at 01:51