1

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.

  • 1
    \xdef and \edef perform exhaustive expansion in the text of the definition, and some things don't work by expansion (the same reason as in your previous question about filecontents). \newcommand (which uses \def internally) doesn't try to expand, so it works. If you want \xdef, then use \protected@xdef instead – Phelype Oleinik Apr 01 '20 at 01:16
  • ok il try \protected@xdef ... – beethovengg14 Apr 01 '20 at 01:22
  • 1
    Note that not everything will work with \protected@xdef, only commands that are designed to work there, namely robust commands (accents like \c, commands declared with \DeclareRobustCommand, or with \newrobustcmd from etoolbox...) – Phelype Oleinik Apr 01 '20 at 01:25
  • I used \protected@xdef\myOwnCommand{\c{C}engel} instead of \xdef\myOwnCommand{\c{C}engel}

    i got the following error ! You can't use a prefix with `the character @'.

    @ l.15 \protected@ xdef\myOwnCommand{\c{C}engel} ?
    – beethovengg14 Apr 01 '20 at 01:25
  • 1
    You need do use \makeatletter before 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
  • note that other accents work fine,like \v. i dont know but there's something about this \c – beethovengg14 Apr 01 '20 at 01:29
  • ok i will use \makeatletter .. \makeatother – beethovengg14 Apr 01 '20 at 01:29
  • it worked using \makeatletter. :) – beethovengg14 Apr 01 '20 at 01:30
  • 1
    It depends on the definition of the accent. \v “works by accident”. Though try \show\myOwnCommand after 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:31
  • the following code also worked: \makeatletter\xdef\myOwnCommand{\v{C}engel}\makeatother

    thank you so much @PhelypeOleinik

    – beethovengg14 Apr 01 '20 at 01:39
  • yes the link you gave is related to my question here. – beethovengg14 Apr 01 '20 at 01:41
  • i think it is really safer to use \protected@xdef – beethovengg14 Apr 01 '20 at 01:44
  • 1
    A few points: 1) In \makeatletter\xdef\myOwnCommand{\v{C}engel}\makeatother you don't need \makeatletter...\makeatother, as there is no command with @ in the name. 2) Try \xdef\myOwnCommand{\v{C}engel}\show\myOwnCommand and see the terminal output. 3) Yes, if the contents of the definition are “text” (accents and such) \protected@xdef is 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:n does the “protection” here). – Phelype Oleinik Apr 01 '20 at 01:51
  • i just realized that \xdef\myOwnCommand{\v{C}engel} as is works (and also using the other accents), but \xdef\myOwnCommand{\c{C}engel} as is does not. initially i thought there is a bug in \c, or something is wrong with its definition. it would be nice though if \xdef\myOwnCommand{\v{C}engel} works for ANY ACCENT. perhaps our TeXperts and LaTeXperts can give this a thought. – beethovengg14 Apr 01 '20 at 02:06

0 Answers0