3

I'm making a new command like which goes kind of like this:

\newcommand\mycommand[1]{\#1section}

So, what I what is to type \mycommand{sub}{blah} to output \subsection{blah} and \mycommand{subsub}{blah} to output \subsubsection{blah}. Is this even possible?

1 Answer 1

9

\csname allows to construct command sequence names:

\newcommand*{\mycommand}[1]{\csname#1section\endcsname}

If the constructed command is not defined, then it does not throw an error. Instead, the constructed command has the meaning \relax.

7
  • Nice, so \csname allows you to construct command sequence names, but is there anything that allows you to construct variables defined with \def? Jul 4, 2017 at 20:02
  • @SkeletonBow The "command sequence names" are just command tokens that start with the escape char (backslash). Macros (defined by \def, \newcommand, ...) belong to them. Jul 4, 2017 at 20:04
  • Oh, okay. Then my problem must lie elsewhere. I just checked and confirmed what you said, anyway :) ) Jul 4, 2017 at 20:05
  • @SkeletonBow Probably, a missing \expandafter: \expandafter\def\csname ...\endcsname Jul 4, 2017 at 20:09
  • 1
    @SkeletonBow -- to make this question "persistent", please insert your mwe in the question itself, not linked elsewhere. (even though it links to another tex.sx question, which probably won't disappear, this one will be more helpful to future visitors if it is complete.) Jul 4, 2017 at 23:51

You must log in to answer this question.

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