I'm trying to do the same as this one, but with \def, \gdef or \xdef instead.
Very quick summary of the other question:
I wanted to print the command (not the contents of the command) regardless of it being defined by \csname or not. Solution was to check whether the first string is \csname or the command.
So \def needs an \expandafter if used with \csname. If not then everything is ok:
\def\str{String}
\expandafter\def\csname str\endcsname{String}
Both are correct. But if I wanted to change \str or \csname str\endcsname with an argument (#1). Now it's not known whether I need to use \expandafter or not. If used when not needed, it results in an error. I wrote this (in analogy with the other question)
\def\defcmd#1{
\ifx\csname#1
\expandafter\def\csname
\else
\def#1
\fi
}
\defcmd\str{String}
\defcmd\csname str\endcsname{String}
This will absorb the first string after \defcmd as argument (#1), check whether it's \csname or anything else and will then replace itself either with \expandafter\def or \def and place the argument back.
When running that code, no problem at all. But when I try to output the command (either directly or in a file) I get the following error:
! Use of \str doesn't match its definition
l.24
And I have no clue why that is. (l.24 is one line after the use of \str)