2

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$}

  • 1
    \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 at 15:49
  • @frougon, thank you! – Clone Oct 21 at 15:49
  • 1
    You're welcome. In fact, if you try this definition, you'll get the error LaTeX Error: Command \c already defined, precisely for the reason I warned you about: there is already a standard \c command, that is used to obtain a cedilla. Find another name. :-) – frougon Oct 21 at 15:57
  • Will do, thank you :) – Clone Oct 21 at 16:02
  • @frougon You should post that as an answer. :D – Alenanno Oct 21 at 16:07
7

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.

| improve this answer | |
  • The cedilla is a diacritica added below letters, it looks like a hook or tail, it appears in French for example. See the character ç. :) – Alenanno Oct 21 at 16:58
  • @Alenanno I know this but wanted to avoid the example \c{c}, that could confuse people who don't already know \c (the answer being rather targeted at beginners). :-) – frougon Oct 21 at 16:59
  • 1
    @dedded The main problem is that it would increase the likelyhood of creating errors (what I called “be confusing”). For instance, suppose the \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 at 19:00
  • 1
    @dedded want to see how one can implement such “arguments,” you may have a look here for instance. In short, one can use \@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 at 19:02
  • 1
    @dedded For TeX people who see a command taking a brace-delimited argument, there is also the expectation that this argument can be a single token written without braces. This is what allows one to write \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 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.