I a looking for a command that would output $C$ if I only write \c and output $15C$ if I write \c{15}. How can that be done?
\newcommand{\c}{$C$}
I a looking for a command that would output $C$ if I only write \c and output $15C$ if I write \c{15}. How can that be done?
\newcommand{\c}{$C$}
While technically feasible, creating commands with brace-delimited optional arguments is not a good idea: this is highly non-standard in LaTeX and is likely to be confusing. The standard syntax for LaTeX is to delimit optional arguments with square brackets, which can be done this way in your case:
\newcommand*{\cc}[1][]{$#1C$}
The [] means that the command has an optional argument and that its default value is empty (\newcommand supports at most one optional argument, and it has to come first; if you want more power and freedom here, use \NewDocumentCommand and friends which are already available if you use the LaTeX format from 2020-10-01 or later, otherwise just require a \usepackage{xparse}). If you replaced the [] with [abc], the default value would be abc.
This is a valid definition that will produce $C$ when no [ follows \cc, and $15C$ when, for instance, you input \cc[15].
Note that I didn't use the name \c that you proposed, otherwise one would get the error LaTeX Error: Command \c already defined. Indeed, the \c command already exists in plain TeX and in LaTeX: it is used to produce a cedilla. For instance, \c{e} prints an e with a cedilla (whatever that means; ç and Ç, which can be obtained with \c{c} and \c{C}, are used in French words such as « remplaçant » and « Ça », for instance).
When naming personal macros, you may choose the names you want as long as they don't clash with already defined commands. In general, one-letter command names should be avoided because they are too likely to be used by some standard macro package (plain TeX, LaTeX, amsmath maybe...). Safe names contain a portion unique to you or to your project; such names are almost always used (at least for internal macros) when writing classes or packages. On this site, we often use names such as \myCommand or \my@command, implicitly suggesting you to replace my with something more personal (because if you take \myCommand from an answer and another \myCommand from a different answer, they are quite likely to clash!). TeX doesn't have the notion of modules or namespaces; being careful with unique prefixes (or suffixes) is a reasonable way to cope with this lacking feature.
ç. :)
– Alenanno
Oct 21 '20 at 16:58
\c{c}, that could confuse people who don't already know \c (the answer being rather targeted at beginners). :-)
– frougon
Oct 21 '20 at 16:59
ç or \c{c}, French is my mother tongue. :-)
– frougon
Oct 21 '20 at 17:05
\cc command discussed here accepts such a brace-delimited optional “argument,” and you have \cc some text... in your document. All fine. Later, you decide that you need to do something local for “Some text”—anything. For instance, that could be \cc {\bfseries some text} .... Instead of just doing some local change, you suddenly made \bfseries some text the “argument” of \cc. Probably not what you wanted here. Now if you're curious and...
– frougon
Oct 21 '20 at 19:00
\@ifnextchar\bgroup to detect if what comes next (after skipping any space tokens) is an opening brace. One could alternatively use \futurelet directly and avoid skipping space tokens, if desired (the code for \@ifnextchar is based on \futurelet, and goes to some lengths in order to ignore any space tokens before the “interesting token”).
– frougon
Oct 21 '20 at 19:02
\cc\relax at the calling site when you want to be sure that no optional argument will be taken from what follows in the input stream (writing from smartphone is a pain).
– frougon
Oct 21 '20 at 19:45
\newcommand\foo{} instead of \newcommand{\foo}{}, for instance. This feature is simply incompatible with optional brace-delimited "arguments", since then any following token that is not a curly brace would be taken as an argument (I completely rewrote this last sentence because it was very badly worded :).
– frougon
Oct 21 '20 at 20:49
\newcommand*{\c}[1][]{$#1C$}. But you should avoid one-letter macros: this entails the risk of clashing with something else. You need to write\c[15], not\c{15}, by the way (braced optional arguments are not a good idea, whereas square brackets are standard in LaTeX for this). – frougon Oct 21 '20 at 15:49LaTeX Error: Command \c already defined, precisely for the reason I warned you about: there is already a standard\ccommand, that is used to obtain a cedilla. Find another name. :-) – frougon Oct 21 '20 at 15:57