In formulas I need often the same constants and variables with lot of "make-up".

I want to have readable and maintainable LaTeX code, so I thought about writing \newcommands like

\newcommand*{\Cref}{C_{\textrm{ref}}} %very short example
\[ \Cref \cdot U = Q \]

But \Cref is already in use by \usepackage{cleveref} and so on …

Finally I run quickly out of short, nice commands.

I am looking for someone who has had the same problem and had a brilliant idea.

  • 1
    You might make \Cref doing different things in math mode and in text mode, but this is not recommendable. I see no solution other than renaming the shorthand to, say, \sCref ("s" for subscript).
    – egreg
    Nov 11, 2012 at 16:25
  • 1
    Or you can rename the original \Cref to for example \Cleveref and then use your own \Cref. Or you redefine \Cref in a way that when called \Cref{} it expands to C_{\textrm{ref}} otherwose the original definition is used. Nov 11, 2012 at 16:35
  • 3
    You could have a single shortcut command with a list of possible names, so for example \scut{Cref} might expand to C_{\textrm{ref}}. Would this make sense?
    – Joseph Wright
    Nov 11, 2012 at 16:44
  • 3
    @Qrrbrbirlbel I'd recommend none of your suggestions; the first is confusing, the second conceptually wrong as it gives a command different semantic values. In LaTeX the command \\ can have several different expansions, but it always means "end a line here", at the user level.
    – egreg
    Nov 11, 2012 at 16:53
  • 1
    I usually write \newcommand{myfoo}{...}. Then I know the command is mine, and won't accidentally conflict with something already defined. That's sometimes worth doing even if myfoo just wraps foo - so you can easily change foo behavior later. Nov 11, 2012 at 17:18

3 Answers 3


Defining \Cref so that it does different things in different contexts (for instance if it has an empty argument or not, or if it is in math or text mode) is definitely not recommendable, as it is confusing.

You can define a "shortcut" producing command:


Then you can set in your preamble


and use \scut{Cref} in your document. You can even say


and use \scut{C_ref}, if you prefer.

If \scut is perceived as too long, one can use a shorter sequence; \s is free, while \S prints § (so it could be redefined, though with care, because some document could use the original command).

One can use a "one character shortcut", for instance | or " (if babel is not used); the following definitions allow also for "parameterized" shortcuts with the same syntax as \newcommand.

\newcommand{\defineshortcut}[1]{\expandafter\newcommand\csname myscut@shortcut@#1\endcsname}
  \global\expandafter\chardef\csname myscut@catcode@#1\endcsname\catcode`#1\relax
  \catcode`#1\csname myscut@catcode@\string#1\endcsname}








However I wouldn't use this kind of "too short" shortcuts, which are error prone. The \useshortcut command should receive a non special ASCII character as its argument; high bit set characters are definitely not safe, because they are treated differently in different encodings.

  • Nice! But, even if using a one-letter command like \q, it's still four extra characters to type for each shortcut, three of which are difficult to access on some keyboards. What would the code look like to make the string, say, °xyz expand to \scut{xyz}? Also, is it possible to do "parametric" shortcuts with your approach?
    – krlmlr
    Nov 11, 2012 at 17:50
  • That looks good. We only need something shorter then \scut... is \S still free? Nov 11, 2012 at 19:49
  • Thank you, this is awesome! I understand that the braces in |{ref} are required because \scut simply calls \@nameuse.? How would you define \scut so that it uses all subsequent letter characters as shortcut name?
    – krlmlr
    Nov 11, 2012 at 22:18
  • @user946850 A final delimiter is necessary; it might be a space, but if you have |Cref at the end of a line TeX would not see a space and havoc would ensue.
    – egreg
    Nov 11, 2012 at 22:22
  • @egreg: Why is using the "end of line" as potential delimiter so difficult? I thought that TeX interprets EOL + all subsequent spaces as a single space...
    – krlmlr
    Nov 11, 2012 at 22:25

Here's a simple approach: make a command for the subscripted "ref".


This makes the "ref" akin to a diacritic or operator: This makes it easier to systematically refer to "ref"-variables in your text. I routinely do the same to define transposes and hermitian adjoints, as in


In other programming languages this is usually solved using namespaces. For LaTeX, there is the namespc package which provides

rudimentary C++-like namespaces in LaTeX.



  \section{First section}


  This is the original definition: \Cref{sec:first}

    This is used from within the ``shortcuts'' environment: \Cref

    \text{Equation} = \text{\Cref}

  This is to access the macro explicitly: \::shortcuts::Cref::

enter image description here

To be honest, I have found it only through a dedicated search, I have never seen it in use before. I am going to be using it from now on for the problem you described.

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.