Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
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 '12 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. –  Qrrbrbirlbel Nov 11 '12 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 '12 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 '12 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. –  Ethan Bolker Nov 11 '12 at 17:18
add comment

3 Answers 3

up vote 10 down vote accepted

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:

\makeatletter
\newcommand{\scut}[1]{\@nameuse{myscut@#1}}
\newcommand{\defineshortcut}[2]{\global\@namedef{myscut@#1}{#2}}
\makeatother

Then you can set in your preamble

\defineshortcut{Cref}{C_{\textnormal{ref}}}

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

\defineshortcut{C_ref}{C_{\textnormal{ref}}}

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.

\documentclass{article}
\usepackage{amsmath}
\makeatletter
\newcommand{\scut}[1]{\@nameuse{myscut@shortcut@#1}}
\newcommand{\defineshortcut}[1]{\expandafter\newcommand\csname myscut@shortcut@#1\endcsname}
\newcommand{\useshortcutsymbol}[1]{%
  \global\expandafter\chardef\csname myscut@catcode@#1\endcsname\catcode`#1\relax
  \catcode`#1=\active
  \begingroup\lccode`~=`#1\relax
  \lowercase{\endgroup\def~##1~}{\@nameuse{myscut@shortcut@##1}}}
\newcommand{\undefineshortcutsymbol}[1]{%
  \catcode`#1\csname myscut@catcode@\string#1\endcsname}
\makeatother

\defineshortcut{C}[1]{C_\textnormal{#1}}
\defineshortcut{Cref}{C_\textnormal{ref}}

\begin{document}
\useshortcutsymbol{|}

$\scut{C}{ref}\quad\scut{Cref}$

$|C|{ref}\quad|Cref|$

\undefineshortcutsymbol{|}

$|C|$

\end{document}

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.

share|improve this answer
    
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 '12 at 17:50
    
That looks good. We only need something shorter then \scut... is \S still free? –  Jonas Stein Nov 11 '12 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 '12 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 '12 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 '12 at 22:25
show 4 more comments

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

 \newcommand*\sref{_{\textrm{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

 \newcommand*\trans{^{\textsf{T}}}
 \newcommand*\herm{^\dagger} 
share|improve this answer
add comment

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.

Example:

\documentclass{scrartcl}
\pagestyle{empty}

\usepackage{amsmath}
\usepackage{namespc}
\usepackage{cleveref}
%
\newcommand{\overwritecommand}[1]{\let#1\undefined\newcommand{#1}}
%
\begin{document}
  \section{First section}
  \label{sec:first}

  \namespace{shortcuts}{%
    \overwritecommand{\Cref}{mycref}
  }{}

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

  \begin{shortcuts}
    This is used from within the ``shortcuts'' environment: \Cref
  \end{shortcuts}

  \begin{align*}
    \usingnamespace{shortcuts}
    \text{Equation} = \text{\Cref}
  \end{align*}

  This is to access the macro explicitly: \::shortcuts::Cref::
\end{document}

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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