New command for subscript with text

macros

I am typing a file where I often have to write $T_{\text{area}}$. But the command for adding text in a subscript is quite long when I have to write it all the time. Do anyone have any tips on a newcommand that could simplify this, for example where I can type T_area and that gives me the correct format?

I tried to use

\newcommand{T_area}{S_{\text{area}}}

but it does not work. Also It would be nice to generalize it for all texts, not ony "area".

Best Answer

The instruction

\newcommand{T_area}{S_{\text{area}}}

is faulty for two separate reasons. First, TeX macros should generally start with a \ (backslash) character. (Well, there is the \csname ... \endcsname possibility; however, that's clearly not what you're concerned with.) Second, TeX control sequences should consist of either one or more lowercase and uppercase alphabetical letters (also known as a to z and A to Z...) or exactly one non-letter character. Examples of non-letter control sequences are \,, \!, \_, \$, \%, \\, etc.

Building on these two insights, the alternative definition

\newcommand{\Tarea}{S_{\text{area}}}

is valid -- but only from a purely syntactical point of view. As the comments posted below the query have already pointed out, the output of the \text macro is not as robust as you may think it is. Try executing $\Tarea$ \textit{$\Tarea$} \textsc{$\Tarea$} \texttt{$\Tarea$} and be prepared to be surprised. If you want the subscript material to be typeset exclusively in the upright shape of the document's main text font, give

\newcommand\TareaX{S_{\textnormal{area}}}

a try. Of course, you are entirely free to choose a different macro name.

Also It would be nice to generalize it for all texts, not ony "area".

Using some unavoidable LaTeX jargon, this objective may be achieved by specifying that \TareaX takes an optional argument with default value area:

\newcommand\TareaX[1][area]{S_{\textnormal{#1}}}

A final remark: Since the TeX-special character _ occurs in the definitions of the macros \Tarea and \TareaX, TeX will issue a (possibly mysterious) warning or error message if these macros are not used while inside math mode.


enter image description here

\documentclass{article}
\usepackage{amsmath} % for \text macro

\newcommand\Tarea{S_{\text{area}}} % problematic
\newcommand\TareaX[1][area]{S_{\textnormal{#1}}} % fine

\begin{document}
$\Tarea$ \textit{$\Tarea$} \textsc{$\Tarea$} \texttt{$\Tarea$} --- not good

$\TareaX$ \textit{$\TareaX$} \textsc{$\TareaX[volume]$} \texttt{$\TareaX[length]$} --- good
\end{document}
Related Question