2

I want to define a command \C4 which produces C4 (both in bold and normal fonts). I wrote something like

\begin{document}
 \DeclareTextFontCommand{\textscn}{\upshape\bfseries}
 \newcommand{\C4}{\textscn{C4}}
\end{document}

This code gives an error. I wonder how can I avoid this error ?!.

Thanks in advance.

3
  • 4
    You cannot define commands having both letters and digits in their name. See tex.stackexchange.com/q/598250/4427
    – egreg
    Nov 30, 2023 at 22:19
  • One commonly used solution is to write the number using roman numerals (which are letters). Thus, Civ. Case-sensitive. Be sure tha the desired command is not already used by a package.
    – rallg
    Nov 30, 2023 at 23:11
  • 1
    \newcommand{\C}[1]{\textscn{C#1}} would allow \C4 and \C5 etc Nov 30, 2023 at 23:52

2 Answers 2

4

You can not have commands with numbers in their name

You could have

\newcommand{\C}[1]{\textscn{C#1}}

defining \C to take an argument so \C4 or \C3 would work, as would \C{hello world}

However I would avoid 1-letter commands, most are pre-defined in latex for accents and similar commands. For example if you start using \C you might be tempted to need \c for a lowercase version but \c is the cedilla accent.

I would use a slighly longer name such as

\newcommand{\upC}[1]{\textscn{C#1}}

which would allow \upC4 and \upC5

or a name based on the semantic meaning, whatever that is. For example:

\newcommand{\category}[1]{\textscn{C#1}}

which would allow \category4 and \category5 if C4 stood for "category 4" in your real context.

4

The syntax rules of TeX allow you to do

\def\C4{\textscn{C4}}

BUT you're NOT defining a command whose name is C4; you're defining the command \C which is required to be followed by 4.

So, for instance, you cannot have also \C3.

You find very interesting posts at *Why* does TeX not allow numbers in command names? about the subject.

It is actually possible to make \C into looking for an integer following it and branch according to the value, but this is quite fragile and not recommended.

I'd simply choose \Civ or \Cfour.

1
  • How many times have you already explained this fact on this network?
    – wipet
    Dec 1, 2023 at 21:11

You must log in to answer this question.

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