7

I'm writing an astronomy paper and have some symbols and variables which are common throughout. It's becoming cumbersome to write them out each time. Is there a way to create a shortcut of sorts for these? Note, most are in math-mode.

For example:
log(Hi)

$\log(\textrm{H}\textsc{i})$

log(SFR/M*)

$\log(\textrm{SFR} / \textrm{M}_{\odot})$
11
  • \def\loghi{\log(\textrm{H}\textsc{i})} Then in usage, $\loghi$. Note I left $ out of the \def, to allow it to be used in conjunction with other math; for example, $\loghi = x$. Commented May 10, 2014 at 1:44
  • Note that for non-math defs, for example \def\rms{root-mean-square}, its usage in text will usually be the \rms{} of..., with the empty braces following, otherwise the succeeding blank gets eaten by the TeX parser. Commented May 10, 2014 at 1:48
  • 1
    Wouldn't this be a job for a \newcommand? Commented May 10, 2014 at 2:01
  • 1
    @cslstr Many people say so, but the author of xspace isn't so sure: tex.stackexchange.com/questions/86565/drawbacks-of-xspace Commented May 10, 2014 at 2:50
  • 2
    @AndyClifton Yes, \newcommand is "safer" than \def in that, if a macro by that name already exists, it will break the compilation, rather than allowing the original definition to be overwritten by the new \def (which could cause strange, difficult to debug, errors). Call it typing laziness on my part. Commented May 10, 2014 at 2:53

2 Answers 2

5

Creating macros that provide shortcuts for frequently used strings is a very good idea. For an entry-level introduction to the uses of the LaTeX \newcommand directive see, e.g., Chapter 6, "Customizing LaTeX", of the guide The Not So Short Introduction to LATEX2e.

For instance, you could issue the instructions

\newcommand\loghi{\log(\mathrm{H}\textsc{i})}
\newcommand\sfrm{\log(\mathrm{SFR} / \mathrm{M}_{\odot})}

in the preamble and, later on in the body of the text, write something like

bleet bleet $\loghi=\sfrm$ more bleet bleet

As you've already noted in a comment, a complication arises if these macros can also be used in bold surroundings such as sectioning headers. Specifically, given the above definition of \loghi, LaTeX will try to typeset the letter i in bold-smallcaps. As you've discovered, if the font family you're using does not feature a bold-smallcaps font, the letter will be set in "ordinary" bold, i.e., as i. Not the desired effect, right?!

What to do? I think you have (at least) two options. The first is to use a font family that does feature a bold-smallcap font. Two such families arenewt xtext/newtxmath and newpxtext/newpxmath . The former provides a "Times Roman" look, the latter a "Palatino" look. This may (or may not...) be acceptable to you.

Second, you could set up the macro that defines \loghi in such a way that it won't even try to use a (possibly nonexistent) bold smallcaps glyph. For instance,

\newcommand\loghi{\log(\mathrm{H}\textsc{\mdseries i})}

(note the addition of \mdseries to the argument of \textsc) instructs LaTeX to apply \textsc to a "medium weight" (rather than a "bold weight") version of i. As long as the medium-weight font features smallcaps glyphs, you'll be guaranteed success.

4
  • Fantastic answer and details. I really appreciate the advice on this one. Thank you.
    – Carl
    Commented May 10, 2014 at 6:22
  • 1
    @Carl - Thanks for the compliment! Incidentally, if the symbols "H" and "i" belong together in some way, it may be better -- from a syntactic point of view -- to set up the shortcut macro as \newcommand\loghi{\log(\textsc{\mdseries Hi})}. An uppercase letter is rendered "as is" when subjected to \textsc, so the look of "H" using either definition will be the same. Nevertheless, even though the appearance will be the same, it's still useful to tailor the shortcut definition to obey all syntactic aspects of the expression at hand.
    – Mico
    Commented May 10, 2014 at 7:14
  • If you want to use the macro in header and use the hyperref package, you should also wrap it in a \texorpdfstring
    – Cephalopod
    Commented May 10, 2014 at 12:01
  • @Arian - I'm not sure how your comment relates the posting's topic, which is how to go about creating shortcuts for recurring expressions. Do verify for yourself that the issue with hyperref (specifically, the possible need for a \texorpdfstring directive) arises equally if the expression is generated via a shortcut macro or if it is written out fully from the start, i.e., without a shortcut macro.
    – Mico
    Commented May 10, 2014 at 15:57
7

I would split the problem into two parts. First you define the elementary symbols, then expressions in terms of them:

\documentclass{article}
\usepackage{amsmath}

%% first the symbols
\newcommand{\Hi}{\textnormal{H\kern-.1em\textsc{i}}}
\newcommand{\SFR}{\mathrm{SFR}}
\newcommand{\Mo}{\mathrm{M}_{\odot}}
%% then common expressions
\newcommand{\lHi}{\log\Hi}
\newcommand{\lsfrmo}{\log(\SFR/\Mo)}

\begin{document}
\begin{gather*}
\log\Hi + \log(\SFR/\Mo)\\
\lHi+\lsfrmo
\end{gather*}
\end{document}

I've added some bit to the definition of \Hi just to show how you can redefine the elementary symbol without any need to modify also the definition of the expression defined in terms of it.

With \textnormal{...} we ensure the font will be the “initial” one, independently of the context. This is needed for \Hi, but not for \SFR; since \mathrm is more efficient than \textnormal, it's preferable when it's possible to use it.

enter image description here

You must log in to answer this question.

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